获取 pandas 的平均年份(多年中的平均天数) [英] Get the average year (mean of days over multiple years) in Pandas

查看:62
本文介绍了获取 pandas 的平均年份(多年中的平均天数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Pandas时间序列和数据帧的新手,正在努力完成此简单任务. 从1/1/2004-12/31/2008开始,我每天都有一个数据集数据"(一维float32-Numpy数组).日期存储为datetime对象"dates"的列表. 基本上,我想计算一个完整的标准年"-所有年份中每一天的平均值(1-365). 我从这个类似的(?)问题开始(

I am new to Pandas timeseries and dataframes and struggle getting this simple task done. I have a dataset "data" (1-dimensional float32-Numpy array) for each day from 1/1/2004 - 12/31/2008. The dates are stored as a list of datetime objects "dates". Basically, I would like to calculate a complete "standard year" - the average value of each day of all years (1-365). I started from this similar (?) question (Getting the average of a certain hour on weekdays over several years in a pandas dataframe), but could not get to the desired result - a time series of 365 "average" days, e.g. the average of all four 1st of January's, 2nd of January's ...

一个小示例脚本:

import numpy as np
import pandas as pd
import datetime

startdate = datetime.datetime(2004, 1, 1)
enddate = datetime.datetime(2008, 1, 1)
days = (enddate + datetime.timedelta(days=1) - startdate).days
data = np.random.random(days)
dates = [startdate + datetime.timedelta(days=x) for x in range(0, days)]

ts = pd.Series(data, dates)
test = ts.groupby(lambda x: (x.year, x.day)).mean()

推荐答案

月份和日期(而不是年份和日期)分组:

Group by the month and day, rather than the year and day:

test = ts.groupby([ts.index.month, ts.index.day]).mean()

收益

1  1     0.499264
   2     0.449357
   3     0.498883
...
12  17    0.408180
    18    0.317682
    19    0.467238
...    
    29    0.413721
    30    0.399180
    31    0.828423
Length: 366, dtype: float64

这篇关于获取 pandas 的平均年份(多年中的平均天数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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