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'))
There are different ways of calculating the week of the year
fromisocalendar
uses ISO standard, where week 1 is the week containing January 4th. Or in other words, the first week containing at least 4 days of the new year.
%W
uses week containing the first monday of the year as week 1. All days before this are considered to be week 0.
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