Skip to content

Commit

Permalink
depr(python): Deprecate dtype_if_empty parameter for Series const…
Browse files Browse the repository at this point in the history
…ructor (#13976)
  • Loading branch information
stinodego authored Jan 25, 2024
1 parent 2847836 commit e89c8ea
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
25 changes: 20 additions & 5 deletions py-polars/polars/series/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,14 @@ class Series:
nan_to_null
In case a numpy array is used to create this Series, indicate how to deal
with np.nan values. (This parameter is a no-op on non-numpy data).
dtype_if_empty=dtype_if_empty : DataType, default None
If no dtype is specified and values contains None, an empty list, or a
list with only None values, set the Polars dtype of the Series data.
If not specified, Float32 is used in those cases.
dtype_if_empty : DataType, default Null
Data type of the Series if `values` contains no non-null data.
.. deprecated:: 0.20.6
The data type for empty Series will always be Null.
To preserve behavior, check if the resulting Series has data type Null and
cast to the desired data type.
This parameter will be removed in the next breaking release.
Examples
--------
Expand Down Expand Up @@ -255,6 +259,15 @@ def __init__(
nan_to_null: bool = False,
dtype_if_empty: PolarsDataType = Null,
):
if dtype_if_empty != Null:
issue_deprecation_warning(
"The `dtype_if_empty` parameter for the Series constructor is deprecated."
" The data type for empty Series will always be Null."
" To preserve behavior, check if the resulting Series has data type Null and cast to the desired data type."
" This parameter will be removed in the next breaking release.",
version="0.20.6",
)

# If 'Unknown' treat as None to attempt inference
if dtype == Unknown:
dtype = None
Expand Down Expand Up @@ -712,7 +725,9 @@ def _comp(self, other: Any, op: ComparisonOperator) -> Series:
if isinstance(other, Sequence) and not isinstance(other, str):
if self.dtype in (List, Array):
other = [other]
other = Series("", other, dtype_if_empty=self.dtype)
other = Series("", other)
if other.dtype == Null:
other.cast(self.dtype)

if isinstance(other, Series):
return self._from_pyseries(getattr(self._s, op)(other._s))
Expand Down
22 changes: 15 additions & 7 deletions py-polars/tests/unit/series/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,9 @@ def test_init_inputs(monkeypatch: Any) -> None:
assert pl.Series("a").dtype == pl.Null # Null dtype used in case of no data
assert pl.Series().dtype == pl.Null
assert pl.Series([]).dtype == pl.Null
assert pl.Series(dtype_if_empty=pl.String).dtype == pl.String
assert pl.Series([], dtype_if_empty=pl.UInt16).dtype == pl.UInt16
assert (
pl.Series([None, None, None]).dtype == pl.Null
) # f32 type used for list with only None
assert pl.Series([None, None, None], dtype_if_empty=pl.Int8).dtype == pl.Int8
# note: "== []" will be cast to empty Series with String dtype.
assert_series_equal(
pl.Series([], dtype_if_empty=pl.String) == [], pl.Series("", dtype=pl.Boolean)
)
assert pl.Series(values=[True, False]).dtype == pl.Boolean
assert pl.Series(values=np.array([True, False])).dtype == pl.Boolean
assert pl.Series(values=np.array(["foo", "bar"])).dtype == pl.String
Expand Down Expand Up @@ -187,6 +180,21 @@ def test_init_inputs(monkeypatch: Any) -> None:
pl.DataFrame(np.array([1, 2, 3]), schema=["a"])


def test_init_dtype_if_empty_deprecated() -> None:
with pytest.deprecated_call():
assert pl.Series(dtype_if_empty=pl.String).dtype == pl.String
with pytest.deprecated_call():
assert pl.Series([], dtype_if_empty=pl.UInt16).dtype == pl.UInt16

with pytest.deprecated_call():
assert pl.Series([None, None, None], dtype_if_empty=pl.Int8).dtype == pl.Int8

# note: "== []" will be cast to empty Series with String dtype.
with pytest.deprecated_call():
s = pl.Series([], dtype_if_empty=pl.String) == []
assert_series_equal(s, pl.Series("", dtype=pl.Boolean))


def test_init_structured_objects() -> None:
# validate init from dataclass, namedtuple, and pydantic model objects
from typing import NamedTuple
Expand Down

0 comments on commit e89c8ea

Please sign in to comment.