Skip to content

Commit

Permalink
python: to_numpy use first type as supertype (#4116)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Jul 22, 2022
1 parent 91f0ae5 commit d394f83
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
10 changes: 8 additions & 2 deletions py-polars/src/dataframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,11 +524,17 @@ impl PyDataFrame {
}

pub fn to_numpy(&self, py: Python) -> Option<PyObject> {
let mut st = DataType::Int8;
let mut st = None;
for s in self.df.iter() {
let dt_i = s.dtype();
st = get_supertype(&st, dt_i).ok()?;
match st {
None => st = Some(dt_i.clone()),
Some(ref mut st) => {
*st = get_supertype(&st, dt_i).ok()?;
}
}
}
let st = st?;

match st {
DataType::UInt32 => self
Expand Down
6 changes: 6 additions & 0 deletions py-polars/tests/test_interop.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,9 @@ def test_from_pyarrow_chunked_array() -> None:
column = pa.chunked_array([[1], [2]])
series = pl.Series("column", column)
assert series.to_list() == [1, 2]


def test_numpy_preserve_uint64_4112() -> None:
assert pl.DataFrame({"a": [1, 2, 3]}).with_column(
pl.col("a").hash()
).to_numpy().dtype == np.dtype("uint64")

0 comments on commit d394f83

Please sign in to comment.