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")))
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...
해결책
다른 팁
http://www.pytables.org/usersguide/datatypes.html tells you that
"There are two types of time: 4-byte signed integer (time32) and 8-byte double precision floating point (time64). Both of them reflect the number of seconds since the Unix epoch, i.e. Jan 1 00:00:00 UTC 1970. They are stored in memory as NumPy's int32 and float64, respectively, and in the HDF5 file using the H5T_TIME class. Integer times are stored on disk as such, while floating point times are split into two signed integer values representing seconds and microseconds (beware: smaller decimals will be lost!)."
So, it seems that you have to convert the datetime object to a timedelta object and convert it to seconds afterwards. Only then can it be stored via pytables.
d = datetime.datetime.strptime('1/1/12', "%m/%d/%y")
i = d - datetime.datetime.strptime('1/1/70', "%m/%d/%y")
toStore = i.total_seconds()
r['trDate'] = toStore
r.append()