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:
['1973-08-07T14:59:56.293246000' '1976-01-12T18:32:41.054560000'
'1979-03-15T22:33:03.822478000' '2002-05-24T14:05:54.794883000'
'2016-07-01T16:44:52.396694000' '2013-08-23T00:20:23.289158000'
'1993-03-19T00:49:44.751458000' '1999-12-22T11:42:08.064675000'
'1988-10-09T21:19:07.400945000' '1984-04-20T18:28:31.620244000']
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([1973, 1976, 1979, 2002, 2016, 2013, 1993, 1999, 1988, 1984],
dtype=int32), array([ 8, 1, 3, 5, 7, 8, 3, 12, 10, 4], dtype=uint8), array([ 7, 12, 15, 24, 1, 23, 19, 22, 9, 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([14, 18, 22, 14, 16, 0, 0, 11, 21, 18], dtype=uint8), array([59, 32, 33, 5, 44, 20, 49, 42, 19, 28], dtype=uint8), array([56, 41, 3, 54, 52, 23, 44, 8, 7, 31], 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([1973, 1976, 1979, 2002, 2016, 2013, 1993, 1999, 1988, 1984],
dtype=int32), array([32, 3, 11, 21, 26, 34, 11, 51, 40, 16], dtype=uint8), array([2, 1, 4, 5, 5, 5, 5, 3, 7, 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 (Sunday=0):
[2 1 4 5 5 5 5 3 0 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.
Time since January 1st:
[18889196293246000 1017161054560000 6388383822478000 12405954794883000
15785092396694000 20218823289158000 6655784751458000 30714128064675000
24441547400945000 9570511620244000]
Total running time of the script: (0 minutes 0.003 seconds)