У меня есть два кадра данных с датой и временем:
df["datetime"] = df[["date","time"]].apply(lambda row: ' '.join(row.values.astype(str)), axis=1)
df["datetime"] = pd.to_datetime(df["datetime"], format='%Y-%m-%d %H:%M:%S')
, а для другого:
df_labels.columns = ["start_date","start_time","end_date","end_time","mode"]
df_labels["start_datetime"] = df_labels[["start_date","start_time"]].apply(lambda row: ' '.join(row.values.astype(str)), axis=1)
df_labels["end_datetime"] = df_labels[["end_date","end_time"]].apply(lambda row: ' '.join(row.values.astype(str)), axis=1)
df_labels["start_datetime"] = df_labels["start_datetime"].str.replace("/","-")
df_labels["end_datetime"] = df_labels["end_datetime"].str.replace("/","-")
df_labels["start_datetime"] = pd.to_datetime(df_labels["start_datetime"], format='%Y-%m-%d %H:%M:%S')
df_labels["end_datetime"] = pd.to_datetime(df_labels["end_datetime"], format='%Y-%m-%d %H:%M:%S')
весь приведенный выше код успешно выполнен.
Пример df :
lat long u1 alt d date time datetime mode
0 39.921712 116.472343 0 13 39298.146204 2007-08-04 03:30:32 2007-08-04 03:30:32
1 39.921705 116.472343 0 13 39298.146215 2007-08-04 03:30:33 2007-08-04 03:30:33
2 39.921695 116.472345 0 13 39298.146227 2007-08-04 03:30:34 2007-08-04 03:30:34
3 39.921683 116.472342 0 13 39298.146238 2007-08-04 03:30:35 2007-08-04 03:30:35
4 39.921672 116.472342 0 13 39298.146250 2007-08-04 03:30:36 2007-08-04 03:30:36
Пример df_labels:
start_date start_time end_date end_time mode start_datetime end_datetime
0 2007/06/26 11:32:29 2007/06/26 11:40:29 bus 2007-06-26 11:32:29 2007-06-26 11:40:29
1 2008/03/28 14:52:54 2008/03/28 15:59:59 train 2008-03-28 14:52:54 2008-03-28 15:59:59
2 2008/03/28 16:00:00 2008/03/28 22:02:00 train 2008-03-28 16:00:00 2008-03-28 22:02:00
3 2008/03/29 01:27:50 2008/03/29 15:59:59 train 2008-03-29 01:27:50 2008-03-29 15:59:59
4 2008/03/29 16:00:00 2008/03/30 15:59:59 train 2008-03-29 16:00:00 2008-03-30 15:59:59
Однако, когда я запускаю это:
for index, row in df_labels.iterrows():
df.loc[(df["datetime"] >= row["start_datetime"]) & (df["datetime"] < row["end_datetime"])] = row["mode"]
, я получаю следующую ошибку:
TypeError: Cannot compare type 'Timestamp' with type 'str'
Пожалуйста, сообщите