w3resource

NumPy: Create 24 python datetime and then put it in a numpy array


Write a NumPy program to create 24 python datetime. datetime objects (single object for every hour), and then put it in a numpy array.

Sample Solution:

Python Code:

# Importing the required libraries
import numpy as np
import datetime

# Creating a datetime object representing the start date and time (January 1, 2000)
start = datetime.datetime(2000, 1, 1)

# Generating an array of datetimes incremented by hours from the start date
# Using list comprehension to create an array of datetime objects
dt_array = np.array([start + datetime.timedelta(hours=i) for i in range(24)])

# Printing the resulting array of datetimes
print(dt_array) 

Sample Output:

[datetime.datetime(2000, 1, 1, 0, 0) datetime.datetime(2000, 1, 1, 1, 0)                                                                      
 datetime.datetime(2000, 1, 1, 2, 0) datetime.datetime(2000, 1, 1, 3, 0)                                                                      
 datetime.datetime(2000, 1, 1, 4, 0) datetime.datetime(2000, 1, 1, 5, 0)                                                                      
 datetime.datetime(2000, 1, 1, 6, 0) datetime.datetime(2000, 1, 1, 7, 0)                                                                      
 datetime.datetime(2000, 1, 1, 8, 0) datetime.datetime(2000, 1, 1, 9, 0)                                                                      
 datetime.datetime(2000, 1, 1, 10, 0) datetime.datetime(2000, 1, 1, 11, 0)                                                                    
 datetime.datetime(2000, 1, 1, 12, 0) datetime.datetime(2000, 1, 1, 13, 0)                                                                    
 datetime.datetime(2000, 1, 1, 14, 0) datetime.datetime(2000, 1, 1, 15, 0)                                                                    
 datetime.datetime(2000, 1, 1, 16, 0) datetime.datetime(2000, 1, 1, 17, 0)                                                                    
 datetime.datetime(2000, 1, 1, 18, 0) datetime.datetime(2000, 1, 1, 19, 0)                                                                    
 datetime.datetime(2000, 1, 1, 20, 0) datetime.datetime(2000, 1, 1, 21, 0)                                                                    
 datetime.datetime(2000, 1, 1, 22, 0) datetime.datetime(2000, 1, 1, 23, 0)]

Explanation:

In the above exercise –

start = datetime.datetime(2000, 1, 1) initializes a Python datetime object to represent January 1, 2000 at 00:00:00.

dt_array = np.array([start + datetime.timedelta(hours=i) for i in range(24)])

In the above code –

  • range(24) creates a list of integers from 0 to 23.
  • datetime.timedelta(hours=i) creates a timedelta object that represents a time duration of i hours.
  • [start + datetime.timedelta(hours=i) for i in range(24)] uses a list comprehension to create a list of 24 datetime objects, where each datetime object is the result of adding a timedelta of i hours to the start datetime object.
  • np.array(...) converts the list of datetime objects to a NumPy array of datetime64[D] data type. The resulting NumPy array contains 24 elements, each representing one hour between January 1, 2000 at 00:00:00 and January 1, 2000 at 23:00:00.

Python-Numpy Code Editor: