.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/utilities/ex_dateutils.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_utilities_ex_dateutils.py: .. _example_dateutils: 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. .. GENERATED FROM PYTHON SOURCE LINES 16-21 Generating Sample Data ---------------------- First, let's create a set of random dates that we can use to demonstrate the functionality of the `dateutils` module. .. GENERATED FROM PYTHON SOURCE LINES 21-44 .. code-block:: Python import datetime import random import numpy import pyinterp def make_date(samples=10): """Generates 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) .. rst-class:: sphx-glr-script-out .. code-block:: none Sample dates: ['2001-09-09T07:06:12.219069000' '2006-07-10T12:24:25.938311000' '1974-07-13T03:58:57.805171000' '1995-01-27T08:45:12.972766000' '2003-08-16T16:55:15.008058000' '1977-05-25T01:23:14.237655000' '1982-09-06T14:33:17.143426000' '2016-12-29T01:58:20.724200000' '2019-08-29T04:00:21.355532000' '1987-04-25T16:20:21.370392000'] .. GENERATED FROM PYTHON SOURCE LINES 45-52 Extracting Date Components -------------------------- You can extract the date components (year, month, and day) from a NumPy array of dates using the :py:func:`pyinterp.dateutils.date ` function. This returns a structured NumPy array. .. GENERATED FROM PYTHON SOURCE LINES 52-56 .. code-block:: Python date_components = pyinterp.dateutils.date(dates) print('\\nDate components:') print(date_components) .. rst-class:: sphx-glr-script-out .. code-block:: none \nDate components: [(2001, 9, 9) (2006, 7, 10) (1974, 7, 13) (1995, 1, 27) (2003, 8, 16) (1977, 5, 25) (1982, 9, 6) (2016, 12, 29) (2019, 8, 29) (1987, 4, 25)] .. GENERATED FROM PYTHON SOURCE LINES 57-63 Extracting Time Components -------------------------- Similarly, you can extract the time components (hour, minute, and second) using the :py:func:`pyinterp.dateutils.time ` function. .. GENERATED FROM PYTHON SOURCE LINES 63-67 .. code-block:: Python time_components = pyinterp.dateutils.time(dates) print('\\nTime components:') print(time_components) .. rst-class:: sphx-glr-script-out .. code-block:: none \nTime components: [( 7, 6, 12) (12, 24, 25) ( 3, 58, 57) ( 8, 45, 12) (16, 55, 15) ( 1, 23, 14) (14, 33, 17) ( 1, 58, 20) ( 4, 0, 21) (16, 20, 21)] .. GENERATED FROM PYTHON SOURCE LINES 68-74 ISO Calendar Information ------------------------ The :py:func:`pyinterp.dateutils.isocalendar ` function returns the ISO calendar information (year, week number, and weekday) for each date. .. GENERATED FROM PYTHON SOURCE LINES 74-78 .. code-block:: Python iso_calendar = pyinterp.dateutils.isocalendar(dates) print('\\nISO calendar:') print(iso_calendar) .. rst-class:: sphx-glr-script-out .. code-block:: none \nISO calendar: [(2001, 36, 7) (2006, 28, 1) (1974, 28, 6) (1995, 4, 5) (2003, 33, 6) (1977, 21, 3) (1982, 36, 1) (2016, 52, 4) (2019, 35, 4) (1987, 17, 6)] .. GENERATED FROM PYTHON SOURCE LINES 79-85 Weekday ------- You can get the day of the week (where Sunday is 0 and Saturday is 6) using the :py:func:`pyinterp.dateutils.weekday ` function. .. GENERATED FROM PYTHON SOURCE LINES 85-89 .. code-block:: Python weekday = pyinterp.dateutils.weekday(dates) print('\\nWeekday (Sunday=0):') print(weekday) .. rst-class:: sphx-glr-script-out .. code-block:: none \nWeekday (Sunday=0): [0 1 6 5 6 3 1 4 4 6] .. GENERATED FROM PYTHON SOURCE LINES 90-97 Time Since January 1st ---------------------- The :py:func:`pyinterp.dateutils.timedelta_since_january ` function calculates the time difference between each date and the first day of its corresponding year. .. GENERATED FROM PYTHON SOURCE LINES 97-101 .. code-block:: Python timedelta = pyinterp.dateutils.timedelta_since_january(dates) print('\\nTime since January 1st:') print(timedelta) .. rst-class:: sphx-glr-script-out .. code-block:: none \nTime since January 1st: [21711972219069000 16460665938311000 16689537805171000 2277912972766000 19673715008058000 12446594237655000 21479597143426000 31370300724200000 20750421355532000 9908421370392000] .. GENERATED FROM PYTHON SOURCE LINES 102-109 Converting to datetime Objects ------------------------------ Finally, you can convert a NumPy array of dates to an array of Python's native :py:class:`datetime.datetime` objects using the :py:func:`pyinterp.dateutils.datetime ` function. .. GENERATED FROM PYTHON SOURCE LINES 109-112 .. code-block:: Python datetime_objects = pyinterp.dateutils.datetime(dates) print('\\nDatetime objects:') print(datetime_objects) .. rst-class:: sphx-glr-script-out .. code-block:: none \nDatetime objects: [datetime.datetime(2001, 9, 9, 7, 6, 12, 219069) datetime.datetime(2006, 7, 10, 12, 24, 25, 938311) datetime.datetime(1974, 7, 13, 3, 58, 57, 805171) datetime.datetime(1995, 1, 27, 8, 45, 12, 972766) datetime.datetime(2003, 8, 16, 16, 55, 15, 8058) datetime.datetime(1977, 5, 25, 1, 23, 14, 237655) datetime.datetime(1982, 9, 6, 14, 33, 17, 143426) datetime.datetime(2016, 12, 29, 1, 58, 20, 724200) datetime.datetime(2019, 8, 29, 4, 0, 21, 355532) datetime.datetime(1987, 4, 25, 16, 20, 21, 370392)] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.002 seconds) .. _sphx_glr_download_auto_examples_utilities_ex_dateutils.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/CNES/pangeo-pyinterp/master?urlpath=lab/tree/notebooks/auto_examples/utilities/ex_dateutils.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: ex_dateutils.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: ex_dateutils.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: ex_dateutils.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_