gpt4 book ai didi

python - 年末或年初的年周组合

转载 作者:太空狗 更新时间:2023-10-30 02:36:16 25 4
gpt4 key购买 nike

我想根据日期构建 YYYY_WW 信息(其中 WW 是周数)。我正在为这些年的结束(开始)而苦苦挣扎,其中一周的一部分可能会落入下一年:

import datetime
import pandas as pd

# generate range of dates and extract YYYY_WW
dates_range = pd.date_range(start='2018-12-29', end='2019-01-02')
weeks = dates_range.strftime("%Y_%V")
print(weeks)

#Index(['2018_52', '2018_52', '2018_01', '2019_01', '2019_01'], dtype='object')

2018_01 显然不正确。任何关于简单解决方法的提示?

最佳答案

您正在寻找 %G 指令:

ISO 8601 year with century representing the year that contains the greater part of the ISO week (%V).

参见 strftime() and strptime() behavior了解详情。

例如:

import datetime
import pandas as pd

dates_range = pd.date_range(start='2018-12-29', end='2019-1-2')
weeks = dates_range.strftime('%G_%V')
print(weeks)
# Index(['2018_52', '2018_52', '2019_01', '2019_01', '2019_01'], dtype='object')

关于python - 年末或年初的年周组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55106916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com