How to slice a Pandas Dataframe based on datetime index How to slice a Pandas Dataframe based on datetime index pandas pandas

How to slice a Pandas Dataframe based on datetime index


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 datetimedf2 = 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)