由于我是编程,Python和Pandas的新手,所以我在此道歉。我试图基于日期时间索引并使用外部联接按步骤合并五个数据框。该数据是我在以下链接中附加的水位测量站数据:https://drive.google.com/open?id=15t7wkU0Sl17WgIS6CAgJpkWONdppdhvH。数据来自各种加拿大和美国的水位测量站系统,并且除了在链接中的美国数据必须通过简单地减去站的高程而减少到一个基准点外,其他数据都非常相似。我已经将美国电视台添加到了自己的模块中以处理该处理。合并工作正常,但合并处理的美国数据时会引发以下错误:
ValueError: can not merge DataFrame with instance of type <type 'int'>
有人可以给我一些有关此错误的见解吗?
我已经在数据帧中搜索了整数,并尝试将其转换为字符串或数据帧,但是合并起来仍然没有运气。
def readNOAA():
noaaDFs = {}
filenames = glob.glob(r"C:\Users\Andrew\Documents\CHS\SeasonalGaugeAnalysis\Input\CO-OPS_*_wl.csv")
dataframes = [pd.read_csv(filename) for filename in filenames]
for df, filename in zip(dataframes, filenames):
df['filename'] = os.path.basename(filename)
df['Station ID'] = df['filename'].str[7:14]
del df['filename']
combined_df = pd.concat(dataframes, ignore_index=True)
df = pd.DataFrame.from_records(combined_df)
df['Date Time (UTC)'] = pd.to_datetime(df['Date'] + ' ' + df['Time (GMT)'])
#df['Date Time (UTC)'] = df['Date Time (UTC)'].tz_localize('utc')
df.set_index('Date Time (UTC)', inplace=True)
df.index = df.index.to_datetime().tz_localize('utc')
df['WL'] = df['Preliminary (m)'].where(df['Verified (m)'].isnull(), df['Preliminary (m)'])
df['WL'] = df['WL'].replace({'-': np.nan})
df.drop(['Date', 'Time (GMT)', 'Predicted (m)', 'Preliminary (m)', 'Verified (m)'], axis=1, inplace=True)
Station3 = df[df['Station ID'].str.match('9052030')] #Oswego, NY
#Station3['Temp Station ID'] = '13771'
Station3['WL'] = Station3['WL'].astype(float) - 74.2
Station3.rename(columns={'WL':'9052030'}, inplace=True)
del Station3['Station ID']
noaaDFs["9052030"]=9052030
hydrometDF = readHydromet()
pcwlDF = readPCWL()
noaaDF = readNOAA()
# Create 3 minute time series to merge station data to so data gaps can be identified
idx = pd.date_range('06-21-2019 20:00:00', periods=25000, freq='3 min')
ts = pd.Series(range(len(idx)), index=idx)
cts = ts.to_frame()
cts.index = cts.index.to_datetime().tz_localize('utc')
cts.drop(0, axis=1, inplace=True)
final13771 = pd.merge(hydrometDF["Station11985"], pcwlDF["station13988"], how='outer', left_index=True,right_index=True)
final13771 = pd.merge(final13771, pcwlDF["station13590"], how='outer', left_index=True,right_index=True)
final13771 = pd.merge(final13771, pcwlDF["station14400"], how='outer', left_index=True,right_index=True)
final13771 = pd.merge(final13771, noaaDF["9052030"], how='outer', left_index=True,right_index=True)
final13771 = pd.merge(final13771, cts, how='outer', left_index=True,right_index=True)
final13771.to_excel("13771.xlsx")