Expand description

Support datetimes and timedeltas

This module provides wrappers for NumPy’s datetime64 and timedelta64 types which are used for time keeping with with an emphasis on scientific applications. This means that while these types differentiate absolute and relative quantities, they ignore calendars (a month is always 30.44 days) and time zones. On the other hand, their flexible units enable them to support either a large range (up to 264 years) or high precision (down to 10-18 seconds).

The corresponding section of the NumPy documentation contains more information.

Example

use numpy::{datetime::{units, Datetime, Timedelta}, PyArray1};
use pyo3::Python;

Python::with_gil(|py| {
    let array = py
        .eval(
            "np.array([np.datetime64('2017-04-21')])",
            None,
            Some(locals),
        )
        .unwrap()
        .downcast::<PyArray1<Datetime<units::Days>>>()
        .unwrap();

    assert_eq!(
        array.get_owned(0).unwrap(),
        Datetime::<units::Days>::from(17_277)
    );

    let array = py
        .eval(
            "np.array([np.datetime64('2022-03-29')]) - np.array([np.datetime64('2017-04-21')])",
            None,
            Some(locals),
        )
        .unwrap()
        .downcast::<PyArray1<Timedelta<units::Days>>>()
        .unwrap();

    assert_eq!(
        array.get_owned(0).unwrap(),
        Timedelta::<units::Days>::from(1_803)
    );
});

Modules

Predefined implementors of the Unit trait

Structs

Corresponds to the datetime64 scalar type
Corresponds to the [timedelta64][scalars-datetime64] scalar type

Traits

Represents the datetime units supported by NumPy