下面的代码用于重新采样长数据集并将其转换为宽格式。
'''create features for all room report'''
def create_train_data(df_room_temperature,df_room_report,time_slot,slot_num):
length_report=df_device_report.shape[0]
'''create features one by one'''
analysis_data=[]
t_bar=tqdm_notebook(range(length_report))
for i in t_bar:
'''get temperaure time serie for last months before reporting'''
time_serie_room_temperature=time_serie_table(i,-1)
if time_serie_room_temperature is None:
print(i)
continue
'''get feature for the report result'''
features=resample_room_temperature_minutes(time_serie_room_temperature,time_slot,slot_num,55)
'''get the report result as label'''
label_result=df_room_report.at[df_room_report.index[i],test_item_title]
room_name_list=df_room_report.at[df_room_report.index[i],'room_name']
sampledate=df_room_report.at[df_room_report.index[i],'Sample(s) Collected']
'''combine result and features'''
features=np.append(features,[label_result])
features=np.append(features,[room_name_list])
features=np.append(features,[sampledate])
analysis_data.append(features)
'''create a dataframe for the features and result'''
columns_name=[]
for i in range(slot_num):
txt='f'+str(i)
columns_name.append(txt)
# columns_name.append('test_time_title')
columns_name.append('Result')
columns_name.append('room_name')
columns_name.append('Sample(s) Collected')
df_analysis=pd.DataFrame(data=analysis_data,columns=columns_name)
print(df_analysis)
return df_analysis,analysis_data
analysis_data_table,analysis_data_list=create_train_data(df_room_temperature,df_room_report,10,10080)
返回错误:
---------------------------------------------------------------------------
UFuncTypeError Traceback (most recent call last)
<ipython-input-48-1ff657b3fdf3> in <module>()
41 # print(df_analysis)
42 return df_analysis,analysis_data
---> 43 analysis_data_table,analysis_data_list=create_train_data(df_room_temperature,df_room_report,10,10080)
2 frames
<ipython-input-15-aa8371ad497b> in add_time(d, rd)
3 day = relativedelta(days=+1)
4
----> 5 out = d + rd
6 if d.day > out.day:
7 out = out + day
UFuncTypeError: ufunc 'add' cannot use operands with types dtype('<M8[ns]') and dtype('O')
在阅读other questions时,我认为这可能是由输入数据的类型引起的,因此我检查了df_room_temperature
和df_room_report
的类型。看来它们都是dtype('<M8[ns]')
类型:
df_room_temperature['Timestamp'].dtype # dtype('<M8[ns]')
df_room_report['Sample(s) Collected'].dtype # dtype('<M8[ns]')
我还尝试了如下所示的转换为datetime
的方法,但这无济于事:
df_room_temperature['Timestamp'] = pd.to_datetime(df_room_temperature['Timestamp'], errors='coerce')
df_room_report['Sample(s) Collected'] = pd.to_datetime(df_room_report['Sample(s) Collected'], errors='coerce')
此错误是什么意思,可能在哪里出错?谢谢