Skip to content

Commit

Permalink
reduced pyarrow dependency: to_numpy
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Oct 9, 2021
1 parent d6d11b6 commit 21e7155
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
9 changes: 8 additions & 1 deletion py-polars/polars/eager/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1814,7 +1814,14 @@ def to_numpy(
kwargs
kwargs will be sent to pyarrow.Array.to_numpy
"""
return self.to_arrow().to_numpy(*args, zero_copy_only=zero_copy_only, **kwargs)
if _PYARROW_AVAILABLE:
return self.to_arrow().to_numpy(
*args, zero_copy_only=zero_copy_only, **kwargs
)
else:
if self.null_count() == 0:
return self.view(ignore_nulls=True)
return self._s.to_numpy()

def to_arrow(self) -> "pa.Array":
"""
Expand Down
28 changes: 28 additions & 0 deletions py-polars/src/series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,34 @@ impl PySeries {
Ok(series.into())
}


/// Should only be called for Series with null types.
/// This will cast to floats so that `None = np.nan`
pub fn to_numpy(&self, py: Python) -> PyObject {
let s = &self.series;
if s.bit_repr_is_large() {
let s = s.cast(&DataType::Float64).unwrap();
let ca = s.f64().unwrap();
let np_arr = PyArray1::from_iter(py, ca.into_iter().map(|opt_v| {
match opt_v {
Some(v) => v,
None => f64::NAN
}
}));
np_arr.into_py(py)
} else {
let s = s.cast(&DataType::Float32).unwrap();
let ca = s.f32().unwrap();
let np_arr = PyArray1::from_iter(py, ca.into_iter().map(|opt_v| {
match opt_v {
Some(v) => v,
None => f32::NAN
}
}));
np_arr.into_py(py)
}
}

pub fn get_object(&self, index: usize) -> PyObject {
let gil = Python::acquire_gil();
let python = gil.python();
Expand Down
8 changes: 8 additions & 0 deletions py-polars/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,3 +664,11 @@ def test_bitwise():
out["and"].to_list() == [1, 0, 1]
out["or"].to_list() == [3, 6, 7]
out["xor"].to_list() == [2, 6, 6]


def test_to_numpy():
pl.eager.series._PYARROW_AVAILABLE = False
a = pl.Series("a", [1, 2, 3])
a.to_numpy() == np.array([1, 2, 3])
a = pl.Series("a", [1, 2, None])
a.to_numpy() == np.array([1.0, 2.0, np.nan])

0 comments on commit 21e7155

Please sign in to comment.