1

Attempting to filter df to only include rows with date before 2018-11-06.

Column is in datetime format. Running this code returns only rows with exact date of 2018-11-06 instead of values less than. Also, when running code with less than symbol '<', only dates later than 2018-11-06 are returned. It appears that I am doing something very incorrectly.

db4=db3[~(db3['registration_dt']>'2018-11-06')]
5
  • Make sure you are using the correct types. Could you give the output of db3['registration_dt'].dtype? Commented Dec 4, 2021 at 21:52
  • Output is datetime64[ns]. Thank you for your assistance! Commented Dec 4, 2021 at 21:54
  • 1
    Does this answer your question? Pandas Filter date Commented Dec 4, 2021 at 21:54
  • 2
    Another possible duplicate: stackoverflow.com/questions/43344656/pandas-filter-csv-by-date Commented Dec 4, 2021 at 21:55
  • Does this answer your question? Pandas Filter CSV by Date Commented Dec 10, 2021 at 1:44

1 Answer 1

1

It seems like you are comparing the string '2018-11-06' with a datetime.

import datetime as dt

# Selects all rows where registration date is after 6 november 2018
df = db3[db3['registration_dt']>dt.datetime(2018,11,6)]


# Selects all rows where registration_dt is before 6 november 2018
df = db3[db3['registration_dt']>dt.datetime(2018,11,6)]

# The ~ symbol can be read as not
# This selects all rows before or equal to 6 november 2018
df = db3[~(db3['registration_dt']>dt.datetime(2018,11,6))]
Sign up to request clarification or add additional context in comments.

Comments

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.