to_datetime give me wrong week number

58 views
Skip to first unread message

Ali Beyram

unread,
Mar 10, 2017, 5:29:32 PM3/10/17
to PyData
Hi,

pd.to_datetime('2017-1-1').week would return 52.

I also checked '2016-1-1' it would give me 53.

I am not sure why or how to fix it.

Best,
Ali


Christopher Aycock

unread,
Mar 13, 2017, 10:25:46 AM3/13/17
to PyData
I assume it's because of


Week 1 is defined as the first Monday of the year, not just the first day.

Ali Beyram

unread,
Mar 13, 2017, 12:34:50 PM3/13/17
to PyData
It makes sense now thank you.

chris bertinato

unread,
Mar 13, 2017, 12:34:52 PM3/13/17
to PyData
This is not a bug.  The week number returned is according to ISO 8601 which starts on Mondays and the first week of the year is defined as the one containing the first Thursday of the year.

January 1, 2016 falls on a Friday, so the first week in 2016 starts on January 4.

January 1, 2017 falls on a Sunday, so the first week in 2016 starts on January 2.


CB

On Friday, March 10, 2017 at 5:29:32 PM UTC-5, Ali Beyram wrote:
Reply all
Reply to author
Forward
0 new messages