- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试弄清楚如何处理周期长度不均匀的 Pandas 中的时间序列数据。我要看的第一个例子是如何计算过去 15 天的移动平均线。这是数据的示例(时间是 UTC)
index date_time data
46701 1/06/2016 19:27 15.00
46702 1/06/2016 19:28 18.25
46703 1/06/2016 19:30 16.50
46704 1/06/2016 19:33 17.20
46705 1/06/2016 19:34 18.18
我不确定我是否应该只填写数据以使其全部以 1 分钟为增量,或者是否有更聪明的方法...如果有人提出建议,我们将不胜感激
谢谢 - KC
最佳答案
你可以这样做。
bfill
(使用下一个有效值的回填),但另一种策略可能更合适,例如 ffill
(传播最后一个有效值的前向填充)。 注意:rolling
的语法已在 pandas 0.18.0 中引入.然而,在以前的版本中使用 pd.rolling_mean
可以做同样的事情。
# Test data
d = {'data': [15.0, 18.25, 16.5, 17.199999999999999, 18.18],
'date_time': ['1/06/2016 19:27',
'1/06/2016 19:28',
'1/06/2016 19:30',
'1/06/2016 19:33',
'1/06/2016 19:34'],
'index': [46701, 46702, 46703, 46704, 46705]}
df = DataFrame(d)
df['date_time'] = pd.to_datetime(df['date_time'])
# Setting the date as the index
df.set_index('date_time', inplace=True)
# Resampling data
df = df.resample('1T').bfill()
# Performing moving average
df['moving'] = df['data'].rolling(window=3, center=True).mean()
df.plot(y=['data', 'moving'])
df
data index moving
date_time
2016-01-06 19:27:00 15.00 46701 NaN
2016-01-06 19:28:00 18.25 46702 16.583333
2016-01-06 19:29:00 16.50 46703 17.083333
2016-01-06 19:30:00 16.50 46703 16.733333
2016-01-06 19:31:00 17.20 46704 16.966667
2016-01-06 19:32:00 17.20 46704 17.200000
2016-01-06 19:33:00 17.20 46704 17.526667
2016-01-06 19:34:00 18.18 46705 NaN
这是一个缺少数据的例子。
# Random data parameters
num_sample = (0, 100)
nb_sample = 1000
start_date = '2016-06-02'
freq = '2T'
random_state = np.random.RandomState(0)
# Generating random data
df = pd.DataFrame({'data': random_state.randint(num_sample[0], num_sample[1], nb_sample)},
index=random_state.choice(
pd.date_range(start=pd.to_datetime(start_date), periods=nb_sample * 3,
freq=freq),
nb_sample))
# Removing duplicate index
df = df.groupby(df.index).first()
# Removing data for closed periods
df.loc[(df.index.hour >= 22) | (df.index.hour <= 7),'data'] = np.nan
# Resampling
df = df.resample('1T').ffill()
# Moving average by hours
df['avg'] = df['data'].rolling(window=60).mean()
ax = df.plot(kind='line', subplots=True)
关于Python Pandas - 周期长度不均匀的移动平均线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37577470/
我需要在半径R的圆内生成一个均匀随机点。 我意识到,通过在区间 [0 ... 2π) 中选择均匀随机的角度,并在区间 (0 ... R) 中选择均匀随机的半径,我最终会得到更多的点朝向中心,因为对于两
我想在一个正方形内生成 N 个点(均匀地)。我怎样才能做到这一点? 最佳答案 非常酷的问题,比我想象的要困难得多,但这就是想法。有关于 n 边形的论文,但我只会做正方形。因此,圆的均匀分布是一个常见问
考虑以下示例: import itertools import numpy as np a = np.arange(0,5) b = np.arange(0,3) c = np.arange(0,7)
SQL Server 将一组值分成 5 组,每组的 sum(count) 应该均匀分布。 表仅包含 2 列 rid 和 count。 create table t1(rid int, count in
我有以下简单的 HTML。 A B C 和 CSS: ul { width: 100%; display: flex; flex-direction:
我是一名优秀的程序员,十分优秀!