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
- start- Value from where we want to start
- stop- Till which value
- num- number of intervals
coding
- 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 np.asarray(t)
Output:
Hopefully, you have learned how to create NumPy linspace out of datetime.
Leave a Reply