1

also the following question is quite similiar to this question: Select DataFrame rows between two dates

I wonder how I can select the rows of a dataframe, if my dataframe column is no datetime column. The above solutions always converting the rows to a datetime column, and I don't want to do that for a later process. Right now I have the following dataframe:

columnA
01.10.2018
02.10.2018
.....

and df.dtypesis object.

My idea would be to use .loc:

df.loc[df['columnA'] >= "02.10.2019"]

which could lead to the outcome:

columnA
02.10.2018

Does this work for this object column? Or do I missing something? I really don't want to convert it to a datetime column.

PV8
  • 5,799
  • 7
  • 43
  • 87

1 Answers1

1

It cannot working for most strings formats, because strings are compared lexicographically (thanks @Jon Clements).

Only if has string in format YYYYMMDD comparing with string working correctly:

print ('02.10.2018' >= '03.10.2017')
False
print ('20181002' >= '20171003')
True

So the best is convert values to datetimes in pandas, if need processing them.

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252