时间戳补齐

def func(file_path,st_time,end_time,freq='15T'):
df=pd.read_csv(file_path)
# df['date']=df['date'].apply(lambda x:x+' 00:15:00')

df['date']=pd.to_datetime(df['date'])
time=pd.DataFrame(pd.date_range(start=st_time, end=end_time,freq=freq))
time.columns=['date']
res=pd.merge(df,time,on='date',how='right')
res.fillna(method="ffill", axis=0,inplace=True)
res.fillna(method="bfill", axis=0,inplace=True)
res.to_csv(file_path.replace('.csv', '_15t.csv'),index=False)

posted @ 2022-05-31 14:25  笨笨和呆呆  阅读(67)  评论(0)    收藏  举报