pythonpandasdatetimetimestamp

Why does pd.to_datetime('2025175', format='%Y%W%w') and pd.Timestamp.fromisocalendar(2025, 17, 5) gives different output?


Why does pd.to_datetime('2025175', format='%Y%W%w') and pd.Timestamp.fromisocalendar(2025, 17, 5) gives different output?

I expected to obtain Timestamp('2025-04-25 00:00:00') for both cases. But the first approach resulted on a Friday one week ahead.

Minimum example

import pandas as pd


friday_datetime = pd.to_datetime('2025175', format='%Y%W%w')
friday_timestamp = pd.Timestamp.fromisocalendar(2025, 17, 5)
assert friday_datetime == friday_timestamp, (friday_datetime, friday_timestamp)

Output:

assert friday_datetime == friday_timestamp, (friday_datetime, friday_timestamp) 
AssertionError: (Timestamp('2025-05-02 00:00:00'), Timestamp('2025-04-25 00:00:00'))

Solution

  • There are different ways of calculating the week of the year

    For 2025 these two methods for counting weeks give different results. Ie week 1 in ISO is from 2024-12-30 to 2025-01-05 whereas week 1 following %W logic is from 2025-01-06 to 2025-01-12. Thus, also your week 17 from to_datetime is one week ahead.

    If you want pd.to_datetime to behave the same as pd.Timestamp.fromisocalendar use %V as format specifier instead of %W.

    See also the docs