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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I taught the same, let's see if there is another workaround
@PV8 - yop, unfortunately not for strings datetimes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.