从一天中不同时间测量的多年温度记录开始,我想以一个矩形数组的日平均值结束,每行代表一年的数据。在
数据看起来像这样
temperature.head()
date
1996-01-01 00:00:00 7.39
1996-01-01 03:00:00 6.60
1996-01-01 06:00:00 7.39
1996-01-01 09:00:00 9.50
1996-01-01 12:00:00 11.00
Name: temperature, dtype: float64
每日平均值
^{pr2}$它产生了
daily.head()
date
1996-01-01 9.89625
1996-01-02 10.73625
1996-01-03 6.98500
1996-01-04 5.62250
1996-01-05 8.84625
Freq: D, Name: temperature, dtype: float64
最后一部分我想
yearly_daily_mean = daily.groupby(pd.TimeGrouper(freq='12M', closed="left"))
但这里有一些问题。在
我正在使用
array_temperature = np.column_stack([group[1] for group in yearly_daily_mean if len(group[1]) == 365])
但应该有更好的方法。在
作为一个辅助问题,我如何选择年份数据的起始日期?在
下面是我该怎么做。非常简单:用你想要的形状创建一个新的df,然后用你想要的东西填充它。在
如果您不希望最后的df在第366天有任何null值,那么您可以删除final If语句,重写
columns2 = range(1,366)
,然后df2将拥有所有非null值(假设在观察的时间段内每天至少有一个度量)。在如果我没弄错,你想把你的每日平均数时间序列(你已经计算过了)重新塑造成一个矩形数据框,不同的日期作为列,不同的年份作为行。} :
这可以通过pandas重塑函数轻松实现,例如使用^{
一些虚拟数据:
首先,我添加带有年份和日期的列:
^{pr2}$现在,我们可以重塑这个数据帧:
假设您已经拥有来自高频数据的日平均值(带
pd.DateTimeIndex
),这是由于:IIUC,您需要将日平均值转换为
DataFrame
,每行有相等数量的列来捕获年度数据。你提到闰年是一个潜在的问题,当目标是相同数量的专栏。在我可以想象出两种解决方法:
row
的天数-可能是365天。为每个row
选择365个连续每日数据点的滚动块,并按index
对齐。在MM-DD
或一年中的天数对齐。在从20年半的日随机
^{pr2}$data
作为模拟日平均温度开始:以下是第一种方法的解决方案:
使用}:
.groupby(pd.TimeGrouper('365D'))
选择365个连续日的区块,并将每日平均值的每个结果groupby
对象作为pd.DataFrame
返回,每个序列的integer
index
,从0
到{要对齐21个数据块,只需转置}行中的
pd.DataFrame
,它们将按columns, with the start date of each sequence in the
索引. This operation will produce an extra
索引{integer
索引对齐。用以下方法清理:要获得
[20 x 365]
DataFrame
,请执行以下操作:如果您想简单地收集年份数据并按日期对齐,以便非闰年有一天丢失在60号左右(而不是366号),您可以:
相关问题 更多 >
编程相关推荐