我是Pandas的新手,我找不到方法,如何使用另一个日期范围通过切片获取日期范围。 使用整数从文档切片的示例效果很好:
In [47]: pd.date_range('04-25-2016 10:00', '04-25-2016 20:00', freq='min')[1:3]
Out[47]: DatetimeIndex(['2016-04-25 10:01:00', '2016-04-25 10:02:00'], dtype='datetime64[ns]', freq='T')
但使用其他date_range
进行切片无效:
In [71]: work_range = pd.date_range('04-25-2016 10:00', '04-25-2016 20:00', freq='min')
In [72]: break_range = pd.date_range('04-25-2016 11:00', '04-25-2016 13:30', freq='min')
In [73]: free_time = work_range[break_range]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-73-361d67af3291> in <module>()
----> 1 free_time = work_range[break_range]
/usr/local/lib/python2.7/dist-packages/pandas/tseries/base.pyc in __getitem__(self, key)
173 attribs['freq'] = freq
174
--> 175 result = getitem(key)
176 if result.ndim > 1:
177 return result
IndexError: index -803545088 is out of bounds for axis 1 with size 601
或者我使用date_range
错误的方式?
我的完整例子:
import pandas as pd
work_range = pd.date_range('04-25-2016 10:00', '04-25-2016 20:00', freq='min')
break_ranges = list()
break_ranges.append(pd.date_range('04-25-2016 11:00', '04-25-2016 13:30', freq='min'))
break_ranges.append(pd.date_range('04-25-2016 15:00', '04-25-2016 15:30', freq='min'))
break_ranges.append(pd.date_range('04-25-2016 13:00', '04-25-2016 13:05', freq='min'))
free_ranges = list()
for range in break_ranges:
# slicing work_range here and put it into `free_ranges` list
答案 0 :(得分:1)
如果您的DataFrame使用DatetimeIndex索引:
work_range = pd.date_range('04-25-2016 10:00', '04-25-2016 20:00', freq='min')
break_range = pd.date_range('04-25-2016 11:00', '04-25-2016 13:30', freq='min')
df = pd.DataFrame({'col': range(len(work_range))}, index=work_range)
>>> len(work_range)
601
>>> len(break_range)
151
>>> df.shape
(601, 1)
# Use `.loc` to select specific range.
>>> df.loc[break_range].shape
(151, 1)
如果您的索引是列值:
df['idx'] = df.index
>>> df.loc[df.idx.isin(break_range)].shape
(151, 2)
要从结果中排除日期范围,只需使用tilda(~
)否定字符:
>>> df.loc[~df.idx.isin(break_range)].shape
(450, 2)
如果您只是使用索引本身。
>>> work_range[work_range.isin(break_range)].shape
(151,)
# To exclude dates:
>>> work_range[~work_range.isin(break_range)].shape
(450,)