如果
df[:'2012-01-07']
返回的是日期在 20120107
之前的子数据帧,那么返回日期在 20120107
之后的是什么呢?df['2012-01-07':]
不会返回...df[:'2012-01-07']
返回的是日期在 20120107
之前的子数据帧,那么返回日期在 20120107
之后的是什么呢?df['2012-01-07':]
不会返回...对我来说它完美地工作,但也许在真实数据中需要通过sort_index
对索引进行排序:
df = pd.DataFrame({'a':[0,1,2,5,4]}, index=pd.date_range('2012-01-05', periods=5))
print (df)
#if need ascending sorting
df = df.sort_index()
a
2012-01-05 0
2012-01-06 1
2012-01-07 2
2012-01-08 5
2012-01-09 4
print (df[:'2012-01-07'])
a
2012-01-05 0
2012-01-06 1
2012-01-07 2
print (df['2012-01-07':])
a
2012-01-07 2
2012-01-08 5
2012-01-09 4
df = pd.DataFrame({'a':[0,1,2,5,4]}, index=pd.date_range('2012-01-05', periods=5))
#descending sorting
df = df.sort_index(ascending=False)
print (df)
a
2012-01-09 4
2012-01-08 5
2012-01-07 2
2012-01-06 1
2012-01-05 0
print (df[:'2012-01-07'])
a
2012-01-09 4
2012-01-08 5
print (df['2012-01-07':])
a
2012-01-07 2
2012-01-06 1
2012-01-05 0