gpt4 book ai didi

python - 如何获取两个日期之间的所有星期?

转载 作者:行者123 更新时间:2023-11-28 22:10:34 25 4
gpt4 key购买 nike

我有数据框just_dates:

0     2003-01-02
1 2015-10-31
2 2015-11-01
16 2015-11-02
33 2015-11-03
44 2015-11-04

和一个我计算的数据框weeks

df['weeks'] = pd.PeriodIndex(df.just_dates, freq='W')

看起来像:

0    2002-12-30/2003-01-05
1 2015-10-26/2015-11-01
2 2015-10-26/2015-11-01
16 2015-11-02/2015-11-08
33 2015-11-02/2015-11-08
44 2015-11-02/2015-11-08

现在我想要一个包含第一周和最后一周之间所有周的数据框,因此 all_weeks 应该如下所示:

0  2002-12-30/2003-01-05
1 2003-01-06/2003-01-12
2 2003-01-13/2003-01-19
...
2015-10-26/2015-11-01
2015-10-26/2015-11-01
2015-11-02/2015-11-08
2015-11-02/2015-11-08
2015-11-02/2015-11-08

有没有简单的方法可以做到这一点?

最佳答案

使用pandas.period_range :

pd.DataFrame(pd.period_range(start=df.weeks.min(),
end=df.weeks.max(),
freq='W'))

[输出]

                         0
0 2002-12-30/2003-01-05
1 2003-01-06/2003-01-12
2 2003-01-13/2003-01-19
3 2003-01-20/2003-01-26
4 2003-01-27/2003-02-02
5 2003-02-03/2003-02-09
.. ...
665 2015-09-28/2015-10-04
666 2015-10-05/2015-10-11
667 2015-10-12/2015-10-18
668 2015-10-19/2015-10-25
669 2015-10-26/2015-11-01
670 2015-11-02/2015-11-08

[671 rows x 1 columns]

关于python - 如何获取两个日期之间的所有星期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56528413/

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