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

Pandas' offset.yearend is returning next year

I have observed that, pandas' offsets.YearEnd is returning next year if input date is year end. Below is one such example

import pandas as pd
pd.to_datetime('2000-12-31') + pd.offsets.YearEnd() ## Timestamp('2001-12-31 00:00:00')

Is it expected behaviour? How can I force to have same year if actual date itself is year-end?

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 can explicitly specify the offset as 0 which will make sure that the calendar year does not gets incremented, See the below example,

>>> pd.to_datetime('2000-12-31') + pd.offsets.YearEnd(0)
Timestamp('2000-12-31 00:00:00')

>>> pd.to_datetime('2000-06-02') + pd.offsets.YearEnd(0)
Timestamp('2000-12-31 00:00:00')
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