Define column names of a table using pytables using a for loop inside the class definition - pytables

We know that if we need to define the column names of a table using pytables we can do it by the following way:
class Project(IsDescription):
alpha = StringCol(20)
beta = StringCol(20)
gamma = StringCol(20)
where alpha, beta and gamma are the desired column names of the table.
But suppose I would like to use a list "ColumnNames_list" which contains the column names as follows:
ColumnNames_list[0] = alpha, ColumnNames_list[1] = beta, ColumnNames_list[2] = gamma
Then how should I define the above class "Project"?
I tried with the following:
ColumnNames_list = []
ColumnNames_list[0] = alpha
ColumnNames_list[1] = beta
ColumnNames_list[2] = gamma
class Project(IsDescription):
for i in range (0, 10):
ColumnNames_list[i] = StringCol(20)
It's showing the error:
TypeError: Passing an incorrect value to a table column. Expected a Col (or subclass) instance and got: "2". Please make use of the Col(), or descendant, constructor to properly initialize columns.

Define the variable first outside the loop:
ColumnNames_list = []
for i in range (0, 10):
ColumnNames_list.append(StringCol(20))
The reason I'm using append() rather than ColumnNames_list[i] = StringCol(20) is because you can't assign to an index that doesn't exist. Trying to assign ColumnNames_list[1] before it exists throws an IndexError.

Related

Convert a spark dataframe to a column

I have a org.apache.spark.sql.DataFrame and I would like to convert it into a column: org.apache.spark.sql.Column.
So basically, this is my dataframe:
val filled_column2 = x.select(first(("col1"),ignoreNulls = true).over(window)) that I want to convert, it into an sql spark column. Anyone could help on that ?
Thank you,
#Jaime Caffarel: this is exactly what i am trying to do, this will give you more visibility. You may also check the error msg in the 2d screenshot
From the documentation of the class org.apache.spark.sql.Column
A column that will be computed based on the data in a DataFrame. A new
column is constructed based on the input columns present in a
dataframe:
df("columnName") // On a specific DataFrame.
col("columnName") // A generic column no yet associcated
with a DataFrame. col("columnName.field") // Extracting a
struct field col("a.column.with.dots") // Escape . in column
names. $"columnName" // Scala short hand for a named
column. expr("a + 1") // A column that is constructed
from a parsed SQL Expression. lit("abc") // A
column that produces a literal (constant) value.
If filled_column2 is a DataFrame, you could do:
filled_column2("col1")
******** EDITED AFTER CLARIFICATION ************
Ok, it seems to me that what you are trying to do is a JOIN operation. Assuming that the product_id is a unique key per each row, I would do something like this:
val filled_column = df.select(df("product_id"), last(("last_prev_week_nopromo"), ignoreNulls = true) over window)
This way, you are also selecting the product_id that you will use as key. Then, you can do the following
val promo_txn_cnt_seas_df2 = promo_txn_cnt_seas_df1
.join(filled_column, promo_txn_cnt_seas_df1("product_id") === filled_column("driver_id"), "inner")
// orderBy("product_id", "week")... (the rest of the operations)
Is this what you are trying to achieve?

A type error: 'builtin_function_or_method' object does not support item assignment. How can I fix that?

I'm running the following code and got a type error:
TypeError: 'builtin_function_or_method' object does not support item assignment
Here's my code:
N_object4 = 4
alpha = np.random.normal(0, 10, N_object4)# Random alpha values (could be greater or less than 0.)
pa = np.abs(alpha)
num = pa.argsort()[-3:][::-1]
gs = np.zeros(N_object4).tolist
for i in range (len(num)): # Iterating from largest abs(alpha) to the smallest.
if alpha[num[i]] > 0:
gs[num[i]+1] = 1
The error happens in my last line. How can I fix this error? Thanks!!
I think its small typo in line 4. You should use tolist():
gs = np.zeros(N_object4).tolist()

Error Handling with Live Data Matlab

I am using a live data API that is returning the next arriving trains. I plan on giving the user the next 5 trains arriving. If there are less than 5 trains arriving, how you handle that? I am having trouble thinking of a way, I was thinking a way with if statements but don't know how I would set them up.
time1Depart = dataReturnedFromLiveAPI{1,1}.orig_departure_time;
time2Depart = dataReturnedFromLiveAPI{1,2}.orig_departure_time;
time3Depart = dataReturnedFromLiveAPI{1,3}.orig_departure_time;
time4Depart = dataReturnedFromLiveAPI{1,4}.orig_departure_time;
time5Depart = dataReturnedFromLiveAPI{1,5}.orig_departure_time;
time1Arrival = dataReturnedFromLiveAPI{1,1}.orig_arrival_time;
time2Arrival = dataReturnedFromLiveAPI{1,2}.orig_arrival_time;
time3Arrival = dataReturnedFromLiveAPI{1,3}.orig_arrival_time;
time4Arrival = dataReturnedFromLiveAPI{1,4}.orig_arrival_time;
time5Arrival = dataReturnedFromLiveAPI{1,5}.orig_arrival_time;
The code right now uses a matrix that goes from 1:numoftrains but I am using just the first five.
It's a bad practice to assign individual value to a separate variable. Better if you pass all related values to a vector or cell array depending on class of orig_departure_time and orig_arrival_time.
It looks like dataReturnedFromLiveAPI is a cell array of structures. Then you can do:
timeDepart = cellfun(#(x), x.orig_departure_time, ...
dataReturnedFromLiveAPI(1,1:min(5,size(dataReturnedFromLiveAPI,2))), ...
'UniformOutput',0 );
timeArrival = cellfun(#(x), x.orig_arrival_time, ...
dataReturnedFromLiveAPI(1,1:min(5,size(dataReturnedFromLiveAPI,2))), ...
'UniformOutput',0 );
Then you how to access the values one by one as
time1Depart = timeDepart{1};
If orig_departure_time and orig_arrival_time are numeric scalars, you can use ...'UniformOutput',1.... You will get output as a vector and can get single values with timeDepart(1).

Name a Table with a Variable in LUA (LÖVE Engine)?

Basically:
I am making a game in the LÖVE Engine where you click to create blocks
Every time you click, a block gets created at your Mouse X and Mouse Y
But, I can only get one block to appear, because I have to name that block (or table) 'object1'
Is there any way to create table after table with increasing values? (like object1{}, object2{}, object3{}, etc... But within the main table, 'created_objects')
But only when clicked, which I suppose rules out the looping part (but if it doesn't please tell me)
Here's my code so far, but it doesn't compile.
function object_create(x, y, id) **--Agruments telling the function where the box should spawn and what the ID of the box is (variable 'obj_count' which increases every time a box is spawned)**
currobj = "obj" .. id **--Gives my currently created object a name**
crob.[currobj] = {} **--Is supposed to create a table for the current object, which holds all of its properties. **
crob.[currobj].body = love.physics.newBody(world, x, y, "dynamic")
crob.[currobj].shape = love.physics.newRectangleShape(30, 30)
crob.[currobj].fixture = love.physics.newFixture(crob.[currobj].body, crob.[currobj].shape, 1) **--The properties**
crob.[currobj].fixture:setRestitution(0.3)
But what should I replace [currobj] with?
Solved
Found what I was looking for. Here's the code if people are wondering:
function block_create(x, y, id) --(the mouse x and y, and the variable that increases)
blocks[id] = {}
blocks[id][1] = love.physics.newBody(world, x, y, "dynamic")
blocks[id][2] = love.physics.newRectangleShape(45, 45)
blocks[id][3] = love.physics.newFixture(blocks[id][1], blocks[id][2])
blocks[id][3]:setRestitution(0.2)
blocks[id][4] = math.random(0, 255) --The Color
blocks[id][5] = math.random(0, 255)
blocks[id][6] = math.random(0, 255)
blockcount = blockcount + 1
i would probably do something like this.
local blocks = {} -- equivalent of your crob table
function create_block(x, y)
local block = funcToCreateBlock() -- whatever code to create the block
table.insert(blocks, block)
return block
end
if you wanted to get a reference to the block you just created with the function, just capture it.
-- gives you the new block, automatically adds it to the list of created blocks
local new_block = create_block(0, 10)
that sticks block objects in your block table and automatically gives each one a numeric index in the table. so if you called create_block() 3 times for 3 different mouse clicks, the blocks table would look like this:
blocks = {
[1] = blockobj1,
[2] = blockobj2,
[3] = blockobj3
}
you could get the second block obj from the blocks table by doing
local block2 = blocks[2]
or you could loop over all the blocks in the table using pairs or ipairs
for idx, block in pairs(blocks) do
-- do something with each block
end
sorry if this doesn't exactly answer your question. but from what you wrote, there didn't seem to be a real reason why you'd need to name the blocks anything specific in the crob table.
If you want those tables to be global, then you can do something like:
sNameOfTable = "NAME"
_G[sNameOfTable] = {1,2}
and then you will have a table variable NAME as depicted here (Codepad).
Otherwise, if you want it to be a child to some other table, something like this would also do:
tTbl = {}
for i = 1, 20 do
local sName = string.format( "NAME%02d", i )
tTbl[sName] = {1,2}
end
for i, v in pairs(tTbl) do
print( i, v )
end
Don't worry about the unsorted output here(Codepad). Lua tables with indexes need not be sorted to be used.

nested pytables

Suppose you are passing a dictionary to the pytable constructor:
h5f.createTable('/','table',{'col1':Float64Col(pos=0),'col2':StringCol(16,pos=1)})
I have the following three beginner's questions related to nested pytables:
1) How do you use a dictionary descriptor for creating a nested pytable?
2) How do you assign positions for the nested columns?
If the top-level column has position pos=1, do you start numbering its subcolumns from 0?
3) How do you assign rows to the nested column?
Thanks for helping!
I've been dynamically creating pytables description using python type(). This should at least get you going...
from tables import *
h5_file = openFile('test_nested_table.h5', 'w')
nested_table_fields = {}
nested_table_fields['x'] = Float64Col(dflt=1, pos=0)
nested_table = type('nested_table', (IsDescription,), nested_table_fields)
main_table_fields = {}
main_table_fields['y'] = Float64Col(dflt=1, pos=0)
main_table_fields['nested_table'] = nested_table
main_table = type('main_table', (IsDescription,), main_table_fields)
h5_table = h5_file.createTable('/', 'nested_table_example', main_table)
print repr(h5_table)