[英]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.