如何通过考虑月份来创建7天的序列

时间:2018-05-23 09:51:32

标签: python pandas

我有时间序列数据。我想为每个组创建序列,这样通过考虑一个月来组合7天。即每个组应在月内。

示例数据集

a
2017-09-25
2017-09-26
2017-09-27
2017-09-28
2017-09-29
2017-09-30
2017-10-01
2017-10-02
2017-10-03
2017-10-04
2017-10-05
2017-10-06
2017-10-07
2017-10-08
2017-10-09
2017-10-10
2017-10-11
2017-10-12
2017-10-13
2017-10-14
2017-10-15
2017-10-16
2017-10-17
2017-10-18
2017-10-19
2017-10-20
2017-10-21
2017-10-22
2017-10-23
2017-10-24
2017-10-25
2017-10-26
2017-10-27
2017-10-28
2017-10-29
2017-10-30
2017-10-31
2017-11-01
2017-11-02
2017-11-03
2017-11-04
2017-11-05
2017-11-06
2017-11-07
2017-11-08
2017-11-09
2017-11-10
2017-11-11
2017-11-12
2017-11-13
2017-11-14
2017-11-15
2017-11-16
2017-11-17
2017-11-18
2017-11-19
2017-11-20
2017-11-21
2017-11-22
2017-11-23
2017-11-24
2017-11-25
2017-11-26
2017-11-27
2017-11-28
2017-11-29
2017-11-30

预期产出:

a         out
2017-09-25  0
2017-09-26  0
2017-09-27  0
2017-09-28  0
2017-09-29  0
2017-09-30  0
2017-10-01  1
2017-10-02  1
2017-10-03  1
2017-10-04  1
2017-10-05  1
2017-10-06  1
2017-10-07  1
2017-10-08  2
2017-10-09  2
2017-10-10  2
2017-10-11  2
2017-10-12  2
2017-10-13  2
2017-10-14  2
2017-10-15  3
2017-10-16  3
2017-10-17  3
2017-10-18  3
2017-10-19  3
2017-10-20  3
2017-10-21  3
2017-10-22  4
2017-10-23  4
2017-10-24  4
2017-10-25  4
2017-10-26  4
2017-10-27  4
2017-10-28  4
2017-10-29  5
2017-10-30  5
2017-10-31  5
2017-11-01  6
2017-11-02  6
2017-11-03  6
2017-11-04  6
2017-11-05  6
2017-11-06  6
2017-11-07  6
2017-11-08  7
2017-11-09  7
2017-11-10  7
2017-11-11  7
2017-11-12  7
2017-11-13  7
2017-11-14  7
2017-11-15  8
2017-11-16  8
2017-11-17  8
2017-11-18  8
2017-11-19  8
2017-11-20  8
2017-11-21  8
2017-11-22  9
2017-11-23  9
2017-11-24  9
2017-11-25  9
2017-11-26  9
2017-11-27  9
2017-11-28  9
2017-11-29  10
2017-11-30  10

到目前为止,我试过这个:

df['id_1']= df.groupby([pd.Grouper(freq='7D')]).ngroup()
df['id_2']= df.groupby([pd.Grouper(freq='W')]).ngroup()
df['id_3']= df.groupby([pd.Grouper(freq='7D'),pd.Grouper(freq='M')]).ngroup()

它给出了

a         id_1 id_2 id_3
2017-09-25  0   0   0
2017-09-26  0   0   0
2017-09-27  0   0   0
2017-09-28  0   0   0
2017-09-29  0   0   0
2017-09-30  0   0   0
2017-10-01  0   0   1
2017-10-02  1   1   2
2017-10-03  1   1   2
2017-10-04  1   1   2
2017-10-05  1   1   2
2017-10-06  1   1   2
2017-10-07  1   1   2
2017-10-08  1   1   2
2017-10-09  2   2   3
2017-10-10  2   2   3
2017-10-11  2   2   3
2017-10-12  2   2   3
2017-10-13  2   2   3
2017-10-14  2   2   3
2017-10-15  2   2   3
2017-10-16  3   3   4
2017-10-17  3   3   4
2017-10-18  3   3   4
2017-10-19  3   3   4
2017-10-20  3   3   4
2017-10-21  3   3   4
2017-10-22  3   3   4
2017-10-23  4   4   5
2017-10-24  4   4   5
2017-10-25  4   4   5
2017-10-26  4   4   5
2017-10-27  4   4   5
2017-10-28  4   4   5
2017-10-29  4   4   5
2017-10-30  5   5   6
2017-10-31  5   5   6
2017-11-01  5   5   7
2017-11-02  5   5   7
2017-11-03  5   5   7
2017-11-04  5   5   7
2017-11-05  5   5   7
2017-11-06  6   6   8
2017-11-07  6   6   8
2017-11-08  6   6   8
2017-11-09  6   6   8
2017-11-10  6   6   8
2017-11-11  6   6   8
2017-11-12  6   6   8
2017-11-13  7   7   9
2017-11-14  7   7   9
2017-11-15  7   7   9
2017-11-16  7   7   9
2017-11-17  7   7   9
2017-11-18  7   7   9
2017-11-19  7   7   9
2017-11-20  8   8   10
2017-11-21  8   8   10
2017-11-22  8   8   10
2017-11-23  8   8   10
2017-11-24  8   8   10
2017-11-25  8   8   10
2017-11-26  8   8   10
2017-11-27  9   9   11
2017-11-28  9   9   11
2017-11-29  9   9   11
2017-11-30  9   9   11

请帮我摆脱这个。

随时提出问题。如果你没有理解这个问题。

1 个答案:

答案 0 :(得分:2)

你可以GetMessage 2次:

groupby