resample函数
需求:
unique_id ds y
BE 2024/5/15 1:10 64.792
BE 2024/5/15 1:11 65.092
BE 2024/5/15 1:12 69.166
BE 2024/5/15 1:13 61.688
BE 2024/5/15 1:14 15984.668
BE 2024/5/15 1:15 7946.808
BE 2024/5/15 1:20 85.256
BE 2024/5/15 1:22 87.256
BE 2024/5/15 1:24 83.256
以上数据在分钟级别存在值缺失,想进行补全。
解决方案:
使用resample进行重采样
import pandas as pd
from datetime import datetime
import matplotlib.pyplot as plt
%matplotlib widget#处理excel数据,如果时间有缺失的话,插入一条数据
df=pd.read_csv('test1.csv')
df['ds']=pd.to_datetime(df['ds'])
df.set_index('ds', inplace=True) # 设置时间戳为索引
resampled_df = df.resample('1T').ffill()
print(resampled_df)
效果:
unique_id y
ds
2024-05-15 01:10:00 BE 64.792
2024-05-15 01:11:00 BE 65.092
2024-05-15 01:12:00 BE 69.166
2024-05-15 01:13:00 BE 61.688
2024-05-15 01:14:00 BE 15984.668
2024-05-15 01:15:00 BE 7946.808
2024-05-15 01:16:00 BE 7946.808
2024-05-15 01:17:00 BE 7946.808
2024-05-15 01:18:00 BE 7946.808
2024-05-15 01:19:00 BE 7946.808
2024-05-15 01:20:00 BE 85.256
2024-05-15 01:21:00 BE 85.256
2024-05-15 01:22:00 BE 87.256
2024-05-15 01:23:00 BE 87.256
2024-05-15 01:24:00 BE 83.256