python - 如何将 'date' 类型的值添加到 pytable?

标签 python pandas hdf5 pytables

我是 pytables 的新手,在尝试将日期类型值添加到使用 pytables 创建的表时遇到错误。这是我正在做的:

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

下一行:

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

返回错误:

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

还有这一行:

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

产生同样的错误:

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

如果我能做到这一点,我的最后一行将是:

r.append()

有什么建议吗?我找不到任何以这种方式使用 pytables 的工作示例,其中使用了“日期”类型的列。提前致谢...

最佳答案

正如类型名称 Time32Col 所暗示的,它需要一个 32 位整数。具体来说,它将是自 1970 年 1 月 1 日纪元以来的秒数。您可以使用 time 模块获取它,例如int(time.mktime(time.strptime("1/1/2012", "%m/%d/%y")))

关于python - 如何将 'date' 类型的值添加到 pytable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20985439/

相关文章:

python - 根据其他两列中的值,用另一个数据框中的值填充一个数据框中的新列? ( python / Pandas )

python - os.system() 在哪个 linux shell 下执行命令?

python - Pandas 替换完整单词字符串

Python Pandas 构建完整的对比矩阵

python - ImportError : No module named dateutil. 解析器

python - 提取具有多个分隔符的文本

Python 代码注释

c++ - hdf5/c++ 中的测试组存在

python - 如何将多个 hdf5 文件聚合成一个图像

python - 为什么如果我将多个空的 Pandas 系列放入 hdf5 hdf5 的大小如此巨大?