【发布时间】:2020-08-07 11:42:12
【问题描述】:
有 750k 行 df 和 15 列,pd.Timestamp 作为 index 称为 ts。
我以近乎实时的方式处理低至毫秒的实时数据。
现在我想将来自df_stats 中更高时间分辨率的一些统计数据作为新列应用到大df。 df_stats 的时间分辨率为 1 分钟。
$ df
+----------------+---+---------+
| ts | A | new_col |
+----------------+---+---------+
| 11:33:11.31234 | 1 | 81 |
+----------------+---+---------+
| 11:33:11.64257 | 2 | 81 |
+----------------+---+---------+
| 11:34:10.12345 | 3 | 60 |
+----------------+---+---------+
$ df_stats
+----------------+----------------+
| ts | new_col_source |
+----------------+----------------+
| 11:33:00.00000 | 81 |
+----------------+----------------+
| 11:34:00.00000 | 60 |
+----------------+----------------+
目前我有下面的代码,但效率低下,因为它需要遍历完整的数据。
我想知道使用pd.cut、bin 或pd.Grouper 是否有更简单的解决方案?或者其他什么来合并两个索引上的时间桶?
df_stats['ts_timeonly'] = df.index.map(lambda x: x.replace(second=0, microsecond=0))
df['ts_timeonly'] = df.index.map(lambda x: x.replace(second=0, microsecond=0))
df = df.merge(df_stats, on='ts_timeonly', how='left', sort=True, suffixes=['', '_hist']).set_index('ts')
【问题讨论】:
-
你试过了吗
pd.merge_asofasof
标签: pandas dataframe merge pandas-groupby pandas-apply