Home > Software design >  Python cannot compare datetime and method
Python cannot compare datetime and method

Time:10-12

I tried to convert two columns to the same format, datetime in this case.

a['sale_date'] =  pd.to_datetime(a['sale_date'])
a['last_date'] =  pd.to_datetime(a['last'])
a[a.last_date>a.sale_date]

When I output the dtypes they both show up as the same:

sale_date                            datetime64[ns]
last_date                                 datetime64[ns]

But I get an error from the comparison of sale_date with last that says:
Invalid comparison between dtype=datetime64[ns] and method Does this mean they are different types? Why does this not show up when I use .dtypes? Visually the outputs look comparable.

CodePudding user response:

last is the name of an existing pandas method. So, it is better to avoid using last as a column name. If you can't avoid it, then you have to select the column using square brackets.

a = pd.DataFrame({'sale_date': pd.date_range('2018-04-09', periods=4, freq='3D'),
                  'last': pd.date_range('2018-04-12', periods=4, freq='1D')})
a[a["last"] > a.sale_date]
#    sale_date       last
# 0 2018-04-09 2018-04-12
# 1 2018-04-12 2018-04-13
  • Related