Python 如何在每月1日之前对pandas DatetimeIndex重新采样
我有一些这样的数据Python 如何在每月1日之前对pandas DatetimeIndex重新采样,python,python-3.x,pandas,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 0
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]
我按月重新取样,指数显示上月的最后一天,我希望指数显示当月的第一天
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
正如@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
参考资料:
df.resample('MS',how='sum')
MS
表示月初流量meW。谢谢你的回答。我继续搜索问题并找到了答案。它看起来不错,而且很容易选择值。谢谢你提供答案。
x=df.set_index('Date')
x.resample('MS').mean()