2

I have a question of comparing data of datetime64[ns] and date like '2017-01-01'.

here is the code: df.loc[(df['Date'] >= datetime.date(2017.1.1), 'TimeRange'] = '2017.1'

but , an error has been showed and said descriptor 'date' requires a 'datetime.datetime' object but received a 'int'.

how can i compare a datetime64 to data (2017-01-01 or 2-17-6-1 and likes)

Thanks

MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
potatout
  • 187
  • 1
  • 11
  • you should transform your date to have the same type to be able to compare it you can for example see this post : https://stackoverflow.com/questions/466345/converting-string-into-datetime – Dadep Jun 05 '17 at 12:46
  • Your code has a syntax error (unclosed `(` before `df['Date']`). Please post the code that results in the particular error along with the traceback. – Billy Jun 05 '17 at 12:55
  • Thank you, @Dadep. great reference. – potatout Jun 07 '17 at 03:22
  • Thank you, @Billy, i have already resolved this issue. Thank you for telling me this error. – potatout Jun 07 '17 at 03:24

2 Answers2

3

Demo:

Source DF:

In [83]: df = pd.DataFrame({'tm':pd.date_range('2000-01-01', freq='9999T', periods=20)})

In [84]: df
Out[84]:
                    tm
0  2000-01-01 00:00:00
1  2000-01-07 22:39:00
2  2000-01-14 21:18:00
3  2000-01-21 19:57:00
4  2000-01-28 18:36:00
5  2000-02-04 17:15:00
6  2000-02-11 15:54:00
7  2000-02-18 14:33:00
8  2000-02-25 13:12:00
9  2000-03-03 11:51:00
10 2000-03-10 10:30:00
11 2000-03-17 09:09:00
12 2000-03-24 07:48:00
13 2000-03-31 06:27:00
14 2000-04-07 05:06:00
15 2000-04-14 03:45:00
16 2000-04-21 02:24:00
17 2000-04-28 01:03:00
18 2000-05-04 23:42:00
19 2000-05-11 22:21:00

Filtering:

In [85]: df.loc[df.tm > '2000-03-01']
Out[85]:
                    tm
9  2000-03-03 11:51:00
10 2000-03-10 10:30:00
11 2000-03-17 09:09:00
12 2000-03-24 07:48:00
13 2000-03-31 06:27:00
14 2000-04-07 05:06:00
15 2000-04-14 03:45:00
16 2000-04-21 02:24:00
17 2000-04-28 01:03:00
18 2000-05-04 23:42:00
19 2000-05-11 22:21:00

In [86]: df.loc[df.tm > '2000-3-1']
Out[86]:
                    tm
9  2000-03-03 11:51:00
10 2000-03-10 10:30:00
11 2000-03-17 09:09:00
12 2000-03-24 07:48:00
13 2000-03-31 06:27:00
14 2000-04-07 05:06:00
15 2000-04-14 03:45:00
16 2000-04-21 02:24:00
17 2000-04-28 01:03:00
18 2000-05-04 23:42:00
19 2000-05-11 22:21:00

not standard date format:

In [87]: df.loc[df.tm > pd.to_datetime('03/01/2000')]
Out[87]:
                    tm
9  2000-03-03 11:51:00
10 2000-03-10 10:30:00
11 2000-03-17 09:09:00
12 2000-03-24 07:48:00
13 2000-03-31 06:27:00
14 2000-04-07 05:06:00
15 2000-04-14 03:45:00
16 2000-04-21 02:24:00
17 2000-04-28 01:03:00
18 2000-05-04 23:42:00
19 2000-05-11 22:21:00
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419
2

You need to ensure that the data you're comparing it with is also in the same format. Assuming that you have two datetime objects, you can do it like this:

import datetime
print(df.loc[(df['Date'] >= datetime.date(2017, 1, 1), 'TimeRange'])

This will create a datetime object and list out the filtered results. You can also assign the results an updated value as you have mentioned above.

Adeel Ahmad
  • 1,033
  • 1
  • 11
  • 24