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, PyArrayMethods};
use pyo3::{Python, types::PyAnyMethods, ffi::c_str};
Python::with_gil(|py| {
let array = py
.eval(
c_str!("np.array([np.datetime64('2017-04-21')])"),
None,
Some(&locals),
)?
.downcast_into::<PyArray1<Datetime<units::Days>>>()?;
assert_eq!(
array.get_owned(0).unwrap(),
Datetime::<units::Days>::from(17_277)
);
let array = py
.eval(
c_str!("np.array([np.datetime64('2022-03-29')]) - np.array([np.datetime64('2017-04-21')])"),
None,
Some(&locals),
)?
.downcast_into::<PyArray1<Timedelta<units::Days>>>()?;
assert_eq!(
array.get_owned(0).unwrap(),
Timedelta::<units::Days>::from(1_803)
);
})
Modules§
Structs§
- Datetime
- Corresponds to the
datetime64
scalar type - Timedelta
- Corresponds to the [
timedelta64
][scalars-datetime64] scalar type
Traits§
- Unit
- Represents the datetime units supported by NumPy