How to create NumPy linspace out of datetime

Hello programmers, in this tutorial, we will learn how to create NumPy linspace out of datetime.

We can create NumPy linspace out of datetime by using the numpy.linspace() method.

numpy.linspace() method

This method returns some values between the given interval.

This method takes some parameters which are

  1. start- Value from where we want to start
  2. stop- Till which value
  3. num- number of intervals


  • 1st we import important libarries, Numpy and Pandas
  • We use the Pandas library for getting starting and ending time stamps so that we can use these two-time stamps in the numpy linspace() method.
  • After assigning starting and ending times, we use the np.linspace() method in which we pass these two starting and ending times, and we want ten date times, so we use interval 10.
  • After that, we convert that numpy linspace formate into proper datetime format so we can easily understand.
  • Now we have these datetime values in the list, and at last, we convert this into a NumPy array, so we use the np.asarray() method. This method converts the input list into an array


#importing libraries
import pandas as pd
import numpy as np
#starting time
start = pd.Timestamp('2022-07-01')
#Ending time
end = pd.Timestamp('2022-07-10')
#np.linspace() method with the interval of 10
t = np.linspace(start.value, end.value, 10)
print("linspace format:\n",t)
#Converting into datetime Index
t = pd.to_datetime(t)
print("DateTime Index:\n",t)
#Converting input into array


linspace formate:
 [1.6566336e+18 1.6567200e+18 1.6568064e+18 1.6568928e+18 1.6569792e+18
 1.6570656e+18 1.6571520e+18 1.6572384e+18 1.6573248e+18 1.6574112e+18]
DateTime Index:
 DatetimeIndex(['2022-07-01', '2022-07-02', '2022-07-03', '2022-07-04',
               '2022-07-05', '2022-07-06', '2022-07-07', '2022-07-08',
               '2022-07-09', '2022-07-10'],
              dtype='datetime64[ns]', freq=None)
array(['2022-07-01T00:00:00.000000000', '2022-07-02T00:00:00.000000000',
       '2022-07-03T00:00:00.000000000', '2022-07-04T00:00:00.000000000',
       '2022-07-05T00:00:00.000000000', '2022-07-06T00:00:00.000000000',
       '2022-07-07T00:00:00.000000000', '2022-07-08T00:00:00.000000000',
       '2022-07-09T00:00:00.000000000', '2022-07-10T00:00:00.000000000'],



Hopefully, you have learned how to create NumPy linspace out of datetime.

Leave a Reply

Your email address will not be published. Required fields are marked *