I need to save datetimes to an Excel file, such that, after reading the Excel file again, I can compare them with other datetimes. Here is a minimal example:
import pandas as pd, datetime
df = pd.DataFrame({'A': [1, 2],
'B': [datetime.datetime(2000,1,1,1,2,3),
datetime.datetime(2010,1,1,1,2,3)]})
df.to_excel('test.xlsx') # save to Excel file
df2 = pd.read_excel('test.xlsx') # load from Excel file
print(df2)
t = df2.loc[df['A'] == 2, 'B'] # select the value in col B by filtering rows
L = [t, datetime.datetime(2020,1,1,1,2,3)]
print(sorted(L))
This gives the following error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
What's the standard pandas
way to retrieve the value of t
as a datetime?