Skip to content

Commit

Permalink
fix: Use primitive constructors to create a Series of lists when dtyp…
Browse files Browse the repository at this point in the history
…e is defined
  • Loading branch information
petrosbar committed Mar 12, 2024
1 parent c5b53ab commit f0cf33e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
16 changes: 13 additions & 3 deletions py-polars/polars/_utils/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,6 @@ def sequence_to_pyseries(

# lists defer to subsequent handling; identify nested type
elif dtype == List:
getattr(dtype, "inner", None)
python_dtype = list

# infer temporal type handling
Expand All @@ -459,7 +458,7 @@ def sequence_to_pyseries(
or is_namedtuple(value.__class__)
) and dtype != Object:
return pl.DataFrame(values).to_struct(name)._s
elif isinstance(value, range):
elif isinstance(value, range) and dtype is None:
values = [range_to_series("", v) for v in values]
else:
# for temporal dtypes:
Expand Down Expand Up @@ -602,7 +601,18 @@ def sequence_to_pyseries(
if isinstance(dtype, Object):
return PySeries.new_object(name, values, strict)
if dtype:
srs = sequence_from_any_value_and_dtype_or_object(name, values, dtype)
if (inner_dtype := getattr(dtype, "inner", None)) is not None:
py_srs = [
None
if value is None
else sequence_to_pyseries("", value, inner_dtype, strict=strict)
for value in values
]
srs = PySeries.new_series_list(name, py_srs, strict)
else:
srs = sequence_from_any_value_and_dtype_or_object(
name, values, dtype
)
if dtype != srs.dtype():
srs = srs.cast(dtype, strict=False)
return srs
Expand Down
9 changes: 9 additions & 0 deletions py-polars/tests/unit/datatypes/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,29 @@ def test_dtype() -> None:
assert a.dtype.is_(pl.List(pl.Int64))

# explicit
u64_max = (2**64) - 1
df = pl.DataFrame(
data={
"i": [[1, 2, 3]],
"li": [[[1, 2, 3]]],
"u": [[u64_max]],
"tm": [[time(10, 30, 45)]],
"dt": [[date(2022, 12, 31)]],
"dtm": [[datetime(2022, 12, 31, 1, 2, 3)]],
},
schema=[
("i", pl.List(pl.Int8)),
("li", pl.List(pl.List(pl.Int8))),
("u", pl.List(pl.UInt64)),
("tm", pl.List(pl.Time)),
("dt", pl.List(pl.Date)),
("dtm", pl.List(pl.Datetime)),
],
)
assert df.schema == {
"i": pl.List(pl.Int8),
"li": pl.List(pl.List(pl.Int8)),
"u": pl.List(pl.UInt64),
"tm": pl.List(pl.Time),
"dt": pl.List(pl.Date),
"dtm": pl.List(pl.Datetime),
Expand All @@ -48,6 +55,8 @@ def test_dtype() -> None:
assert df.rows() == [
(
[1, 2, 3],
[[1, 2, 3]],
[u64_max],
[time(10, 30, 45)],
[date(2022, 12, 31)],
[datetime(2022, 12, 31, 1, 2, 3)],
Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/unit/io/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def test_write_json_duration() -> None:
([["a", "b"], [None, None]], pl.Array(pl.Utf8, width=2)),
([[True, False, None], [None, None, None]], pl.Array(pl.Utf8, width=3)),
(
[[[1, 2, 3], [4, None]], None, [[None, None, 2]]],
[[[1, 2, 3], [4, None, 5]], None, [[None, None, 2]]],
pl.List(pl.Array(pl.Int32(), width=3)),
),
(
Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/unit/series/test_describe.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def test_series_describe_null() -> None:
def test_series_describe_nested_list() -> None:
s = pl.Series(
values=[[10e10, 10e15], [10e12, 10e13], [10e10, 10e15]],
dtype=pl.List(pl.Int64),
dtype=pl.List(pl.Float64),
)
result = s.describe()
stats = {
Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/unit/test_serde.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def test_serde_array_dtype() -> None:
assert_series_equal(pickle.loads(pickle.dumps(s)), s)

nested_s = pl.Series(
[[[1, 2, 3], [4, None]], None, [[None, None, 2]]],
[[[1, 2, 3], [4, None, 5]], None, [[None, None, 2]]],
dtype=pl.List(pl.Array(pl.Int32(), width=3)),
)
assert_series_equal(pickle.loads(pickle.dumps(nested_s)), nested_s)
Expand Down

0 comments on commit f0cf33e

Please sign in to comment.