我正在尝试使用 Pandas,使用 select 和 terms 从 HDFStore 中检索存储的数据。一个没有条件的简单 select() 返回所有数据。但是,当我尝试根据 DateTimeIndex 过滤数据时,会返回除最后一行之外的所有内容。
我怀疑关于时间戳如何在内部存储以及它们的精度有些可疑,但我看不出它为什么不起作用或我能做些什么。任何指针都会有所帮助,因为我对此很陌生。
我创建了一个小型“单元测试”来调查......
import os
import tempfile
import uuid
import pandas as pd
import numpy as np
import time
import unittest
import sys
class PandasTestCase(unittest.TestCase):
def setUp(self):
print "Pandas version: {0}".format(pd.version.version)
print "Python version: {0}".format(sys.version)
self._filename = os.path.join(tempfile.gettempdir(), '{0}.{1}'.format(str(uuid.uuid4()), 'h5'))
self._store = pd.HDFStore(self._filename)
def tearDown(self):
self._store.close()
if os.path.isfile(self._filename):
os.remove(self._filename)
def test_filtering(self):
t_start = time.time() * 1e+9
t_end = t_start + 1e+9 # 1 second later, i.e. 10^9 ns
sample_count = 1000
timestamps = np.linspace(t_start, t_end, num=sample_count).tolist()
data = {'channel_a': range(sample_count)}
time_index = pd.to_datetime(timestamps, utc=True, unit='ns')
df = pd.DataFrame(data, index=time_index, dtype=long)
key = 'test'
self._store.append(key, df)
retrieved_df = self._store.select(key)
retrieved_timestamps = np.array(retrieved_df.index.values, dtype=np.uint64).tolist()
print "Retrieved {0} timestamps, w/o filter.".format(len(retrieved_timestamps))
self.assertItemsEqual(retrieved_timestamps, timestamps)
stored_time_index = self._store[key].index
# Create a filter based on first and last values of index, i.e. from <= index <= to.
from_filter = pd.Term('index>={0}'.format(pd.to_datetime(stored_time_index[0], utc=True, unit='ns')))
to_filter = pd.Term('index<={0}'.format(pd.to_datetime(stored_time_index[-1], utc=True, unit='ns')))
retrieved_df_interval = self._store.select(key, [from_filter, to_filter])
retrieved_timestamps_interval = np.array(retrieved_df_interval.index.values, dtype=np.uint64).tolist()
print "Retrieved {0} timestamps, using filter".format(len(retrieved_timestamps_interval))
self.assertItemsEqual(retrieved_timestamps_interval, timestamps)
if __name__ == '__main__':
unittest.main()
...输出以下内容:
Pandas version: 0.12.0
Python version: 2.7.3 (default, Apr 10 2013, 06:20:15)
[GCC 4.6.3]
Retrieved 1000 timestamps, w/o filter.
Retrieved 999 timestamps, using filter
F
======================================================================
FAIL: test_filtering (__main__.PandasTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "pandastest.py", line 53, in test_filtering
self.assertItemsEqual(retrieved_timestamps_interval, timestamps)
AssertionError: Element counts were not equal:
First has 1, Second has 0: 1.377701660170978e+18
----------------------------------------------------------------------
Ran 1 test in 0.039s
FAILED (failures=1)
Process finished with exit code 1
更新:修改条款的创建后,使用备用构造函数,一切正常。像这样:
# Create a filter based on first and last values of index, i.e. from <= index <= to.
#from_filter = pd.Term('index>={0}'.format(pd.to_datetime(stored_time_index[0], utc=True, unit='ns')))
from_filter = pd.Term('index','>=', stored_time_index[0])
#to_filter = pd.Term('index<={0}'.format(pd.to_datetime(stored_time_index[-1], utc=True, unit='ns')))
to_filter = pd.Term('index','<=', stored_time_index[-1])