Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare list of datetimes with datetime in Python

Tags:

python

numpy

I have a list of datetime objects and would like to find the ones which are within a certain time frame:

import datetime

dates = [ datetime.datetime(2007, 1, 2, 0, 1),
          datetime.datetime(2007, 1, 3, 0, 2),
          datetime.datetime(2007, 1, 4, 0, 3),
          datetime.datetime(2007, 1, 5, 0, 4),
          datetime.datetime(2007, 1, 6, 0, 5),
          datetime.datetime(2007, 1, 7, 0, 6) ]
#in reality this is a list of over 25000 dates

mask = (dates>datetime.datetime(2007,1,3)) & \
       (dates<datetime.datetime(2007,1,6))

However, this results in the following error: "TypeError: can't compare datetime.datetime to list"

How can I fix my code?

like image 379
HyperCube Avatar asked Jan 03 '13 16:01

HyperCube


People also ask

Can you compare Datetimes in Python?

When you have two datetime objects, the date and time one of them represent could be earlier or latest than that of other, or equal. To compare datetime objects, you can use comparison operators like greater than, less than or equal to. Like any other comparison operation, a boolean value is returned.

Can you compare Datetimes?

The DateTime. Compare() method in C# is used for comparison of two DateTime instances. It returns an integer value, <0 − If date1 is earlier than date2.

How do I find the difference between two Datetimes in Python?

timedelta() method. To find the difference between two dates in Python, one can use the timedelta class which is present in the datetime library. The timedelta class stores the difference between two datetime objects.

How do you compare a timestamp in Python?

Comparison between pandas timestamp objects is carried out using simple comparison operators: >, <,==,< = , >=. The difference can be calculated using a simple '–' operator. Given time can be converted to pandas timestamp using pandas. Timestamp() method.


2 Answers

You can mask a numpy.array in the syntax you describe (but not a list):

import numpy as np

date1 = np.array(dates)
mask = (dates1 > datetime.datetime(2007,1,3)) & \
       (dates1 < datetime.datetime(2007,1,6))

In [14]: mask
Out[14]: array([False,  True,  True,  True, False, False], dtype=bool)

In [15]: dates1[mask]
Out[15]: array([2007-01-03 00:02:00, 2007-01-04 00:03:00, 2007-01-05 00:04:00], dtype=object)

(since this question is tagged numpy, presumably this is what you were intending.)

like image 73
Andy Hayden Avatar answered Sep 24 '22 03:09

Andy Hayden


If your dates list is in sorted order, you can use the bisect module:

>>> import bisect
>>> bisect.bisect_right(dates, datetime.datetime(2007,1,3))
1
>>> bisect.bisect_left(dates, datetime.datetime(2007,1,6))
4

The .bisect_* functions return indices into the dates list:

>>> lower = bisect.bisect_right(dates, datetime.datetime(2007,1,3))
>>> upper = bisect.bisect_left(dates, datetime.datetime(2007,1,6))
>>> mask = dates[lower:upper]
>>> mask
[datetime.datetime(2007, 1, 3, 0, 2), datetime.datetime(2007, 1, 4, 0, 3), datetime.datetime(2007, 1, 5, 0, 4)]
like image 29
Martijn Pieters Avatar answered Sep 23 '22 03:09

Martijn Pieters