如何重新采样6个月 [英] How to resample 6 months

查看:100
本文介绍了如何重新采样6个月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在大熊猫中重新采样6个月?例如,我有2个日期'2018-02-07''2018-09-17'.我想在6个月内对它们进行重新采样,结果应为'2018-06-30''2018-12-31'.

How to resample 6 months in pandas? For example, I have 2 dates '2018-02-07' and '2018-09-17'. I want to resample them in 6 month period, the result should be '2018-06-30' and '2018-12-31'.

我设置以下代码:

import pandas as pd
import numpy as np

series = pd.Series([1,2], index=[pd.Timestamp('2018-02-07'), pd.Timestamp('2018-09-17')])
s.resample('6M').sum()

它给出'2018-02-28''2018-08-31''2019-02-28',但这不是我想要的.

It gives '2018-02-28', '2018-08-31', '2019-02-28', but this is not what I want.

推荐答案

我认为这可以帮助您实现目标:

I think this can help your purposes:

> series.resample('2Q', closed='left').last()

2018-06-30    1
2018-12-31    2
Freq: 2Q-DEC, dtype: int64

只需每2个季度重新采样一次,但将时间间隔缩小到左侧"并获取每个学期的最后一个值. 文档中有关熊猫别名的更多信息.

Just resampling every 2 Quarters, but closing the interval to the 'left' and getting the last value we have for each semester. More info about the aliases in pandas in the documentation.

这篇关于如何重新采样6个月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆