Discussion:
[pytables-users] Table.append() error w/ one column
Ken Walker
2018-11-26 17:03:22 UTC
Permalink
I've been tinkering with Table.append and ran into an error I can't
diagnose.
I started with the 'Particle" append example in the documentation here:
https://www.pytables.org/usersguide/libref/structured_storage.html#tables.Table.append

The description defines 5 columns and Table.append() works exactly as
documented:
class Particle(tb.IsDescription):
name = tb.StringCol(16, pos=1) # 16-character String
lati = tb.IntCol(pos=2) # integer
longi = tb.IntCol(pos=3) # integer
pressure = tb.Float32Col(pos=4) # float (single-precision)
temperature = tb.FloatCol(pos=5) # double (double-precision)

When I modified to only define the 'name' column, I get an error.
The same happens when I only define the 'pressure' column (at pos=1)
Table.append seems to work so long as I have 2 or more columns.
Is this a limitation of the Table.append method?

To verify I can append to a table with 1 column, I used the table.row /
append method (similar to tutorial 1).
The following code works:
import tables as tb, numpy as np
class Particle(tb.IsDescription):
name = tb.StringCol(16) # 16-character String

h5f = tb.open_file("particle_t1.h5", mode="w", title="Test file")
group1 = h5f.create_group("/", 'detector1', 'Detector1 information')
table1 = h5f.create_table(group1, 'readout1', Particle, "Readout example")
particle = table1.row
for i in range(3):
particle['name'] = 'Particle: %6d' % (i)
particle.append()

table1.flush()

for x in table1.iterrows() :
print (x['name'])

h5f.close()

The following code gives an error when appending to table1:
import tables as tb
class Particle(tb.IsDescription):
name = tb.StringCol(16, pos=1) # 16-character String

h5f = tb.open_file('append_t1.h5', mode='w')
group1 = h5f.create_group("/", 'detector1', 'Detector1 information')
table1 = h5f.create_table(group1, 'readout1', Particle, "A table")

# Append several rows in only one call
table1.append([("Particle: 10"),
("Particle: 11"),
("Particle: 12")])

for x in table1.iterrows():
print (x['name'])

h5f.close()

Am I missing something?
Thanks.
--
You received this message because you are subscribed to the Google Groups "pytables-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pytables-users+***@googlegroups.com.
To post to this group, send an email to pytables-***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Francesc Alted
2018-11-26 17:11:13 UTC
Permalink
Hi Ken,

Yes, to create an actual tuple, you missed the comma after the string (e.g.
("Particle: 10",)). After fixing this in your second example, this is
the output I am getting:

b'Particle: 10'

b'Particle: 11'

b'Particle: 12'

Cheers
Francesc
Post by Ken Walker
I've been tinkering with Table.append and ran into an error I can't
diagnose.
https://www.pytables.org/usersguide/libref/structured_storage.html#tables.Table.append
The description defines 5 columns and Table.append() works exactly as
name = tb.StringCol(16, pos=1) # 16-character String
lati = tb.IntCol(pos=2) # integer
longi = tb.IntCol(pos=3) # integer
pressure = tb.Float32Col(pos=4) # float (single-precision)
temperature = tb.FloatCol(pos=5) # double (double-precision)
When I modified to only define the 'name' column, I get an error.
The same happens when I only define the 'pressure' column (at pos=1)
Table.append seems to work so long as I have 2 or more columns.
Is this a limitation of the Table.append method?
To verify I can append to a table with 1 column, I used the table.row /
append method (similar to tutorial 1).
import tables as tb, numpy as np
name = tb.StringCol(16) # 16-character String
h5f = tb.open_file("particle_t1.h5", mode="w", title="Test file")
group1 = h5f.create_group("/", 'detector1', 'Detector1 information')
table1 = h5f.create_table(group1, 'readout1', Particle, "Readout example")
particle = table1.row
particle['name'] = 'Particle: %6d' % (i)
particle.append()
table1.flush()
print (x['name'])
h5f.close()
import tables as tb
name = tb.StringCol(16, pos=1) # 16-character String
h5f = tb.open_file('append_t1.h5', mode='w')
group1 = h5f.create_group("/", 'detector1', 'Detector1 information')
table1 = h5f.create_table(group1, 'readout1', Particle, "A table")
# Append several rows in only one call
table1.append([("Particle: 10"),
("Particle: 11"),
("Particle: 12")])
print (x['name'])
h5f.close()
Am I missing something?
Thanks.
--
You received this message because you are subscribed to the Google Groups
"pytables-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
Francesc Alted
--
You received this message because you are subscribed to the Google Groups "pytables-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pytables-users+***@googlegroups.com.
To post to this group, send an email to pytables-***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Ken Walker
2018-11-26 18:02:10 UTC
Permalink
That's embarrassing. LOL @ me
Thanks for the quick response.
Post by Francesc Alted
Hi Ken,
Yes, to create an actual tuple, you missed the comma after the string
(e.g. ("Particle: 10",)). After fixing this in your second example,
b'Particle: 10'
b'Particle: 11'
b'Particle: 12'
Cheers
Francesc
Post by Ken Walker
I've been tinkering with Table.append and ran into an error I can't
diagnose.
https://www.pytables.org/usersguide/libref/structured_storage.html#tables.Table.append
The description defines 5 columns and Table.append() works exactly as
name = tb.StringCol(16, pos=1) # 16-character String
lati = tb.IntCol(pos=2) # integer
longi = tb.IntCol(pos=3) # integer
pressure = tb.Float32Col(pos=4) # float (single-precision)
temperature = tb.FloatCol(pos=5) # double (double-precision)
When I modified to only define the 'name' column, I get an error.
The same happens when I only define the 'pressure' column (at pos=1)
Table.append seems to work so long as I have 2 or more columns.
Is this a limitation of the Table.append method?
To verify I can append to a table with 1 column, I used the table.row /
append method (similar to tutorial 1).
import tables as tb, numpy as np
name = tb.StringCol(16) # 16-character String
h5f = tb.open_file("particle_t1.h5", mode="w", title="Test file")
group1 = h5f.create_group("/", 'detector1', 'Detector1 information')
table1 = h5f.create_table(group1, 'readout1', Particle, "Readout example"
)
particle = table1.row
particle['name'] = 'Particle: %6d' % (i)
particle.append()
table1.flush()
print (x['name'])
h5f.close()
import tables as tb
name = tb.StringCol(16, pos=1) # 16-character String
h5f = tb.open_file('append_t1.h5', mode='w')
group1 = h5f.create_group("/", 'detector1', 'Detector1 information')
table1 = h5f.create_table(group1, 'readout1', Particle, "A table")
# Append several rows in only one call
table1.append([("Particle: 10"),
("Particle: 11"),
("Particle: 12")])
print (x['name'])
h5f.close()
Am I missing something?
Thanks.
--
You received this message because you are subscribed to the Google Groups
"pytables-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
<javascript:>.
For more options, visit https://groups.google.com/d/optout.
--
Francesc Alted
--
You received this message because you are subscribed to the Google Groups "pytables-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pytables-users+***@googlegroups.com.
To post to this group, send an email to pytables-***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...