Home > other >  Add the time remaining until the end of the year to today's date
Add the time remaining until the end of the year to today's date

Time:05-18

I want to filter a data frame using a date column. The code yould return only the rows with a date less than today's date time until the year's end.

I tried the following:

df2=df[df['date'] < dt.datetime.today().strftime('%Y-%m-%d') pd.tseries.offsets.YearEnd]

CodePudding user response:

You shouldn't be turning your datetimes into strings. Also, you forgot to instantiate YearEnd.

The following will give you the year end, using today's year:

year_end = pd.Timestamp.utcnow().date()   pd.offsets.YearEnd()

You should then be able to use that for filtering, i.e.:

df[df['date'] <= year_end]
  • Related