Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Histogram datetime objects in Numpy

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?

like image 775
mar tin Avatar asked May 19 '15 15:05

mar tin


2 Answers

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)
like image 175
alexw Avatar answered Oct 22 '22 03:10

alexw


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)
like image 45
Mike Müller Avatar answered Oct 22 '22 02:10

Mike Müller