Home > Mobile >  How does datetime understand 53rd week?
How does datetime understand 53rd week?

Time:10-01

When assigning data to a specific year-week (using pandas 1.5.0), there is a surprising behavior (at least to me) around new years day and I wonder if I am doing something wrong, here is how I am computing the year and week for Jan 1 2021:

pd.to_datetime('2021-01-01 00:10:00 00:00').year

>>> 2021

pd.to_datetime('2021-01-01 00:10:00 00:00').week

>>> 53

The value of 2021 makes sense, and I could understand 53 as the 53rd week of 2020, but put together when I sort/plot the data by year-wake, this point ends up at the end of 2021. Do I need to call year/week together to make sure they return a consistent answer?

CodePudding user response:

.weekofyear and .week have been deprecated since version 1.1.0 (July 28, 2020). you can use .isocalendar().week to access the week instead.

CodePudding user response:

Adding isocalendar() to the call appears to resolve the discrepency:

pd.to_datetime('2021-01-01 00:10:00 00:00').isocalendar().year

>>> 2020

pd.to_datetime('2021-01-01 00:10:00 00:00').isocalendar().week

>>> 53

  • Related