批量读取的txt文件有缺测行,降缺测时间找出来并补为nan
import os
import pandas as pd
import numpy as np
import glob
path_root=r'E:\meteorological data\190701-091023'
date = pd.date_range('2019-07-01 01:00:00','2019-10-10 23:00:00',freq='H')
frame = []
for itime in date:
filename = 'surf_hour_' + itime.strftime('%Y%m%d%H') + '.txt'
namestr = os.path.join(path_root,filename)
# if os.path.exists(namestr):
#print(filename)
# try:
# df = pd.read_csv(namestr,sep='\s+',header=0,index_col=0)
# ser = df.loc[54399]
# except:
# ser = pd.Series([np.nan]*15, name = 54399,index=ser.index)
# else:
# ser = pd.Series([np.nan]*15, name = 54399,index=ser.index)
try:
df = pd.read_csv(namestr,sep='\s+',header=0,index_col=0)
ser = df.loc[54399]
except:
ser = pd.Series([np.nan]*15, name = 54399,index=ser.index)
frame.append(ser)
df = pd.DataFrame(frame, index =date )
网友评论