0

Currently my column headers are in the format x.strftime('%b %Y') (Eg. June 2022, Jul 2022, Aug 2022 and so on....)

I want to apply the following code selecting a particular range of cols with the below code:

Df = Df.loc[:, (Df.columns >= datetime.date(2022, 2, 1)) & (Df.columns <= datetime.date(2023, 2, 28))]

However, I am getting the following error:

'>=' not supported between instances of 'str' and 'datetime.date'

How can I compare the two?

Jamiu S.
  • 5,257
  • 5
  • 12
  • 34
Saanchi
  • 69
  • 6
  • Since months are not correctly ordered when compared alphabetically, you will have to convert the headers back to date/time instances. If you need to do this kind of comparison, this is a good argument for leaving the dates as "2022-06-01" and "2022-07-01". – Tim Roberts Feb 23 '23 at 19:41
  • I actually need the dates to be in the format (%b %Y) in a prior step so as to merge the necessary tables and arrive at the resultant DF table above. Hence, I need to compare dates in format above. Any workaround? – Saanchi Feb 23 '23 at 19:43

1 Answers1

0

You first need to convert dates from str to datetime objects using pd.to_datetime() and then in Df.lo[] you convert the dates range to Timestamp objects using pd.Timestamp()

Df.columns = pd.to_datetime(Df.columns, format="%b %Y")

Df = Df.loc[:, (Df.columns >= pd.Timestamp(datetime.date(2022, 2, 1))) & 
            (Df.columns <= pd.Timestamp(datetime.date(2023, 2, 28)))]
Jamiu S.
  • 5,257
  • 5
  • 12
  • 34