Creating numpy linspace from date and time

I am writing a script that displays some data with dates on the x axis (in matplotlib). I need to create numpy.linspace from these dates in order to subsequently create a spline. Is it possible to do this?

What I tried:

 import datetime import numpy as np dates = [ datetime.datetime(2015, 7, 2, 0, 31, 41), datetime.datetime(2015, 7, 2, 1, 35), datetime.datetime(2015, 7, 2, 2, 37, 9), datetime.datetime(2015, 7, 2, 3, 59, 16), datetime.datetime(2015, 7, 2, 5, 2, 23)] x = np.linspace(min(dates), max(dates), 500) 

It throws this error:

 TypeError: unsupported operand type(s) for *: 'datetime.datetime' and 'float' 

I also tried converting datetime to np.datetime64 , but this also does not work:

 dates = [ np.datetime64(i) for i in dates ] x = np.linspace(min(dates), max(dates), 500) 

Mistake:

 TypeError: ufunc multiply cannot use operands with types dtype('<M8[us]') and dtype('float64') 
+11
python numpy matplotlib datetime
source share
5 answers

Do you consider using pandas ? Using this approach from this possible duplicate question , you can use np.linspace as follows

 import pandas as pd start = 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 get np.array linear time series

 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]') 
+10
source share

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

 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 np.array with dates from start to end in steps (not including the end date, you can easily change it).

+5
source share

Starting with panda 0.23 you can use date_range :

 import pandas as pd x = pd.date_range(min(dates), max(dates), periods=500).to_pydatetime() 
+2
source share

The last error tells us that np.datetime objects cannot propagate. Addition has been defined - you can add n timesteps to the date and get a different date. But it does not make sense to multiply the date.

 In [1238]: x=np.array([1000],dtype='datetime64[s]') In [1239]: x Out[1239]: array(['1970-01-01T00:16:40'], dtype='datetime64[s]') In [1240]: x[0]*3 ... TypeError: ufunc multiply cannot use operands with types dtype('<M8[s]') and dtype('int32') 

Thus, an easy way to create a range of datetime objects is to add a range of timestamps. Here, for example, I use 10 second increments

 In [1241]: x[0]+np.arange(0,60,10) Out[1241]: array(['1970-01-01T00:16:40', '1970-01-01T00:16:50', '1970-01-01T00:17:00', '1970-01-01T00:17:10', '1970-01-01T00:17:20', '1970-01-01T00:17:30'], dtype='datetime64[s]') 

The error in linspace is the result of her attempt to multiply the start value by 1. , as can be seen from the full error stack:

 In [1244]: np.linspace(x[0],x[-1],10) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-1244-6e50603c0c4e> in <module>() ----> 1 np.linspace(x[0],x[-1],10) /usr/lib/python3/dist-packages/numpy/core/function_base.py in linspace(start, stop, num, endpoint, retstep, dtype) 88 89 # Convert float/complex array scalars to float, gh-3504 ---> 90 start = start * 1. 91 stop = stop * 1. 92 TypeError: ufunc multiply cannot use operands with types dtype('<M8[s]') and dtype('float64') 

Despite the comment, it looks like it just converts ints to float. In any case, it was not written with datetime64 objects.

user89161's is the way to go if you want to use the linspace syntax, otherwise you can just add the increments of your size to the start date.

arange works with these dates:

 In [1256]: np.arange(x[0],x[0]+60,10) Out[1256]: array(['1970-01-01T00:16:40', '1970-01-01T00:16:50', '1970-01-01T00:17:00', '1970-01-01T00:17:10', '1970-01-01T00:17:20', '1970-01-01T00:17:30'], dtype='datetime64[s]') 
0
source share
 import numpy # 1.15 start = numpy.datetime64('2001-01-01') end = numpy.datetime64('2019-01-01') # Linspace in days: days = numpy.linspace(start.astype('f8'), end.astype('f8'), dtype='<M8[D]') # Linspace in milliseconds MS1D = 24 * 60 * 60 * 1000 daytimes = numpy.linspace(start.astype('f8') * MS1D, end.astype('f8') * MS1D, dtype='<M8[ms]') 
0
source share

All Articles