Note
Go to the end to download the full example code or to run this example in your browser via Binder.
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:
['2022-01-21T05:45:43.672150000' '2013-04-18T00:39:04.965688000'
'2001-03-18T17:44:00.624996000' '1988-10-23T21:23:58.324588000'
'2026-02-12T13:48:12.551510000' '1999-10-06T18:56:04.527685000'
'1987-11-12T08:54:54.194450000' '1978-06-25T02:51:17.322249000'
'2023-01-22T14:22:41.427120000' '2016-10-31T21:51:50.178902000']
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([2022, 2013, 2001, 1988, 2026, 1999, 1987, 1978, 2023, 2016],
dtype=int32), array([ 1, 4, 3, 10, 2, 10, 11, 6, 1, 10], dtype=uint8), array([21, 18, 18, 23, 12, 6, 12, 25, 22, 31], 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([ 5, 0, 17, 21, 13, 18, 8, 2, 14, 21], dtype=uint8), array([45, 39, 44, 23, 48, 56, 54, 51, 22, 51], dtype=uint8), array([43, 4, 0, 58, 12, 4, 54, 17, 41, 50], 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([2022, 2013, 2001, 1988, 2026, 1999, 1987, 1978, 2023, 2016],
dtype=int32), array([ 3, 16, 11, 42, 7, 40, 46, 25, 3, 44], dtype=uint8), array([5, 4, 7, 7, 4, 3, 4, 7, 7, 1], 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 (Sunday=0):
[5 4 0 0 4 3 4 0 0 1]
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.
Time since January 1st:
[ 1748743672150000 9247144965688000 6630240624996000 25651438324588000
3678492551510000 24087364527685000 27248094194450000 15130277322249000
1866161427120000 26344310178902000]
Total running time of the script: (0 minutes 0.008 seconds)