繁体   English   中英

如何在Python中使用时间戳索引插入数据?

[英]How to interpolate data with timestamp indices in Python?

我有一个带有一列时间戳和一列值的pandas数据框,我想进行线性插值并获取不同时间戳的值。 数据框如下所示:

                  timestamp            c0
0  2014-01-01T00:00:03.500Z  38605.324219
2  2014-01-01T00:00:21.500Z  37872.890625
4  2014-01-01T00:00:39.600Z  38124.664062
6  2014-01-01T00:00:57.600Z  38185.699219
8  2014-01-01T00:01:15.700Z  38460.367188

我编写了这样的函数来提供原始数据帧并进行插值:

def interp18to9(df):
        dates = pd.date_range(pd.to_datetime(df.iloc[0]['timestamp']),
                              pd.to_datetime(df.iloc[-1]['timestamp']), freq='9S')
        new_df = pd.DataFrame()
        new_df['timestamp'] = pd.to_datetime(dates)
        new_df['c0'] = np.interp(x=dates,
                                 xp=pd.to_datetime(df.iloc[:]['timestamp']),
                                 fp=df.iloc[:]['c0'])
        return new_df

我收到一条错误消息:

TypeError: Cannot cast array data from dtype('<M8[ns]') to dtype('float64') according to the rule 'safe'

通过搜索以前的案例,我找不到此问题的解决方案,在此先感谢您。

如何使用熊猫的内部功能:

# 'floor' date to seconds
df['timestamp'] = pd.to_datetime((df['timestamp'].
                                  astype(np.int64)//10**9 * 10**9).astype('datetime64[ns]'))

# new range
new_range = pd.date_range(df.timestamp[0], df.timestamp.values[-1], freq='9S')

# resample and interpolate
df.set_index('timestamp').reindex(new_range).interpolate().reset_index()

输出:

+----+----------------------+--------------+
|    |        index         |      c0      |
+----+----------------------+--------------+
| 0  | 2014-01-01 00:00:03  | 38605.324219 |
| 1  | 2014-01-01 00:00:12  | 38239.107422 |
| 2  | 2014-01-01 00:00:21  | 37872.890625 |
| 3  | 2014-01-01 00:00:30  | 37998.777343 |
| 4  | 2014-01-01 00:00:39  | 38124.664062 |
| 5  | 2014-01-01 00:00:48  | 38155.181640 |
| 6  | 2014-01-01 00:00:57  | 38185.699219 |
| 7  | 2014-01-01 00:01:06  | 38323.033204 |
| 8  | 2014-01-01 00:01:15  | 38460.367188 |
+----+----------------------+--------------+

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM