Skip to content

Commit

Permalink
numpy v2: fixed segfault during conversion from datetime64 array
Browse files Browse the repository at this point in the history
  • Loading branch information
Anexen committed Jul 25, 2024
1 parent 8526645 commit 9353db2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 23 deletions.
15 changes: 9 additions & 6 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,24 @@ jobs:
runs-on: "ubuntu-22.04"
steps:
- uses: actions/checkout@v4
- name: Install dev-requirements
run: pip install -r dev-requirements.txt
- uses: actions/cache@v4
with:
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Test
run: cargo test --release
- name: Test no numpy installed works
- name: Test no numpy
run: |
pip uninstall numpy -y
cargo test --release --features nonumpy
- name: Install numpy v1
run: pip install 'numpy>=1,<2' 'pandas>=1,<2'
- name: Test numpy v1
run: cargo test --release
- name: Install numpy v2
run: pip install 'numpy>=2,<3' 'pandas>=2,<3'
- name: Test numpy v2
run: cargo test --release

build:
if: ${{ startsWith(github.ref, 'refs/tags/') || github.event_name == 'workflow_dispatch' }}
Expand Down
2 changes: 1 addition & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
maturin==0.14.*
maturin==1.4.*
numpy==1.*
pandas==1.*
numpy-financial==1.*
21 changes: 5 additions & 16 deletions src/conversions.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::str::FromStr;

use numpy::{
datetime::{units, Datetime as datetime64},
PyArray1,
};
use numpy::PyArray1;
use pyo3::{
exceptions::{PyTypeError, PyValueError},
prelude::*,
Expand Down Expand Up @@ -101,15 +98,6 @@ impl From<&PyDate> for DateLike {
}
}

impl From<&datetime64<units::Days>> for DateLike {
fn from(value: &datetime64<units::Days>) -> Self {
let days_since_unix_epoch: i32 = Into::<i64>::into(*value) as i32;
let date = Date::from_julian_day(UNIX_EPOCH_JULIAN_DAY + days_since_unix_epoch).unwrap();

date.into()
}
}

impl<'s> FromPyObject<'s> for DateLike {
fn extract(obj: &'s PyAny) -> PyResult<Self> {
if let Ok(py_date) = obj.downcast::<PyDate>() {
Expand All @@ -126,7 +114,7 @@ impl<'s> FromPyObject<'s> for DateLike {
match obj.get_type().name()? {
"datetime64" => Ok(obj
.call_method1("astype", ("datetime64[D]",))?
.call_method1("astype", ("int",))?
.call_method1("astype", ("int32",))?
.extract::<DaysSinceUnixEpoch>()?
.into()),

Expand All @@ -150,11 +138,12 @@ where
fn extract_date_series_from_numpy(series: &PyAny) -> PyResult<Vec<DateLike>> {
Ok(series
.call_method1("astype", ("datetime64[D]",))?
.downcast::<PyArray1<datetime64<units::Days>>>()?
.call_method1("astype", ("int32",))?
.downcast::<PyArray1<i32>>()?
.readonly()
.as_slice()?
.iter()
.map(|x| x.into())
.map(|&x| DateLike::from(DaysSinceUnixEpoch(x)))
.collect())
}

Expand Down

0 comments on commit 9353db2

Please sign in to comment.