Date and Time Utilities#

The pyinterp.dateutils module provides a set of utility functions for working with dates and times in NumPy arrays. These functions are designed to be fast and efficient, making it easy to perform common date and time calculations.

This example will walk you through the various functions available in the pyinterp.dateutils module.

Generating Sample Data#

First, let’s create a set of random dates that we can use to demonstrate the functionality of the dateutils module.

import datetime
import random

import numpy

import pyinterp


def make_date(samples: int = 10) -> numpy.ndarray:
    """Generate random dates."""
    epoch = datetime.datetime(1970, 1, 1)
    delta = datetime.datetime.now() - datetime.datetime(1970, 1, 1)

    pydates = [epoch + random.random() * delta for _ in range(samples)]
    npdates = numpy.array(pydates).astype("datetime64[ns]")

    return npdates


dates = make_date()
print("Sample dates:")
print(dates)
Sample dates:
['2023-07-11T19:46:01.912673000' '1974-01-03T19:00:54.771225000'
 '1983-10-07T22:43:25.025726000' '2016-09-18T22:56:36.439549000'
 '2015-11-30T03:11:03.254137000' '1999-10-21T13:38:30.780059000'
 '1995-10-14T00:28:10.608456000' '1986-02-03T00:17:02.385611000'
 '2018-07-05T23:50:27.945131000' '2005-07-30T10:11:30.103300000']

Extracting Date Components#

You can extract the date components (year, month, and day) from a NumPy array of dates using the pyinterp.dateutils.date function. This returns a structured NumPy array.

date_components = pyinterp.dateutils.date(dates)
print("Date components:")
print(date_components)
Date components:
(array([2023, 1974, 1983, 2016, 2015, 1999, 1995, 1986, 2018, 2005],
      dtype=int32), array([ 7,  1, 10,  9, 11, 10, 10,  2,  7,  7], dtype=uint8), array([11,  3,  7, 18, 30, 21, 14,  3,  5, 30], dtype=uint8))

Extracting Time Components#

Similarly, you can extract the time components (hour, minute, and second) using the pyinterp.dateutils.time function.

time_components = pyinterp.dateutils.time(dates)
print("Time components:")
print(time_components)
Time components:
(array([19, 19, 22, 22,  3, 13,  0,  0, 23, 10], dtype=uint8), array([46,  0, 43, 56, 11, 38, 28, 17, 50, 11], dtype=uint8), array([ 1, 54, 25, 36,  3, 30, 10,  2, 27, 30], dtype=uint8))

ISO Calendar Information#

The pyinterp.dateutils.isocalendar function returns the ISO calendar information (year, week number, and weekday) for each date.

iso_calendar = pyinterp.dateutils.isocalendar(dates)
print("ISO calendar:")
print(iso_calendar)
ISO calendar:
(array([2023, 1974, 1983, 2016, 2015, 1999, 1995, 1986, 2018, 2005],
      dtype=int32), array([28,  1, 40, 37, 49, 42, 41,  6, 27, 30], dtype=uint8), array([2, 4, 5, 7, 1, 4, 6, 1, 4, 6], dtype=uint8))

Weekday#

You can get the day of the week (where Sunday is 0 and Saturday is 6) using the pyinterp.dateutils.weekday function.

weekday = pyinterp.dateutils.weekday(dates)
print("Weekday (Sunday=0):")
print(weekday)
Weekday (Sunday=0):
[2 4 5 0 1 4 6 1 4 6]

Time Since January 1st#

The pyinterp.dateutils.timedelta_since_january function calculates the time difference between each date and the first day of its corresponding year.

timedelta = pyinterp.dateutils.timedelta_since_january(dates)
print("Time since January 1st:")
print(timedelta)
Time since January 1st:
[16573561912673000   241254771225000 24187405025726000 22632996439549000
 28782663254137000 25364310780059000 24712090608456000  2852222385611000
 16069827945131000 18180690103300000]

Total running time of the script: (0 minutes 0.005 seconds)

Gallery generated by Sphinx-Gallery