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:
['1971-07-23T03:46:31.077343000' '1981-07-06T05:14:52.427562000'
 '2016-01-04T00:00:34.208363000' '1979-12-07T08:04:16.353778000'
 '2004-09-30T12:04:08.452435000' '1982-09-03T06:01:00.050464000'
 '2010-02-16T18:05:04.448040000' '2018-10-14T19:01:34.328803000'
 '2021-08-26T23:38:43.257361000' '2006-07-07T00:44:53.801475000']

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([1971, 1981, 2016, 1979, 2004, 1982, 2010, 2018, 2021, 2006],
      dtype=int32), array([ 7,  7,  1, 12,  9,  9,  2, 10,  8,  7], dtype=uint8), array([23,  6,  4,  7, 30,  3, 16, 14, 26,  7], 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([ 3,  5,  0,  8, 12,  6, 18, 19, 23,  0], dtype=uint8), array([46, 14,  0,  4,  4,  1,  5,  1, 38, 44], dtype=uint8), array([31, 52, 34, 16,  8,  0,  4, 34, 43, 53], 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([1971, 1981, 2016, 1979, 2004, 1982, 2010, 2018, 2021, 2006],
      dtype=int32), array([29, 28,  1, 49, 40, 35,  7, 41, 34, 27], dtype=uint8), array([5, 1, 1, 5, 4, 5, 2, 7, 4, 5], 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):
[5 1 1 5 4 5 2 0 4 5]

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:
[17552791077343000 16089292427562000   259234208363000 29405056353778000
 23630648452435000 21189660050464000  4039504448040000 24778894328803000
 20561923257361000 16159493801475000]

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

Gallery generated by Sphinx-Gallery