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:
['2007-01-14T11:49:23.670810000' '2023-08-01T20:10:16.981672000'
 '1970-03-01T20:56:00.553275000' '1976-01-23T17:11:48.932135000'
 '2009-12-01T20:53:52.865454000' '2003-01-19T21:33:05.154018000'
 '1976-09-29T14:53:15.350174000' '2020-11-16T08:33:53.589989000'
 '2000-07-09T14:32:02.352584000' '2011-08-11T14:10:20.776977000']

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:
[(2007,  1, 14) (2023,  8,  1) (1970,  3,  1) (1976,  1, 23)
 (2009, 12,  1) (2003,  1, 19) (1976,  9, 29) (2020, 11, 16)
 (2000,  7,  9) (2011,  8, 11)]

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:
[(11, 49, 23) (20, 10, 16) (20, 56,  0) (17, 11, 48) (20, 53, 52)
 (21, 33,  5) (14, 53, 15) ( 8, 33, 53) (14, 32,  2) (14, 10, 20)]

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:
[(2007,  2, 7) (2023, 31, 2) (1970,  9, 7) (1976,  4, 5) (2009, 49, 2)
 (2003,  3, 7) (1976, 40, 3) (2020, 47, 1) (2000, 27, 7) (2011, 32, 4)]

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):
[0 2 0 5 2 0 3 1 0 4]

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:
[ 1165763670810000 18389416981672000  5172960553275000  1962708932135000
 28932832865454000  1632785154018000 23554395350174000 27678833589989000
 16468322352584000 19231820776977000]

Converting to datetime Objects#

Finally, you can convert a NumPy array of dates to an array of Python’s native datetime.datetime objects using the pyinterp.dateutils.datetime function.

datetime_objects = pyinterp.dateutils.datetime(dates)
print('Datetime objects:')
print(datetime_objects)
Datetime objects:
[datetime.datetime(2007, 1, 14, 11, 49, 23, 670810)
 datetime.datetime(2023, 8, 1, 20, 10, 16, 981672)
 datetime.datetime(1970, 3, 1, 20, 56, 0, 553275)
 datetime.datetime(1976, 1, 23, 17, 11, 48, 932135)
 datetime.datetime(2009, 12, 1, 20, 53, 52, 865454)
 datetime.datetime(2003, 1, 19, 21, 33, 5, 154018)
 datetime.datetime(1976, 9, 29, 14, 53, 15, 350174)
 datetime.datetime(2020, 11, 16, 8, 33, 53, 589989)
 datetime.datetime(2000, 7, 9, 14, 32, 2, 352584)
 datetime.datetime(2011, 8, 11, 14, 10, 20, 776977)]

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

Gallery generated by Sphinx-Gallery