-1

I have a dataset containing the following column:

date<BR>
2016-09-01<BR>
2016-10-05<BR>
2015-02-07<BR>
2017-02-18<BR>
2019-11-27<BR>
2020-01-24<BR>
2010-09-14<BR>

I want to select the rows from 1-10-2016 till now, and I have the following script:

df_selection=df[(df['date'] >= 2016-10-1)]<BR>
df_selection

But I get the following error with this script: Invalid comparison between dtype=datetime64[ns] and int

How can I make a correct selection? Thanks!

Michael Szczesny
  • 4,911
  • 5
  • 15
  • 32
marita
  • 143
  • 7

1 Answers1

1

You get the error because 2016-10-1 is not a date, it's a subtraction of integers, giving an integer. Use an actual date datetime.date(2016, 10, 1)

If you get your dates from strings, you can use datetime.strptime to convert it to an actual date object.

blue_note
  • 27,712
  • 9
  • 72
  • 90
  • I don't know how to use this in my script. My column ['date'] is already a dtype: datetime64[ns] – marita Oct 08 '20 at 15:05
  • your column is, but `2016-10-1` is not. write it as a `datetime.date` object – blue_note Oct 08 '20 at 15:30
  • Sorry, I am a slow learner and I just don't know how to use it in a script. How do I change the column 'date' into a workable column? – marita Oct 09 '20 at 11:48
  • the column is okay already, the problem is `2016-10-1`, use `datetime.date(2016, 10, 1)` instead. – blue_note Oct 09 '20 at 12:50