【问题标题】:How can I group a time series according to the seasons using pandas?如何使用 pandas 根据季节对时间序列进行分组?
【发布时间】:2020-11-17 06:56:55
【问题描述】:

我有一个来自 MODIS/Aqua 传感器的每日海表温度时间序列,从 2002 年开始到 2020 年:

data['Image_Datetime'] = pd.to_datetime(data['Image_Datetime'], format='%Y-%m-%d %H:%M:%S')
data = data.set_index('Image_Datetime')
data.head()

我想将与夏季(12 月、1 月和 2 月)对应的所有数据分组,这样我就可以只分析单个季节的温度变化。我知道这应该或多或少简单,但我一直在寻找解决方案,但仅使用 pandas 似乎都不是很直观。

【问题讨论】:

    标签: python pandas time-series


    【解决方案1】:

    您必须住在南半球才能在 12 月举行夏季。

    试试这个:

    season = data['Image_Datetime'].dt.month.map(lambda x: 'Summer' if x in [12, 1, 2] else 'Winter')
    
    # Mean temperature by season
    data.group_by(season)['Temperature'].mean()
    

    【讨论】:

    • 您好,代码,感谢您的帮助。是的,我住在南半球 :) 不幸的是,当我尝试您的建议时,我收到一条与我的 DatetimeIndex "Image_Datetime" 相关的 KeyError 消息
    • 如果你在set_index后面加上一行,使用season = data.index.dt.month....
    • 使用 season = data.index.dt.month 然后我得到一个 AttributeError: 'DatetimeIndex' object has no attribute 'dt'。也尝试在设置索引前申请,但错误依旧
    猜你喜欢
    • 2014-12-15
    • 2014-08-20
    • 1970-01-01
    • 2019-11-18
    • 2017-10-09
    • 2018-04-13
    • 2014-05-06
    • 1970-01-01
    • 2016-07-02
    相关资源
    最近更新 更多