我在 pandas dataframe 中有温度和辐射的时间序列.时间分辨率为 1 分钟,以常规步长.
I have a times series with temperature and radiation in a pandas dataframe. The time resolution is 1 minute in regular steps.
import datetime
import pandas as pd
import numpy as np
date_times = pd.date_range(datetime.datetime(2012, 4, 5, 8, 0),
datetime.datetime(2012, 4, 5, 12, 0),
freq='1min')
tamb = np.random.sample(date_times.size) * 10.0
radiation = np.random.sample(date_times.size) * 10.0
frame = pd.DataFrame(data={'tamb': tamb, 'radiation': radiation},
index=date_times)
frame
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 241 entries, 2012-04-05 08:00:00 to 2012-04-05 12:00:00
Freq: T
Data columns:
radiation 241 non-null values
tamb 241 non-null values
dtypes: float64(2)
如何将此 dataframe 下采样到一小时的分辨率,计算温度的每小时 mean 和每小时 sum辐射?
How can I down-sample this dataframe to a resolution of one hour, computing the hourly mean for the temperature and the hourly sum for radiation?
在 pandas 0.18 中,重采样 API 发生了变化(参见 文档).所以对于 pandas >= 0.18 的答案是:
With pandas 0.18 the resample API changed (see the docs). So for pandas >= 0.18 the answer is:
In [31]: frame.resample('1H').agg({'radiation': np.sum, 'tamb': np.mean})
Out[31]:
tamb radiation
2012-04-05 08:00:00 5.161235 279.507182
2012-04-05 09:00:00 4.968145 290.941073
2012-04-05 10:00:00 4.478531 317.678285
2012-04-05 11:00:00 4.706206 335.258633
2012-04-05 12:00:00 2.457873 8.655838
<小时>
旧答案:
Old Answer:
我正在回答我的问题以反映 pandas >= 0.8 中与时间序列相关的变化(所有其他答案都已过时).
I am answering my question to reflect the time series related changes in pandas >= 0.8 (all other answers are outdated).
使用 pandas >= 0.8 答案是:
Using pandas >= 0.8 the answer is:
In [30]: frame.resample('1H', how={'radiation': np.sum, 'tamb': np.mean})
Out[30]:
tamb radiation
2012-04-05 08:00:00 5.161235 279.507182
2012-04-05 09:00:00 4.968145 290.941073
2012-04-05 10:00:00 4.478531 317.678285
2012-04-05 11:00:00 4.706206 335.258633
2012-04-05 12:00:00 2.457873 8.655838
这篇关于如何重新采样具有应用于每列的不同函数的数据框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何在 conda 环境中安装 Selenium?How to install Selenium in a conda environment?(如何在 conda 环境中安装 Selenium?)
使用 Anaconda installe 在 Windows 上获取 CUDA 和 CUDNNget the CUDA and CUDNN version on windows with Anaconda installe(使用 Anaconda installe 在 Windows 上获取 CUDA 和 CUDNN 版本)
如何下载适用于 python 3.6 的 AnacondaHow can I download Anaconda for python 3.6(如何下载适用于 python 3.6 的 Anaconda)
使用两个不同的 Python 发行版Using two different Python Distributions(使用两个不同的 Python 发行版)
除了 OSX 上现有的 pyenv 安装之外,如何安装 AnaHow can I install Anaconda aside an existing pyenv installation on OSX?(除了 OSX 上现有的 pyenv 安装之外,如何安装 Anaconda?)
在 Cygwin 中为 Anaconda 永久设置 Python 路径Permanently set Python path for Anaconda within Cygwin(在 Cygwin 中为 Anaconda 永久设置 Python 路径)