恐怕不是你想要的。但这可能会有所帮助:
我使用这个数据集http://archive.ics.uci.edu/ml/datasets/Occupancy+Detection+# 进行测试。
对于日期时间索引,解析为时间戳的日期和字符串可以作为索引参数传递,也可以作为 truncate 和 pd.Timestamp 的 before 和 after 参数传递。
In [1]: import pandas as pd
In [2]: df = pd.read_csv('datatest2.txt', parse_dates=[1], index_col=[1])
In [3]: df.index
Out[3]:
DatetimeIndex(['2015-02-11 14:48:00', '2015-02-11 14:49:00',
'2015-02-11 14:50:00', '2015-02-11 14:51:00',
'2015-02-11 14:51:59', '2015-02-11 14:53:00',
'2015-02-11 14:54:00', '2015-02-11 14:55:00',
'2015-02-11 14:55:59', '2015-02-11 14:57:00',
...
'2015-02-18 09:10:00', '2015-02-18 09:10:59',
'2015-02-18 09:11:59', '2015-02-18 09:13:00',
'2015-02-18 09:14:00', '2015-02-18 09:15:00',
'2015-02-18 09:16:00', '2015-02-18 09:16:59',
'2015-02-18 09:17:59', '2015-02-18 09:19:00'],
dtype='datetime64[ns]', name='date', length=9752, freq=None)
In [4]: df['2015-02-12':'2015-02-13'].index
Out[4]:
DatetimeIndex(['2015-02-12 00:00:00', '2015-02-12 00:01:00',
'2015-02-12 00:02:00', '2015-02-12 00:03:00',
'2015-02-12 00:04:00', '2015-02-12 00:04:59',
'2015-02-12 00:06:00', '2015-02-12 00:07:00',
'2015-02-12 00:08:00', '2015-02-12 00:08:59',
...
'2015-02-13 23:50:00', '2015-02-13 23:51:00',
'2015-02-13 23:51:59', '2015-02-13 23:53:00',
'2015-02-13 23:54:00', '2015-02-13 23:55:00',
'2015-02-13 23:55:59', '2015-02-13 23:57:00',
'2015-02-13 23:57:59', '2015-02-13 23:58:59'],
dtype='datetime64[ns]', name='date', length=2880, freq=None)
In [5]: df.truncate(before=pd.Timestamp('2015-02-12'), after=pd.Timestamp('2015-02-14')).index
Out[5]:
DatetimeIndex(['2015-02-12 00:00:00', '2015-02-12 00:01:00',
'2015-02-12 00:02:00', '2015-02-12 00:03:00',
'2015-02-12 00:04:00', '2015-02-12 00:04:59',
'2015-02-12 00:06:00', '2015-02-12 00:07:00',
'2015-02-12 00:08:00', '2015-02-12 00:08:59',
...
'2015-02-13 23:51:00', '2015-02-13 23:51:59',
'2015-02-13 23:53:00', '2015-02-13 23:54:00',
'2015-02-13 23:55:00', '2015-02-13 23:55:59',
'2015-02-13 23:57:00', '2015-02-13 23:57:59',
'2015-02-13 23:58:59', '2015-02-14 00:00:00'],
dtype='datetime64[ns]', name='date', length=2881, freq=None)
In [6]: df.truncate(before='2015-02-12', after='2015-02-14').index
Out[6]:
DatetimeIndex(['2015-02-12 00:00:00', '2015-02-12 00:01:00',
'2015-02-12 00:02:00', '2015-02-12 00:03:00',
'2015-02-12 00:04:00', '2015-02-12 00:04:59',
'2015-02-12 00:06:00', '2015-02-12 00:07:00',
'2015-02-12 00:08:00', '2015-02-12 00:08:59',
...
'2015-02-13 23:51:00', '2015-02-13 23:51:59',
'2015-02-13 23:53:00', '2015-02-13 23:54:00',
'2015-02-13 23:55:00', '2015-02-13 23:55:59',
'2015-02-13 23:57:00', '2015-02-13 23:57:59',
'2015-02-13 23:58:59', '2015-02-14 00:00:00'],
dtype='datetime64[ns]', name='date', length=2881, freq=None)
In [7]: df.truncate(before='2015-02-12 01:00:00', after='2015-02-13 23:00:00').index
Out[7]:
DatetimeIndex(['2015-02-12 01:00:00', '2015-02-12 01:01:00',
'2015-02-12 01:01:59', '2015-02-12 01:02:59',
'2015-02-12 01:04:00', '2015-02-12 01:05:00',
'2015-02-12 01:06:00', '2015-02-12 01:07:00',
'2015-02-12 01:08:00', '2015-02-12 01:08:59',
...
'2015-02-13 22:51:00', '2015-02-13 22:52:00',
'2015-02-13 22:53:00', '2015-02-13 22:53:59',
'2015-02-13 22:54:59', '2015-02-13 22:56:00',
'2015-02-13 22:57:00', '2015-02-13 22:58:00',
'2015-02-13 22:59:00', '2015-02-13 22:59:59'],
dtype='datetime64[ns]', name='date', length=2761, freq=None)
因此我认为您只需要修改您的函数来验证是否通过了有效日期(和时间)。