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:
['2011-07-28T16:13:30.226203000' '1971-01-26T22:30:33.402363000'
 '2022-09-12T18:19:45.222054000' '1977-11-17T20:34:52.137985000'
 '2022-03-07T11:09:48.606773000' '2006-06-19T22:46:18.759211000'
 '2011-10-19T19:39:53.861749000' '1998-08-09T20:18:42.876841000'
 '1991-07-27T05:49:21.624186000' '1980-06-10T01:13:54.781738000']

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([2011, 1971, 2022, 1977, 2022, 2006, 2011, 1998, 1991, 1980],
      dtype=int32), array([ 7,  1,  9, 11,  3,  6, 10,  8,  7,  6], dtype=uint8), array([28, 26, 12, 17,  7, 19, 19,  9, 27, 10], 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([16, 22, 18, 20, 11, 22, 19, 20,  5,  1], dtype=uint8), array([13, 30, 19, 34,  9, 46, 39, 18, 49, 13], dtype=uint8), array([30, 33, 45, 52, 48, 18, 53, 42, 21, 54], 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([2011, 1971, 2022, 1977, 2022, 2006, 2011, 1998, 1991, 1980],
      dtype=int32), array([30,  4, 37, 46, 10, 25, 42, 32, 30, 24], dtype=uint8), array([4, 2, 1, 4, 1, 1, 3, 7, 6, 2], 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):
[4 2 1 4 1 1 3 0 6 2]

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:
[18029610226203000  2241033402363000 22011585222054000 27722092137985000
  5656188606773000 14683578759211000 25213193861749000 19081122876841000
 17905761624186000 13914834781738000]

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

Gallery generated by Sphinx-Gallery