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')]
lkatiforis
  • 5,703
  • 2
  • 16
  • 35
Dustin
  • 43
  • 5
  • Make sure you are using the correct types. Could you give the output of db3['registration_dt'].dtype? – Erik Dec 04 '21 at 21:52
  • Output is datetime64[ns]. Thank you for your assistance! – Dustin Dec 04 '21 at 21:54
  • 1
    Does this answer your question? [Pandas Filter date](https://stackoverflow.com/questions/58904112/pandas-filter-date) – Alan Dec 04 '21 at 21:54
  • 2
    Another possible duplicate: https://stackoverflow.com/questions/43344656/pandas-filter-csv-by-date – Alan Dec 04 '21 at 21:55
  • Does this answer your question? [Pandas Filter CSV by Date](https://stackoverflow.com/questions/43344656/pandas-filter-csv-by-date) – Vasco Cansado Carvalho Dec 10 '21 at 01:44

1 Answers1

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))]
Erik
  • 755
  • 1
  • 5
  • 17