Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

depr(python): Deprecate dtype_if_empty parameter for Series constructor #13976

Merged
merged 2 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
stinodego marked this conversation as resolved.
Show resolved Hide resolved
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