'How to select values in pandas dataframe between dates

What I have is a pandas df that has a date column called ‘shortdate’. What I want to do is only select entries between two certain dates. Those dates are the latest date in the column and the 90 days before the latest date (Note there are multiple entries for each day).

When I do the following below, I am getting an error that says: “Can only compare identically-labeled Series objects” However I am trying to compare dates values so I don’t see the problem.

last_dr_entry = df_dr.tail(n=1) #getting the last entry in the column which is the latest date because data is already sorted
latest_date = last_dr_entry['shortdate'] #latest date
oldest_date = latest_date - timedelta(days=5) #oldest date

short_date_full_list = df_dr.tail(n=90)
short_date_date_list = short_date_full_list['shortdate']

df_between = (short_date_full_list['shortdate'] > oldest_date) & (short_date_full_list['shortdate'] <= latest_date)
print(df_between)

My python isnt the best, I know that even in what I have done above is only setting the oldest date back 5 days prior to the latest date, this is just for testing, because i dont know how to set it to 90 days before the latest day.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source