Search code examples
pythonpandashdf5pytables

How do I add a value of 'date' type to a pytable?


I'm new to pytables, and I'm getting errors while trying to add a date- type value to a table created with pytables. Here's what I'm doing:

from tables import *
import csv
import datetime

class Test(IsDescription):
    trDate = Time32Col()

str = 'C:testTable.h5'
fileh = open_file(str, mode='w')
group = fileh.createGroup("/",'pricing','daily pricing and vol')
table = fileh.create_table(group,'nodeName',Test,'Pricing and volume')

r = table.row

the next line:

r['trDate'] = datetime.datetime.strptime('1/1/12', "%m/%d/%y")

returns the error:

#TypeError: invalid type (<class 'datetime.datetime'>) for column ``trDate``
#> c:\users\me\desktop\untitled0.py(16)<module>()

and this line:

r['trDate'] = '1/1/12'

yields the same error:

#TypeError: invalid type (<class 'str'>) for column ``trDate``
#> c:\users\me\desktop\untitled0.py(21)<module>()

if I could get this far, my final line would be:

r.append()

Any suggestions? I can't find any working examples using pytables in this way, where a column of type 'date' is being used. Thanks in advance...


Solution

  • As the type name Time32Col implies, it wants a 32-bit integer. Specifically, it's going to be the number of seconds since the epoch, January 1, 1970. You can get this using the time module, e.g. int(time.mktime(time.strptime("1/1/2012", "%m/%d/%y")))