Skip to content

Commit de381b6

Browse files
authored
Merge pull request #308 from PyO3/datetime
Add support for datetime64 and timedelta64 element types
2 parents 76fd0fe + 2ef2bfd commit de381b6

File tree

7 files changed

+404
-23
lines changed

7 files changed

+404
-23
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
- Add dynamic borrow checking to safely construct references into the interior of NumPy arrays. ([#274](https://github.com/PyO3/rust-numpy/pull/274))
55
- The deprecated iterator builders `NpySingleIterBuilder::{readonly,readwrite}` and `NpyMultiIterBuilder::add_{readonly,readwrite}` now take referencces to `PyReadonlyArray` and `PyReadwriteArray` instead of consuming them.
66
- The destructive `PyArray::resize` method is now unsafe if used without an instance of `PyReadwriteArray`. ([#302](https://github.com/PyO3/rust-numpy/pull/302))
7+
- Add support for `datetime64` and `timedelta64` element types via the `datetime` module. ([#308](https://github.com/PyO3/rust-numpy/pull/308))
78
- Add support for IEEE 754-2008 16-bit floating point numbers via an optional dependency on the `half` crate. ([#314](https://github.com/PyO3/rust-numpy/pull/314))
89
- The `inner`, `dot` and `einsum` functions can also return a scalar instead of a zero-dimensional array to match NumPy's types ([#285](https://github.com/PyO3/rust-numpy/pull/285))
910
- The `PyArray::resize` function supports n-dimensional contiguous arrays. ([#312](https://github.com/PyO3/rust-numpy/pull/312))
1011
- Deprecate `PyArray::from_exact_iter` after optimizing `PyArray::from_iter`. ([#292](https://github.com/PyO3/rust-numpy/pull/292))
1112
- Remove `DimensionalityError` and `TypeError` from the public API as they never used directly. ([#315](https://github.com/PyO3/rust-numpy/pull/315))
13+
- Remove the deprecated `PyArrayDescr::get_type` which was replaced by `PyArrayDescr::typeobj` in the last cycle. ([#308](https://github.com/PyO3/rust-numpy/pull/308))
1214
- Fix returning invalid slices from `PyArray::{strides,shape}` for rank zero arrays. ([#303](https://github.com/PyO3/rust-numpy/pull/303))
1315

1416
- v0.16.2

examples/simple/src/lib.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
use numpy::ndarray::{ArrayD, ArrayViewD, ArrayViewMutD};
1+
use numpy::ndarray::{ArrayD, ArrayViewD, ArrayViewMutD, Zip};
22
use numpy::{
3-
Complex64, IntoPyArray, PyArray1, PyArrayDyn, PyReadonlyArrayDyn, PyReadwriteArrayDyn,
3+
datetime::{units, Timedelta},
4+
Complex64, IntoPyArray, PyArray1, PyArrayDyn, PyReadonlyArray1, PyReadonlyArrayDyn,
5+
PyReadwriteArray1, PyReadwriteArrayDyn,
46
};
57
use pyo3::{
68
pymodule,
@@ -70,5 +72,17 @@ fn rust_ext(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
7072
x.readonly().as_array().sum()
7173
}
7274

75+
// example using timedelta64 array
76+
#[pyfn(m)]
77+
fn add_minutes_to_seconds(
78+
mut x: PyReadwriteArray1<Timedelta<units::Seconds>>,
79+
y: PyReadonlyArray1<Timedelta<units::Minutes>>,
80+
) {
81+
#[allow(deprecated)]
82+
Zip::from(x.as_array_mut())
83+
.and(y.as_array())
84+
.apply(|x, y| *x = (i64::from(*x) + 60 * i64::from(*y)).into());
85+
}
86+
7387
Ok(())
7488
}

examples/simple/tests/test_ext.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import numpy as np
2-
from rust_ext import axpy, conj, mult, extract
2+
from rust_ext import axpy, conj, mult, extract, add_minutes_to_seconds
33

44

55
def test_axpy():
@@ -24,3 +24,12 @@ def test_extract():
2424
x = np.arange(5.0)
2525
d = {"x": x}
2626
np.testing.assert_almost_equal(extract(d), 10.0)
27+
28+
29+
def test_add_minutes_to_seconds():
30+
x = np.array([10, 20, 30], dtype="timedelta64[s]")
31+
y = np.array([1, 2, 3], dtype="timedelta64[m]")
32+
33+
add_minutes_to_seconds(x, y)
34+
35+
assert np.all(x == np.array([70, 140, 210], dtype="timedelta64[s]"))

0 commit comments

Comments
 (0)