Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 以每月频率重新采样,左标签(月初)_Python_Pandas - Fatal编程技术网

Python 以每月频率重新采样,左标签(月初)

Python 以每月频率重新采样,左标签(月初),python,pandas,Python,Pandas,考虑每小时的时间序列,如: 导入numpy 进口大熊猫 数据=numpy.random.random(365*24) 索引=熊猫。日期范围('2018-01-01','2019-01-01',频率='H',关闭='左') 系列=熊猫。系列(数据,索引=索引) 看起来是这样的: 2018-01-01 00:00:00 0.823988 2018-01-01 01:00:00 0.169911 2018-01-01 02:00:00 0.359008 2018-01-01 03:

考虑每小时的时间序列,如:

导入numpy
进口大熊猫
数据=numpy.random.random(365*24)
索引=熊猫。日期范围('2018-01-01','2019-01-01',频率='H',关闭='左')
系列=熊猫。系列(数据,索引=索引)
看起来是这样的:

2018-01-01 00:00:00    0.823988
2018-01-01 01:00:00    0.169911
2018-01-01 02:00:00    0.359008
2018-01-01 03:00:00    0.873489
                         ...   
2018-12-31 20:00:00    0.898772
2018-12-31 21:00:00    0.635318
2018-12-31 22:00:00    0.061060
2018-12-31 23:00:00    0.972468
Freq: H, Length: 8760, dtype: float64
我想将此系列重新采样并求和到每月频率:

series.resample('M').sum()
但是在箱子的左边设置标签/时间戳。因此,不是:

2018-01-31    371.188835
2018-02-28    336.244967
2018-03-31    370.686715
2018-04-30    363.955540
2018-05-31    387.631062
2018-06-30    372.343839
2018-07-31    365.484547
2018-08-31    352.756428
2018-09-30    378.930171
2018-10-31    388.491260
2018-11-30    362.552504
2018-12-31    387.159189
Freq: M, dtype: float64
我想得到:

2018-01-01    371.188835
2018-02-01    336.244967
2018-03-01    370.686715
2018-04-01    363.955540
2018-05-01    387.631062
2018-06-01    372.343839
2018-07-01    365.484547
2018-08-01    352.756428
2018-09-01    378.930171
2018-10-01    388.491260
2018-11-01    362.552504
2018-12-01    387.159189
Freq: M, dtype: float64
我试过:

series.resample('M',closed='left').sum()
series.resample('M',closed='right').sum()
series.resample('M',label='left').sum()
series.resample('M',label='right').sum()
series.resample('M',closed='left',label='left').sum()
series.resample('M',closed='left',label='right').sum()
series.resample('M',closed='right',label='left').sum()
series.resample('M',closed='right',label='right').sum()
没有成功

我知道我可以:

series=series.resample('M',label='left').sum()
series.index+=pandas.DateOffset(1,'D')

但我觉得应该有更好的方法来做到这一点。

确实有更好的方法。您可以使用
'MS'
规则重新采样:

>series.resample('MS').sum()
2018-01-01    371.188835
2018-02-01    336.244967
2018-03-01    370.686715
2018-04-01    363.955540
2018-05-01    387.631062
2018-06-01    372.343839
2018-07-01    365.484547
2018-08-01    352.756428
2018-09-01    378.930171
2018-10-01    388.491260
2018-11-01    362.552504
2018-12-01    387.159189
频率:MS,数据类型:float64

请参阅。

如果一天不是那么重要,那么使用
周期索引可能会很有用。(不同的种子,因此数字不同)

默认情况下,可以使用
.to\u timestamp

res.index = res.index.to_timestamp()
print(res)

2018-01-01    376.144859
2018-02-01    353.536371
2018-03-01    365.711851
2018-04-01    364.050189
2018-05-01    371.040633
2018-06-01    360.810081
2018-07-01    378.734175
2018-08-01    360.652323
2018-09-01    360.645801
2018-10-01    360.035224
2018-11-01    356.731138
2018-12-01    369.220704
Freq: MS, dtype: float64

不完全是我想要的,但也很有用,谢谢^^
res.index = res.index.to_timestamp()
print(res)

2018-01-01    376.144859
2018-02-01    353.536371
2018-03-01    365.711851
2018-04-01    364.050189
2018-05-01    371.040633
2018-06-01    360.810081
2018-07-01    378.734175
2018-08-01    360.652323
2018-09-01    360.645801
2018-10-01    360.035224
2018-11-01    356.731138
2018-12-01    369.220704
Freq: MS, dtype: float64