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:
['1973-03-07T15:45:22.230973000' '1993-03-12T06:31:59.339093000'
 '1993-01-19T23:42:39.393719000' '2023-11-23T14:30:11.253347000'
 '1992-05-11T05:05:14.514620000' '2022-07-18T21:23:54.344243000'
 '1981-11-16T12:06:49.457420000' '1980-09-04T15:49:35.046091000'
 '1988-05-18T10:51:33.119363000' '1974-11-17T17:55:01.633615000']

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([1973, 1993, 1993, 2023, 1992, 2022, 1981, 1980, 1988, 1974],
      dtype=int32), array([ 3,  3,  1, 11,  5,  7, 11,  9,  5, 11], dtype=uint8), array([ 7, 12, 19, 23, 11, 18, 16,  4, 18, 17], 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([15,  6, 23, 14,  5, 21, 12, 15, 10, 17], dtype=uint8), array([45, 31, 42, 30,  5, 23,  6, 49, 51, 55], dtype=uint8), array([22, 59, 39, 11, 14, 54, 49, 35, 33,  1], 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([1973, 1993, 1993, 2023, 1992, 2022, 1981, 1980, 1988, 1974],
      dtype=int32), array([10, 10,  3, 47, 20, 29, 47, 36, 20, 46], dtype=uint8), array([3, 5, 2, 4, 1, 1, 1, 4, 3, 7], 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):
[3 5 2 4 1 1 1 4 3 0]

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:
[ 5672722230973000  6071519339093000  1640559393719000 28218611253347000
 11336714514620000 17184234344243000 27605209457420000 21397775046091000
 11962293119363000 27712501633615000]

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

Gallery generated by Sphinx-Gallery