HDFStore:检查多索引(Datacolumns)是否包含特定日期

2024-09-30 02:31:14 发布

您现在位置:Python中文网/ 问答频道 /正文

当前表格结构:

store.get_storer('stocks').table
Out[65]:
/stocks/table (Table(26543,)) ''
  description := {
  "index": Int64Col(shape=(), dflt=0, pos=0),
  "values_block_0": Float64Col(shape=(4,), dflt=0.0, pos=1),
  "values_block_1": Int64Col(shape=(1,), dflt=0, pos=2),
  "Name": StringCol(itemsize=40, shape=(), dflt=b'', pos=3),
  "Code": StringCol(itemsize=8, shape=(), dflt=b'', pos=4),
  "Date": Int64Col(shape=(), dflt=0, pos=5)}
  byteorder := 'little'
  chunkshape := (630,)
  autoindex := True
  colindexes := {
    "index": Index(6, medium, shuffle, zlib(1)).is_csi=False,
    "Name": Index(6, medium, shuffle, zlib(1)).is_csi=False,
    "Code": Index(6, medium, shuffle, zlib(1)).is_csi=False,
    "Date": Index(6, medium, shuffle, zlib(1)).is_csi=False}

我尝试检查某个日期的索引是否存在: 我的第一次尝试引发语法错误:

tod = dt.datetime.today().date()

store = pd.HDFStore(f'{wkd}Database.h5)

df = store.select('stocks', f'Date={tod}')

if len(df) > 0:

   print('Found something!')

这段代码也有同样的错误:

g = store.get_storer('stocks')
g.read_axes(f'Date={lastday}')

这是错误消息:

  File "<ipython-input-13-0cbe359284fa>", line 1, in <module>
    g.read_axes(f'Date={lastday}')

  File "path2venv/lib/python3.8/site-packages/pandas/io/pytables.py", line 3665, in read_axes
    self.selection = Selection(self, where=where, **kwargs)

  File "path2venv/lib/python3.8/site-packages/pandas/io/pytables.py", line 5003, in __init__
    self.terms = self.generate(where)

  File "path2venv/lib/python3.8/site-packages/pandas/io/pytables.py", line 5016, in generate
    return Expr(where, queryables=q, encoding=self.table.encoding)

  File "path2venv/lib/python3.8/site-packages/pandas/core/computtion/pytables.py", line 551, in __init__
    self.terms = self.parse()

  File "path2venv/lib/python3.8/site-packages/pandas/core/computation/expr.py", line 847, in parse
    return self._visitor.visit(self.expr)

  File "path2venv/lib/python3.8/site-packages/pandas/core/computation/expr.py", line 437, in visit
    raise e

  File "path2venv/lib/python3.8/site-packages/pandas/core/computation/expr.py", line 431, in visit
    node = ast.fix_missing_locations(ast.parse(clean))

  File "/usr/lib64/python3.8/ast.py", line 47, in parse
    return compile(source, filename, mode, flags,

  File "<unknown>", line 1
    Date ==2020 -0 3 -0 7

如何将日期传递给查询? 是否有更好的解决方案来检查索引列

编辑:也许是个bug


Tags: storeinpyposselfpandasdatelib

热门问题