我有两个数组:
- 3D numpy数组,形状为(1,87648,100),dtype float64
1.形状为(87648,)、类型为pandas的1D数组DatetimeIndex
3D数组沿着轴=1的值对应于1D数组中的每小时序列日期时间。总持续时间为10年,其中有2个闰年(即8760 * 8 + 8784 * 2 = 87648)。没有夏令时,因此每天正好有24个对应的值。
我想计算10年数据中一年中每小时的平均值。这意味着,在10年中,我想平均1月1日的所有0小时,1月1日的所有1小时,...,这样我在最后有8784个平均值,每个平均值都是10个数据点的平均值,除了2月29日的24小时,这些将是每个2个数据点的平均值。
为了更精确地说明,所需的结果是具有形状(1,8748,100)和dtype float64的3D数组。
让3D数组被称为“volume”,1D datetime数组被称为“datetime_array”,我不完整的最后一次尝试是在这个方向上,但我真的对这个问题感到困惑:
hour_of_year = np.array([dt.hour + (dt.dayofyear - 1) * 24 for dt in datetime_array])
volume_by_hour = np.reshape(volume, (volume.shape[0], volume.shape[1] / 24, volume.shape[2], 24))
profile = np.array([np.mean(group, axis=0) for i, group in np.ndenumerate(volume)]).reshape(???)
第一行的问题已经是它没有区分日期,所以1417到1440在一个正常的年份里对应的是3月1日,而在闰年里对应的是2月29日。
如果闰年的区别使它变得更加复杂,那么它并不那么重要,可以忽略。
1条答案
按热度按时间w7t8yxp51#
假设你使用的是
pd.DatetimeIndex
,你可能会发现pandas操作在这种情况下比只使用numpy更有用。下面是一个尝试:在这里,我使用
pd.DatetimeIndex.strftime
作为唯一标识在取平均值时要分组在一起的行的方法,但您也可以使用[df.index.month, df.index.day, df.index.hour]
输出如下所示:
你可以把它作为一个numpy数组返回,它有一个前导的单例维度: