[英]Two different week numbers evaluated to same date
I'm parsing weekly time series data from a source that shows dates using a year and week number. 我正在解析来自显示使用年份和周数的日期的来源的每周时间序列数据。 However, when trying to use Python's datetime.strptime
function to turn these into YYYY-MM-DD dates, two different week numbers sometimes evaluate to the same date, when I know that they should not. 但是,当尝试使用Python的datetime.strptime
函数将它们转换为YYYY-MM-DD日期时,两个不同的周数有时会计算到相同的日期,当我知道它们不应该。 The weekly data is for the week ending on a Friday. 每周数据是在星期五结束的一周。 For example: 例如:
datetime.strptime("1998-5-53", "%Y-%w-%U")
Out[43]: datetime.datetime(1999, 1, 8, 0, 0)
datetime.strptime("1999-5-01", "%Y-%w-%U")
Out[44]: datetime.datetime(1999, 1, 8, 0, 0)
The underlying data from the European Central Bank; 来自欧洲中央银行的基础数据; an example series is here . 这里有一个示例系列。
1998 did not have 53 weeks; 1998年没有53周; December 31st is week 52: 12月31日是第52周:
>>> datetime.strptime("1998-4-52", "%Y-%w-%U")
datetime.datetime(1998, 12, 31, 0, 0)
Note that the documentation for %U
states: 请注意%U
的文档说明:
Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. 一年中的周数(星期日作为一周的第一天)作为十进制数[00,53]。 All days in a new year preceding the first Sunday are considered to be in week 0. 在第一个星期日之前的新年中的所有日子都被认为是在第0周。
It could be that your data source sees the days preceding the first sunday as week 1 instead. 可能是您的数据源将第一个星期日之前的日期视为第1周。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.