How to slice a Pandas Dataframe based on datetime index

痞子三分冷 提交于 2019-12-01 03:44:53

If you have set the "Timestamp" column as the index , then you can simply use

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

IIUC, a simple slicing?

from datetime import datetime
df2 = df[(df.Timestamp >= datetime(2009, 05, 01)) &
         (df.Timestamp <= datetime(2010, 03, 01))]

You can do something like:

df2 = df.set_index('Timestamp')['2009-05-01' :'2010-03-01'] print(df2)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!