我是Python的新手,我有一个包含日期的数据集S2
。当我使用命令时:
available_datapoints = S2.index,
然后
print(available_datapoints)
的产率:
<class 'pandas.tseries.index.DatetimeIndex'>
[2017-05-07 00:00:00+00:00, ..., 2017-07-27 23:50:00+00:00]
Length: 11808, Freq: 10T, Timezone: UTC stop
但是,我想要2017-05-07 00:00:00+00:00
而不是2017-11-07 00:00:00+00:00
,而不是2017-07-27 23:50:00+00:00
,我想停止2017-07-22 23:50:00+00:00
。
任何人都知道我如何改变这个?
答案 0 :(得分:1)
我认为您可以使用DataFrame.truncate
:
#Sample data
S2 = pd.DataFrame({'a': range(11808)},
index=pd.date_range(start='2017-05-07',periods=11808, freq='10T'))
print (S2.head())
a
2017-05-07 00:00:00 0
2017-05-07 00:10:00 1
2017-05-07 00:20:00 2
2017-05-07 00:30:00 3
2017-05-07 00:40:00 4
print (S2.tail())
a
2017-07-27 23:10:00 11803
2017-07-27 23:20:00 11804
2017-07-27 23:30:00 11805
2017-07-27 23:40:00 11806
2017-07-27 23:50:00 11807
S2 = S2.truncate(before='2017-07-11', after='2017-07-22 23:50:00')
print (S2.head())
a
2017-07-11 00:00:00 9360
2017-07-11 00:10:00 9361
2017-07-11 00:20:00 9362
2017-07-11 00:30:00 9363
2017-07-11 00:40:00 9364
print (S2.tail())
a
2017-07-22 23:10:00 11083
2017-07-22 23:20:00 11084
2017-07-22 23:30:00 11085
2017-07-22 23:40:00 11086
2017-07-22 23:50:00 11087
答案 1 :(得分:0)
假设你真的想要开始2017-07-11&#39;而不是2017-11-07&#39;(在2017-07-23&#39;结束后),您可以使用Partial String Indexing:
<强> SETUP 强>
df = pd.DataFrame(index = pd.date_range('2017-05-07 00:00:00+00:00','2017-07-27 23:50:00+00:00', freq='10T'))
print(df.index)
DatetimeIndex(['2017-05-07 00:00:00+00:00', '2017-05-07 00:10:00+00:00',
'2017-05-07 00:20:00+00:00', '2017-05-07 00:30:00+00:00',
'2017-05-07 00:40:00+00:00', '2017-05-07 00:50:00+00:00',
'2017-05-07 01:00:00+00:00', '2017-05-07 01:10:00+00:00',
'2017-05-07 01:20:00+00:00', '2017-05-07 01:30:00+00:00',
...
'2017-07-27 22:20:00+00:00', '2017-07-27 22:30:00+00:00',
'2017-07-27 22:40:00+00:00', '2017-07-27 22:50:00+00:00',
'2017-07-27 23:00:00+00:00', '2017-07-27 23:10:00+00:00',
'2017-07-27 23:20:00+00:00', '2017-07-27 23:30:00+00:00',
'2017-07-27 23:40:00+00:00', '2017-07-27 23:50:00+00:00'],
dtype='datetime64[ns, UTC]', length=11808, freq='10T')
现在,使用带切片的部分字符串索引:
df1 = df['2017-07-11':'2017-07-22 23:50:00']
print(df_1.index)
输出:在2017-07-11之前和2017-07-22 23:50之后的时间较小的数据帧下降:
DatetimeIndex(['2017-07-11 00:00:00+00:00', '2017-07-11 00:10:00+00:00',
'2017-07-11 00:20:00+00:00', '2017-07-11 00:30:00+00:00',
'2017-07-11 00:40:00+00:00', '2017-07-11 00:50:00+00:00',
'2017-07-11 01:00:00+00:00', '2017-07-11 01:10:00+00:00',
'2017-07-11 01:20:00+00:00', '2017-07-11 01:30:00+00:00',
...
'2017-07-22 22:20:00+00:00', '2017-07-22 22:30:00+00:00',
'2017-07-22 22:40:00+00:00', '2017-07-22 22:50:00+00:00',
'2017-07-22 23:00:00+00:00', '2017-07-22 23:10:00+00:00',
'2017-07-22 23:20:00+00:00', '2017-07-22 23:30:00+00:00',
'2017-07-22 23:40:00+00:00', '2017-07-22 23:50:00+00:00'],
dtype='datetime64[ns, UTC]', length=1728, freq='10T')