将 Pandas 数据帧行移动到最近的时间步

阿瑟·奇玛

背景

我有一个由 100 个数千个值组成的大型数据框。数据帧的头部如下所示

df = pd.DataFrame([np.nan, 1100, 1400, np.nan, 14000],
                   index=pd.to_datetime(["2011-05-25 10:00:00",
                                         "2011-05-25 16:40:00",
                                         "2011-05-25 17:06:00",
                                         "2011-05-25 17:10:00",
                                         "2011-05-25 17:24:00"])

                           0
2011-05-25 10:00:00      NaN
2011-05-25 16:40:00   1100.0
2011-05-25 17:06:00   1400.0
2011-05-25 17:10:00      NaN
2011-05-25 17:24:00  14000.0

我想要的是

这些值并不总是以 6 分钟的时间步长记录。我想将未在 6 分钟时间步长记录的值移动到最近的 6 分钟步长。我希望新数据框如下所示

n_df = pd.DataFrame([np.nan, 1100, 1400, np.nan, 14000],
                   index=pd.to_datetime(["2011-05-25 10:00:00",
                                         "2011-05-25 16:42:00",
                                         "2011-05-25 17:06:00",
                                         "2011-05-25 17:12:00",
                                         "2011-05-25 17:24:00"])
                   )

                           0
2011-05-25 10:00:00      NaN
2011-05-25 16:42:00   1100.0
2011-05-25 17:06:00   1400.0
2011-05-25 17:12:00      NaN
2011-05-25 17:24:00  14000.0

对我来说重要的是 n_df 中的所有值都应该是 6 分钟的时间步长,因此属性n_df.index.freq不能是None.

我怎样才能做到这一点。

到目前为止,我for通过迭代df并找到最近的 6 分钟步骤并将值移动/复制到该步骤来使用循环来完成它,但是如果df大于 1000 ,这将非常慢

我试过的


    index = pd.date_range(df.index[0], end=df.index[-1], freq="6min")
    pydatetime_index = index.to_pydatetime()
    n_df = pd.DataFrame(columns=df.columns, index=index)

    for _idx, i in enumerate(df.index):
        nearest_neighbor = np.abs(pydatetime_index - i.to_pydatetime())
        idx = np.argmin(nearest_neighbor)
        val = df.loc[i]
        n_df.iloc[idx] = val
耶斯列

您可以使用merge_asofwithnearest并指定tolerance参数:

index = pd.date_range(df.index[0], end=df.index[-1], freq="6min")
df1 = pd.DataFrame(index=index)

df2 = pd.merge_asof(df1, 
                    df, 
                    left_index=True, 
                    right_index=True, 
                    direction='nearest', 
                    tolerance=pd.Timedelta('3Min'))
print (df2)
                           0
2011-05-25 10:00:00      NaN
2011-05-25 10:06:00      NaN
2011-05-25 10:12:00      NaN
2011-05-25 10:18:00      NaN
2011-05-25 10:24:00      NaN
                     ...
2011-05-25 17:00:00      NaN
2011-05-25 17:06:00   1400.0
2011-05-25 17:12:00      NaN
2011-05-25 17:18:00      NaN
2011-05-25 17:24:00  14000.0

[75 rows x 1 columns]

或类似DataFrame.reindex

df2 = df.reindex(index, method='nearest', tolerance=pd.Timedelta('3Min'))
print (df2)
                           0
2011-05-25 10:00:00      NaN
2011-05-25 10:06:00      NaN
2011-05-25 10:12:00      NaN
2011-05-25 10:18:00      NaN
2011-05-25 10:24:00      NaN
                     ...
2011-05-25 17:00:00      NaN
2011-05-25 17:06:00   1400.0
2011-05-25 17:12:00      NaN
2011-05-25 17:18:00      NaN
2011-05-25 17:24:00  14000.0

[75 rows x 1 columns]

或者:

df2 = df.resample('6Min').first()
print (df2)
                           0
2011-05-25 10:00:00      NaN
2011-05-25 10:06:00      NaN
2011-05-25 10:12:00      NaN
2011-05-25 10:18:00      NaN
2011-05-25 10:24:00      NaN
                     ...
2011-05-25 17:00:00      NaN
2011-05-25 17:06:00   1400.0
2011-05-25 17:12:00      NaN
2011-05-25 17:18:00      NaN
2011-05-25 17:24:00  14000.0

[75 rows x 1 columns]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将行移动到另一个数据帧时如何加速 Pandas contains

使用Pandas将数据从行移动到创建的列-Python

python pandas,转换数据集,将行移动到列

将数据从sqlalchemy移动到pandas DataFrame

根据Pandas中的索引和条件将数据从列移动到行

Python / Pandas / XML - 将 Pandas 数据帧行写回 LXML

将缺失的日期时间插入 Pandas 数据帧 Python

将 Pandas 数据帧行作为纯文本进行迭代

将数据从行移动到列 VBA

将for循环的输出写入pandas数据帧

PANDAS:将数据帧与 ID 相结合

将输出转换为 Pandas 数据帧

R将命名列移动到数据帧的末尾

Python Pandas将Na或Null值移动到新数据框

将索引值移动到pandas数据框中的列名称中

将 Pandas 数据帧中的数据转换为 keras LSTM 的时间序列训练数据

如何使用 Pandas 将文件的前 2 行移动到末尾

Pandas Dataframe:将具有相同列值的多行移动到新的特定行中

如何将时间序列数据集转换为 Pandas 数据帧

将 dict 转换为 Pandas 数据帧,将键保持在一行中

通过 Pandas 数据帧将值读入新数据帧

将时间索引添加到来自 Google Finance 的 Pandas 数据帧

将多个数据帧的列覆盖到日期时间的 Pandas 在循环中不起作用

Pandas 数据框 - 将最近的两行与条件进行比较

将 mysql 数据从行移动到数据透视表

Pandas - 将数据附加到数据帧会导致比文件源更多的行

这是在条件下将行从一个数据帧移动到另一个数据帧的正确方法吗?

根据匹配的列将行从一个数据帧移动到另一个数据帧

将数据行移动到单列,同时保留行标题