I have an array of datetime objects and I'd like to histogram them in Python.
The Numpy histogram method doesn't accept datetimes, the error thrown is
File "/usr/lib/python2.7/dist-packages/numpy/lib/function_base.py", line 176, in histogram
mn, mx = [mi+0.0 for mi in range]
TypeError: unsupported operand type(s) for +: 'datetime.datetime' and 'float'
Is there a way to perform this other than manually converting the datetime object?
The .timestamp()
method only seems to work as of Python 3.3. If you are using an older version of Python, you will need to compute it directly:
import datetime
import numpy as np
to_timestamp = np.vectorize(lambda x: (x - datetime.datetime(1970, 1, 1)).total_seconds())
from_timestamp = np.vectorize(lambda x: datetime.datetime.utcfromtimestamp(x))
## Compute the histogram
hist, bin_edges = np.histogram(to_timestamp(dates))
## Print the histogram, and convert bin edges back to datetime objects
print hist, from_timestamp(bin_edges)
numpy.histogram
works only with numbers. When dt_array
is your array of datetime
objects, this would give you the histogram:
to_timestamp = np.vectorize(lambda x: x.timestamp())
time_stamps = to_timestamp(dt_array)
np.histogram(time_stamps)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With