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:
['1974-04-28T10:45:28.961318000' '2019-10-20T05:05:41.504980000'
'2023-04-17T11:35:17.533904000' '1986-10-22T12:58:34.462204000'
'1988-07-06T22:26:09.538163000' '1994-02-02T00:04:56.864393000'
'2005-10-27T19:38:47.552131000' '1982-07-09T02:36:51.095903000'
'1998-12-04T01:36:16.679774000' '1986-05-20T04:47:59.367669000']
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([1974, 2019, 2023, 1986, 1988, 1994, 2005, 1982, 1998, 1986],
dtype=int32), array([ 4, 10, 4, 10, 7, 2, 10, 7, 12, 5], dtype=uint8), array([28, 20, 17, 22, 6, 2, 27, 9, 4, 20], 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([10, 5, 11, 12, 22, 0, 19, 2, 1, 4], dtype=uint8), array([45, 5, 35, 58, 26, 4, 38, 36, 36, 47], dtype=uint8), array([28, 41, 17, 34, 9, 56, 47, 51, 16, 59], 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([1974, 2019, 2023, 1986, 1988, 1994, 2005, 1982, 1998, 1986],
dtype=int32), array([17, 42, 16, 43, 27, 5, 43, 27, 49, 21], dtype=uint8), array([7, 7, 1, 3, 3, 3, 4, 5, 5, 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 (Sunday=0):
[0 0 1 3 3 3 4 5 5 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.
Time since January 1st:
[10147528961318000 25247141504980000 9200117533904000 25448314462204000
16237569538163000 2765096864393000 25904327552131000 16339011095903000
29122576679774000 12026879367669000]
Total running time of the script: (0 minutes 0.004 seconds)