Skip to content

Commit

Permalink
python: respect dtype in create from numpy array (#2899)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Mar 15, 2022
1 parent bfbedfa commit 9b74fc7
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 10 deletions.
2 changes: 0 additions & 2 deletions py-polars/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions py-polars/polars/internals/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ def __init__(
self._s = arrow_to_pyseries(name, values)
elif isinstance(values, np.ndarray):
self._s = numpy_to_pyseries(name, values, strict, nan_to_null)
if dtype is not None:
self._s = self.cast(dtype, strict=True)._s
elif isinstance(values, Sequence):
self._s = sequence_to_pyseries(name, values, dtype=dtype, strict=strict)
elif _PANDAS_AVAILABLE and isinstance(values, (pd.Series, pd.DatetimeIndex)):
Expand Down
8 changes: 0 additions & 8 deletions py-polars/src/npy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,6 @@ pub unsafe fn aligned_array<T: Element + NativeType>(
);
(PyArray1::from_owned_ptr(py, ptr), buf)
}
/// TODO: needs more explanation
/// # Safety
///
/// Create a vector from raw parts.
pub unsafe fn vec_from_ptr<T>(ptr: usize, len: usize) -> Vec<T> {
let ptr = ptr as *mut T;
Vec::from_raw_parts(ptr, len, len)
}

/// Get reference counter for numpy arrays.
/// - For CPython: Get reference counter.
Expand Down
4 changes: 4 additions & 0 deletions py-polars/tests/test_interop.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,7 @@ def test_from_null_column() -> None:

def test_to_pandas_series() -> None:
assert (pl.Series("a", [1, 2, 3]).to_pandas() == pd.Series([1, 2, 3])).all()


def test_respect_dtype_with_series_from_numpy() -> None:
assert pl.Series("foo", np.array([1, 2, 3]), dtype=pl.UInt32).dtype == pl.UInt32

0 comments on commit 9b74fc7

Please sign in to comment.