Creating numpy linspace out of datetime Creating numpy linspace out of datetime numpy numpy

Creating numpy linspace out of datetime


Have you considered using pandas? Using an approach from this possible duplicate question, you can make use of np.linspace in the following way

import pandas as pdstart = pd.Timestamp('2015-07-01')end = pd.Timestamp('2015-08-01')t = np.linspace(start.value, end.value, 100)t = pd.to_datetime(t)

To obtain an np.array of the linear timeseries

In [3]: np.asarray(t)Out[3]: array(['2015-06-30T17:00:00.000000000-0700',       '2015-07-01T00:30:54.545454592-0700',       '2015-07-01T08:01:49.090909184-0700',               ...       '2015-07-31T01:58:10.909090816-0700',       '2015-07-31T09:29:05.454545408-0700',       '2015-07-31T17:00:00.000000000-0700'], dtype='datetime64[ns]')


As of pandas 0.23 you can use date_range:

import pandas as pdx = pd.date_range(min(dates), max(dates), periods=500).to_pydatetime()


As far as I know, np.linspace does not support datetime objects. But perhaps we can make our own function which roughly simulates it:

def date_linspace(start, end, steps):  delta = (end - start) / steps  increments = range(0, steps) * np.array([delta]*steps)  return start + increments

This should give you an np.array with dates going from start to end in steps steps (not including the end date, can be easily modified).