添加数据,满足所有的 时间序列,以nan值为插入函数

import numpy as np
import pandas as pd
import datetime
import re
import time
import glob
import copy

def buchong_shuju(path_in):
time_parse = lambda date: datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
df = pd.read_csv(path_in, encoding='utf-8', parse_dates=['date'],
date_parser=time_parse)
c= pd.read_csv(r'.\nan值补充.csv')
d = []
a = np.array(c['hb'])

helper = pd.DataFrame({'date': pd.date_range(df['date'].min(), df['date'].max(), freq='H')})
df1 = pd.merge(df,helper,on = 'date',how='outer').sort_values('date')

op1 = df1.groupby(df1['date'])
for date,op2 in op1:
# print(len(op2))

if len(op2)==1:
s = op2['date']

op2 =copy.deepcopy(c)
op2['date'] = op2['date'].apply(lambda x:s)
# print(op2)
# d.append(op2)
# s =d
# print(s)
op2 = op2.sort_values('hb')
d.append(op2.sort_values('hb'))
print(d)

return d
def save_weniian(path_in,path_out):
with open(path_in,'r')as f:
df = pd.DataFrame()
df = df.append(buchong_shuju(path_in))
# print(df)
df.to_csv(path_out,index=False)
print('Finished ' + path_in)
if __name__ == "__main__":
ff = glob.glob("*_日t.csv")
if not ff:
print('No files to convert.')
exit()
else:
print("Detected ASCII *日_.csv files: \n", "\n".join(ff))
for f in ff:
path_in = f
path_out = re.sub(r'(?i)t.csv', 't_1.csv', f)
s = buchong_shuju(f)
d = save_weniian(path_in, path_out)



posted @ 2020-09-12 13:16  云飞01  阅读(212)  评论(0编辑  收藏  举报