Skip to content

Commit

Permalink
Python: check type consistency in list create
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Nov 27, 2021
1 parent 50494e9 commit 2d475d8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
22 changes: 16 additions & 6 deletions py-polars/polars/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,22 @@ def sequence_to_pyseries(
nested_value = _get_first_non_none(value)
nested_dtype = type(nested_value) if value is not None else float

if not _PYARROW_AVAILABLE: # pragma: no cover
dtype = py_type_to_dtype(nested_dtype)
try:
return PySeries.new_list(name, values, dtype)
except BaseException:
pass
# logs will show a panic if we infer wrong dtype
# and its hard to error from rust side
# to reduce the likelihood of this happening
# we infer the dtype of first 100 elements
# if all() fails, we will hit the PySeries.new_object
if not _PYARROW_AVAILABLE:
if all(
isinstance(val, nested_dtype)
for val in values[: min(100, len(values))]
): # pragma: no cover
dtype = py_type_to_dtype(nested_dtype)
try:
return PySeries.new_list(name, values, dtype)
except BaseException:
pass
# pass we create an object if we get here
else:
try:
nested_arrow_dtype = py_type_to_arrow_type(nested_dtype)
Expand Down
8 changes: 5 additions & 3 deletions py-polars/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,11 @@ def test_shape() -> None:


def test_create_list_series() -> None:
a = [[1, 2], None, [None, 3]]
s = pl.Series("", a)
assert s.to_list() == a
for b in [True, False]:
pl.internals.series._PYARROW_AVAILABLE = b
a = [[1, 2], None, [None, 3]]
s = pl.Series("", a)
assert s.to_list() == a


def test_iter() -> None:
Expand Down

0 comments on commit 2d475d8

Please sign in to comment.