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 DataType.is_nested #11844

Merged
merged 2 commits into from
Oct 20, 2023
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
2 changes: 2 additions & 0 deletions py-polars/polars/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
DURATION_DTYPES,
FLOAT_DTYPES,
INTEGER_DTYPES,
NESTED_DTYPES,
NUMERIC_DTYPES,
TEMPORAL_DTYPES,
Array,
Expand Down Expand Up @@ -253,6 +254,7 @@
"DURATION_DTYPES",
"FLOAT_DTYPES",
"INTEGER_DTYPES",
"NESTED_DTYPES",
"NUMERIC_DTYPES",
"TEMPORAL_DTYPES",
# polars.type_aliases
Expand Down
2 changes: 2 additions & 0 deletions py-polars/polars/datatypes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
FLOAT_DTYPES,
INTEGER_DTYPES,
N_INFER_DEFAULT,
NESTED_DTYPES,
NUMERIC_DTYPES,
SIGNED_INTEGER_DTYPES,
TEMPORAL_DTYPES,
Expand Down Expand Up @@ -113,6 +114,7 @@
"DURATION_DTYPES",
"FLOAT_DTYPES",
"INTEGER_DTYPES",
"NESTED_DTYPES",
"NUMERIC_DTYPES",
"N_INFER_DEFAULT",
"SIGNED_INTEGER_DTYPES",
Expand Down
32 changes: 30 additions & 2 deletions py-polars/polars/datatypes/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,21 @@ def is_not(self, other: PolarsDataType) -> bool:

@classproperty
def is_nested(self) -> bool:
"""Check if this data type is nested."""
"""
Check if this data type is nested.

.. deprecated:: 0.19.10
Use `dtype in pl.NESTED_DTYPES` instead.

"""
from polars.utils.deprecation import issue_deprecation_warning

message = (
"`DataType.is_nested` is deprecated and will be removed in the next breaking release."
" It will be changed to a classmethod rather than a property."
" To silence this warning, use `dtype in pl.NESTED_DTYPES` instead."
)
issue_deprecation_warning(message, version="0.19.10")
return False


Expand Down Expand Up @@ -220,7 +234,21 @@ class NestedType(DataType):

@classproperty
def is_nested(self) -> bool:
"""Check if this data type is nested."""
"""
Check if this data type is nested.

.. deprecated:: 0.19.10
Use `dtype in pl.NESTED_DTYPES` instead.

"""
from polars.utils.deprecation import issue_deprecation_warning

message = (
"`DataType.is_nested` is deprecated and will be removed in the next breaking release."
" It will be changed to a classmethod rather than a property."
" To silence this warning, use `dtype in pl.NESTED_DTYPES` instead."
)
issue_deprecation_warning(message, version="0.19.10")
return True


Expand Down
4 changes: 4 additions & 0 deletions py-polars/polars/datatypes/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
Int16,
Int32,
Int64,
List,
Struct,
Time,
UInt8,
UInt16,
Expand Down Expand Up @@ -73,5 +75,7 @@
FLOAT_DTYPES | INTEGER_DTYPES | frozenset([Decimal])
)

NESTED_DTYPES: frozenset[PolarsDataType] = DataTypeGroup([List, Struct])

# number of rows to scan by default when inferring datatypes
N_INFER_DEFAULT = 100
3 changes: 2 additions & 1 deletion py-polars/polars/testing/asserts/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from polars import functions as F
from polars.datatypes import (
FLOAT_DTYPES,
NESTED_DTYPES,
UNSIGNED_INTEGER_DTYPES,
Categorical,
List,
Expand Down Expand Up @@ -139,7 +140,7 @@ def _assert_series_values_equal(
unequal = unequal | left.is_nan() | right.is_nan()

# check nested dtypes in separate function
if left.dtype.is_nested or right.dtype.is_nested:
if left.dtype in NESTED_DTYPES or right.dtype in NESTED_DTYPES:
if _assert_series_nested(
left=left.filter(unequal),
right=right.filter(unequal),
Expand Down
18 changes: 12 additions & 6 deletions py-polars/tests/unit/datatypes/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_dtype() -> None:
"dt": pl.List(pl.Date),
"dtm": pl.List(pl.Datetime),
}
assert all(tp.is_nested for tp in df.dtypes)
assert all(tp in pl.NESTED_DTYPES for tp in df.dtypes)
assert df.schema["i"].inner == pl.Int8 # type: ignore[union-attr]
assert df.rows() == [
(
Expand All @@ -69,17 +69,15 @@ def test_categorical() -> None:
out = (
df.group_by(["a", "b"])
.agg(
[
pl.col("c").count().alias("num_different_c"),
pl.col("c").alias("c_values"),
]
pl.col("c").count().alias("num_different_c"),
pl.col("c").alias("c_values"),
)
.filter(pl.col("num_different_c") >= 2)
.to_series(3)
)

assert out.inner_dtype == pl.Categorical
assert not out.inner_dtype.is_nested
assert out.inner_dtype not in pl.NESTED_DTYPES


def test_cast_inner() -> None:
Expand Down Expand Up @@ -565,3 +563,11 @@ def test_list_inner_cast_physical_11513() -> None:
},
)
assert df.select(pl.col("struct").take(0)).to_dict(False) == {"struct": [[]]}


@pytest.mark.parametrize(
("dtype", "expected"), [(pl.List, True), (pl.Struct, True), (pl.Utf8, False)]
)
def test_list_is_nested_deprecated(dtype: PolarsDataType, expected: bool) -> None:
with pytest.deprecated_call():
assert dtype.is_nested is expected
2 changes: 1 addition & 1 deletion py-polars/tests/unit/datatypes/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def test_struct_unnest_multiple() -> None:
# List input
result = df_structs.unnest(["s1", "s2"])
assert_frame_equal(result, df)
assert all(tp.is_nested for tp in df_structs.dtypes)
assert all(tp in pl.NESTED_DTYPES for tp in df_structs.dtypes)

# Positional input
result = df_structs.unnest("s1", "s2")
Expand Down