w3resource

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


4. 24 Hour Datetime Objects

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.

For more Practice: Solve these Related Problems:

  • Create a numpy array of 24 datetime.datetime objects for a given day using list comprehension and verify each hour increment.
  • Generate a numpy array of np.datetime64 objects representing every hour of a specific day using np.arange with an hourly timedelta.
  • Write a function that takes a date string and returns an array of 24 datetime objects covering every hour of that day.
  • Convert the generated datetime.datetime array to np.datetime64 format and confirm that both arrays represent the same time points.

Python-Numpy Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a NumPy program to count the number of days of a given month.
Next: Write a NumPy program to find the first Monday in May 2017.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.