1

I am dropping rows based on a datetime condition, I have it working with the following line

df.drop(df[df.index.date == datetime(2017,9,14).date()].index, inplace=True)

However when I actually run the code I am not passing a datetime(2017,9,14).date() for comparison I am passing a datetime.date(2017,9,14) . So the code would look something like this...

df.drop(df[df.index.date == datetime.date(2017,9,14)].index, inplace=True)

but that obviously throws a error :

"descriptor 'date' requires a 'datetime.datetime' object but received a 'int'"

what would be the best way to fix this problem to be able to compare dates.

2 Answers 2

2

You can simplify code - select all rows if not datetime.date(2017,9,14) - so it remove rows with datetime.date(2017,9,14):

rng = pd.date_range('2017-09-13', periods=10, freq='10H')
df = pd.DataFrame({'a': range(10)}, index=rng)  
print (df)
                     a
2017-09-13 00:00:00  0
2017-09-13 10:00:00  1
2017-09-13 20:00:00  2
2017-09-14 06:00:00  3
2017-09-14 16:00:00  4
2017-09-15 02:00:00  5
2017-09-15 12:00:00  6
2017-09-15 22:00:00  7
2017-09-16 08:00:00  8
2017-09-16 18:00:00  9

import datetime
df1 = df[df.index.date != datetime.date(2017,9,14)]
print (df1)
                     a
2017-09-13 00:00:00  0
2017-09-13 10:00:00  1
2017-09-13 20:00:00  2
2017-09-15 02:00:00  5
2017-09-15 12:00:00  6
2017-09-15 22:00:00  7
2017-09-16 08:00:00  8
2017-09-16 18:00:00  9
Sign up to request clarification or add additional context in comments.

4 Comments

throws the same error : descriptor 'date' requires a 'datetime.datetime' object but received a 'int'. When looking farther into it df.index.date is a NP array and datetime.date is a object. I think this is the issue?
@blonc - What is print (df.index.dtype) ?
it is a datetime64[ns]
appreciate the link to other question. thanks for help, problem solved.
0

The error is thrown by datetime.date(2017,9,14), it has nothing to do with Pandas.

Try:

from datetime import datetime
datetime.date(2017,9,14)

Throws:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-8658ce936e92> in <module>
----> 1 datetime.date(2017,9,14)

TypeError: descriptor 'date' requires a 'datetime.datetime' object but received a 'int'

but:

import datetime
datetime.date(2017,9,14)

works and it also works in your code.

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.