如何根据日期时间索引切片Pandas Dataframe

这一直困扰着我多年:

给出一个简单的pandas DataFrame

>>> df

Timestamp     Col1
2008-08-01    0.001373
2008-09-01    0.040192
2008-10-01    0.027794
2008-11-01    0.012590
2008-12-01    0.026394
2009-01-01    0.008564
2009-02-01    0.007714
2009-03-01   -0.019727
2009-04-01    0.008888
2009-05-01    0.039801
2009-06-01    0.010042
2009-07-01    0.020971
2009-08-01    0.011926
2009-09-01    0.024998
2009-10-01    0.005213
2009-11-01    0.016804
2009-12-01    0.020724
2010-01-01    0.006322
2010-02-01    0.008971
2010-03-01    0.003911
2010-04-01    0.013928
2010-05-01    0.004640
2010-06-01    0.000744
2010-07-01    0.004697
2010-08-01    0.002553
2010-09-01    0.002770
2010-10-01    0.002834
2010-11-01    0.002157
2010-12-01    0.001034

如何将其分开,以便新的DataFrame等于2009-05-01和2010-03-01之间日期的df条目

>>> df2

Timestamp     Col1
2009-05-01    0.039801
2009-06-01    0.010042
2009-07-01    0.020971
2009-08-01    0.011926
2009-09-01    0.024998
2009-10-01    0.005213
2009-11-01    0.016804
2009-12-01    0.020724
2010-01-01    0.006322
2010-02-01    0.008971
2010-03-01    0.003911

最佳答案 如果您已将“Timestamp”列设置为索引,则只需使用即可

df['2009-05-01' :'2010-03-01']
点赞