python - Pandas 通过索引从 HDF5 中获取特定行

标签 python pandas hdf5

我有一个已写入 HDF5 文件的 pandas DataFrame。数据按时间戳索引,如下所示:

In [5]: df
Out[5]:
                          Codes   Price  Size
Time
2015-04-27 01:31:08-04:00     T  111.75    23
2015-04-27 01:31:39-04:00     T  111.80    23
2015-04-27 01:31:39-04:00     T  113.00    35
2015-04-27 01:34:14-04:00     T  113.00    85
2015-04-27 01:55:15-04:00     T  113.50   203
...                         ...     ...   ...
2015-05-26 11:35:00-04:00    CA  110.55   196
2015-05-26 11:35:00-04:00    CA  110.55    98
2015-05-26 11:35:00-04:00    CA  110.55   738
2015-05-26 11:35:00-04:00    CA  110.55    19
2015-05-26 11:37:01-04:00        110.55    12

我想创建一个可以传递 Pandas 的函数 DatetimeIndex它将返回一个 DataFrame,其中包含 DatetimeIndex 中每个时间戳之前或之前的行。

我遇到的问题是连接 read_hdf如果我要查找超过 30 行,查询将不起作用 -- 请参阅 [ pandas read_hdf with 'where' condition limitation?

我现在做的是这个,但是必须有更好的解决方案:

from pandas import read_hdf, DatetimeIndex
from datetime import timedelta
import pytz

def getRows(file, dataset, index):

    if len(index) == 1:
        start = index.date[0]
        end = (index.date + timedelta(days=1))[0]
    else:
        start = index.date.min()
        end = (index.date.max() + timedelta(days=1))

    where = '(index >= "' + str(start) + '") & (index < "' str(end) + '")'

    df = read_hdf(file, dataset, where=where)

    df = df.groupby(level=0).last().reindex(index, method='pad')

    return df

最佳答案

这是使用 where mask 的示例

In [22]: pd.set_option('max_rows',10)

In [23]: df = DataFrame({'A' : np.random.randn(100), 'B' : pd.date_range('20130101',periods=100)}).set_index('B')

In [24]: df
Out[24]: 
                   A
B                   
2013-01-01  0.493144
2013-01-02  0.421045
2013-01-03 -0.717824
2013-01-04  0.159865
2013-01-05 -0.485890
...              ...
2013-04-06 -0.805954
2013-04-07 -1.014333
2013-04-08  0.846877
2013-04-09 -1.646908
2013-04-10 -0.160927

[100 rows x 1 columns]

存储测试帧

In [25]: store = pd.HDFStore('test.h5',mode='w')

In [26]: store.append('df',df)

创建一个随机选择的日期。

In [27]: dates = df.index.take(np.random.randint(0,100,10))

In [28]: dates
Out[28]: DatetimeIndex(['2013-03-29', '2013-02-16', '2013-01-15', '2013-02-06', '2013-01-12', '2013-02-24', '2013-02-18', '2013-01-06', '2013-03-17', '2013-03-21'], dtype='datetime64[ns]', name=u'B', freq=None, tz=None)

选择索引列(完整)

In [29]: c = store.select_column('df','index')

In [30]: c
Out[30]: 
0    2013-01-01
1    2013-01-02
2    2013-01-03
3    2013-01-04
4    2013-01-05
        ...    
95   2013-04-06
96   2013-04-07
97   2013-04-08
98   2013-04-09
99   2013-04-10
Name: B, dtype: datetime64[ns]

选择所需的索引器。这实际上可能有些复杂,例如你可能想要一个 .reindex(method='nearest')

In [34]: c[c.isin(dates)] 
Out[34]: 
5    2013-01-06
11   2013-01-12
14   2013-01-15
36   2013-02-06
46   2013-02-16
48   2013-02-18
54   2013-02-24
75   2013-03-17
79   2013-03-21
87   2013-03-29
Name: B, dtype: datetime64[ns]

选择你想要的行

In [32]: store.select('df',where=c[c.isin(dates)].index)
Out[32]: 
                   A
B                   
2013-01-06  0.680930
2013-01-12  0.165923
2013-01-15 -0.517692
2013-02-06 -0.351020
2013-02-16  1.348973
2013-02-18  0.448890
2013-02-24 -1.078522
2013-03-17 -0.358597
2013-03-21 -0.482301
2013-03-29  0.343381

In [33]: store.close()

关于python - Pandas 通过索引从 HDF5 中获取特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30483754/

相关文章:

c++ - Visual Studio 2012 中的 HDF5DotNet

c++ - 使用传入参数在 C++ 中创建 3D 数组

python - 不确定如何在循环中使用列表理解

python - 基于python中的条件绘制多色线

python - 向 DataFrame 添加多个列并将它们设置为等于现有列

python - 何时在 Tensorflow 模型保存中使用 .ckpt、.hdf5 和 .pb 文件扩展名?

python - 使用 ftplib 循环 ftp 中的文件在第二个循环中返回 "No such file or directory"

python - 写入文件以便保留最后 N 行

python-3.x - 从具有值列表的列中检索唯一值

python - pandas 找到两个滚动最高点并计算斜率