python pandas数据框根据日期条件切片

问题描述:

我可以使用python datetime对象读取和切割大熊猫数据帧,但是我被迫仅在索引中使用现有日期。例如,这样做:

I am able to read and slice pandas dataframe using python datetime objects, however I am forced to use only existing dates in index. For example, this works:

>>> data
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 252 entries, 2010-12-31 00:00:00 to 2010-04-01 00:00:00
Data columns:
Adj Close    252  non-null values
dtypes: float64(1)

>>> st = datetime.datetime(2010, 12, 31, 0, 0)
>>> en = datetime.datetime(2010, 12, 28, 0, 0)

>>> data[st:en]
            Adj Close
Date                 
2010-12-31     593.97
2010-12-30     598.86
2010-12-29     601.00
2010-12-28     598.92

但是,如果我使用不存在的开始或结束日期DF,我得到python KeyError。

However if I use a start or end date that is not present in the DF, I get python KeyError.

我的问题:如何查询日期范围的数据框对象?即使开始和结束日期不存在于DataFrame中。大熊猫是否允许基于范围的切片?

My Question : How do I query the dataframe object for a date range; even when the start and end dates are not present in the DataFrame. Does pandas allow for range based slicing?

我使用的是大熊猫版本0.10.1

I am using pandas version 0.10.1

使用 searchsorted 先找到最近的时间,然后使用它切片。

Use searchsorted to find the nearest times first, and then use it to slice.

In [15]: df = pd.DataFrame([1, 2, 3], index=[dt.datetime(2013, 1, 1), dt.datetime(2013, 1, 3), dt.datetime(2013, 1, 5)])

In [16]: df
Out[16]: 
            0
2013-01-01  1
2013-01-03  2
2013-01-05  3

In [22]: start = df.index.searchsorted(dt.datetime(2013, 1, 2))

In [23]: end = df.index.searchsorted(dt.datetime(2013, 1, 4))

In [24]: df.ix[start:end]
Out[24]: 
            0
2013-01-03  2