Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading