简体   繁体   中英

pandas dropping rows based on datetime condition

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM