如何在每月的1日之前对熊猫的DatetimeIndex进行重新采样

时间:2018-12-07 01:45:53

标签: python python-3.x pandas

我有一些这样的数据

df = pd.DataFrame(index = pd.date_range('01/27/2018',periods = 290,freq = 'H'))
df['v'] = 1
In [85]: df
Out[85]:
                     v
2018-01-27 00:00:00  1
2018-01-27 01:00:00  1
2018-01-27 02:00:00  1
2018-01-27 03:00:00  1
2018-01-27 04:00:00  1
2018-01-27 05:00:00  1
2018-01-27 06:00:00  1
2018-01-27 07:00:00  1
2018-01-27 08:00:00  1
2018-01-27 09:00:00  1
2018-01-27 10:00:00  1
2018-01-27 11:00:00  1
2018-01-27 12:00:00  1
...                 ..
2018-02-07 12:00:00  1
2018-02-07 13:00:00  1
2018-02-07 14:00:00  1
2018-02-07 15:00:00  1
2018-02-07 16:00:00  1
2018-02-07 17:00:00  1
2018-02-07 18:00:00  1
2018-02-07 19:00:00  1
2018-02-07 20:00:00  1
2018-02-07 21:00:00  1
2018-02-07 22:00:00  1
2018-02-07 23:00:00  1
2018-02-08 00:00:00  1
2018-02-08 01:00:00  1

[290 rows x 1 columns]

我按月重新采样,索引显示上个月的最后一天,我希望索引显示每月的1号

In [87]: df.resample('M', how='sum')
Out[87]:
              v
2018-01-31  120
2018-02-28  170

我想要的效果是这样的,那么我该如何编写代码

Out[87]:
                  v
    2018-01-01  120
    2018-02-01  170

2 个答案:

答案 0 :(得分:1)

self

答案 1 :(得分:0)

正如@sacul在评论中提到的,请使用MS

可用选项:

B       business day frequency
C       custom business day frequency (experimental)
D       calendar day frequency
W       weekly frequency
M       month end frequency
SM      semi-month end frequency (15th and end of month)
BM      business month end frequency
CBM     custom business month end frequency
MS      month start frequency
SMS     semi-month start frequency (1st and 15th)
BMS     business month start frequency
CBMS    custom business month start frequency
Q       quarter end frequency
BQ      business quarter endfrequency
QS      quarter start frequency
BQS     business quarter start frequency
A       year end frequency
BA      business year end frequency
AS      year start frequency
BAS     business year start frequency
BH      business hour frequency
H       hourly frequency
T       minutely frequency
S       secondly frequency
L       milliseonds
U       microseconds
N       nanoseconds

参考:Offset Aliases