我有数据帧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

以及一个数据帧
df['weeks'] = pd.PeriodIndex(df.just_dates, freq='W')

weeks看起来像:
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

现在我想要一个数据框架,包含从第一周到最后一周之间的所有周,所以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]

10-07 20:24