I am trying to filter a dataframe based on date field.
Date Value
201810 100
201811 150
201812 95
201901 125
201902 150
201903 200
201904 225
The filtering is to be done dynamically. E.g. the first date and end date should not be 'hard coded'. So my approach is as follows:
month = pd.DataFrame(set(df['Date']),columns=['Date'])
df['Date'] = pd.to_datetime(df['Date'],format='%Y%m)
From here, I have to filter based on date and create a new dataframe. In this the last date, say 201903.
dt_first = month['Date'].head(1) <---first date is being dynamically created
dt_last = month.iloc[-2] <-- last date, dynamically created.
df_filter = df[(df.Date.ge(dt_first))&(df.Date.le(dt_last))]
But the last line is generating a blank dataframe. The resultant dataframe should look like
Date Value
201810 100
201811 150
201812 95
201901 125
201902 150
201903 200
I know I am missing out something.
Can anybody please suggest how to effectively filter the above dataframe based on condition?