简体   繁体   English

between_time在熊猫面板上不起作用

[英]between_time not working on a pandas panel

The method between_time is defined in a pandas panel object, I would expect this method to be applied to all dataframes in the panel, but this is not working. 方法between_time是在pandas面板对象中定义的,我希望此方法可以应用于面板中的所有数据框,但是不能正常工作。 So what is this method doing? 那么这种方法在做什么呢?

Example

rng = pd.date_range('1/1/2013',periods=100,freq='D')
data = np.random.randn(100, 4)
cols = ['A','B','C','D']
df1, df2, df3 = pd.DataFrame(data, rng, cols), pd.DataFrame(data, rng, cols), pd.DataFrame(data, rng, cols)
pf = pd.Panel({'df1':df1,'df2':df2,'df3':df3})
pf.between_time('08:00:00', '09:00:00')

The above code returns this error 上面的代码返回此错误

Traceback (most recent call last): File "", line 1, in File "/usr/lib64/python2.7/site-packages/pandas-0.14.0-py2.7-linux-x86_64.egg/pandas/core/generic.py", line 2796, in between_time raise TypeError('Index must be DatetimeIndex') TypeError: Index must be DatetimeIndex 追溯(最近一次通话):文件“ /usr/lib64/python2.7/site-packages/pandas-0.14.0-py2.7-linux-x86_64.egg/pandas/core中的文件“”,行1 /generic.py“,第2796行,介于between_time中,引发TypeError('索引必须为DatetimeIndex')TypeError:索引必须为DatetimeIndex

You don't actually have any times that would be selected, so I changed your example a bit (you were using D frequency) 您实际上没有选择任何时间,因此我对示例进行了一些更改(您使用的是D频率)

In [17]: rng = pd.date_range('1/1/2013',periods=100,freq='H')

In [18]: data = np.random.randn(100, 4)

In [19]: df1, df2, df3 = pd.DataFrame(data, rng, cols), pd.DataFrame(data, rng, cols), pd.DataFrame(data, rng, cols)

In [20]: pf = pd.Panel({'df1':df1,'df2':df2,'df3':df3})

In [21]: pf
Out[21]: 
<class 'pandas.core.panel.Panel'>
Dimensions: 3 (items) x 100 (major_axis) x 4 (minor_axis)
Items axis: df1 to df3
Major_axis axis: 2013-01-01 00:00:00 to 2013-01-05 03:00:00
Minor_axis axis: A to D

The axis parameter is not implemented at the moment, so you can do this: 目前尚未实现axis参数,因此您可以执行以下操作:

In [22]: indexer = pf.major_axis.indexer_between_time('08:00','09:00')

In [23]: pf.take(indexer,axis='major')
Out[23]: 
<class 'pandas.core.panel.Panel'>
Dimensions: 3 (items) x 8 (major_axis) x 4 (minor_axis)
Items axis: df1 to df3
Major_axis axis: 2013-01-01 08:00:00 to 2013-01-04 09:00:00
Minor_axis axis: A to D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM