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

feat(python)!: Update constructors for Array and Decimal #12837

Merged
merged 2 commits into from
Dec 1, 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
4 changes: 2 additions & 2 deletions docs/src/python/user-guide/expressions/lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@
pl.Series("Array_2", [[1, 7, 3], [8, 1, 0]]),
],
schema={
"Array_1": pl.Array(inner=pl.Int64, width=2),
"Array_2": pl.Array(inner=pl.Int64, width=3),
"Array_1": pl.Array(pl.Int64, 2),
"Array_2": pl.Array(pl.Int64, 3),
},
)
print(array_df)
Expand Down
64 changes: 7 additions & 57 deletions py-polars/polars/datatypes/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,26 +327,9 @@ class Decimal(NumericType):

def __init__(
self,
*args: Any,
precision: int | None = None,
scale: int = 0,
):
from polars.utils.deprecation import issue_deprecation_warning

if args:
# TODO: When removing this deprecation, update the `to_object`
# implementation in py-polars/src/conversion.rs to use `call1` instead of
# `call`
issue_deprecation_warning(
"`Decimal` parameters `scale` and `precision` will change positions in the next breaking release."
" Use keyword arguments to keep current behavior and silence this warning.",
version="0.19.13",
)
if len(args) == 1:
scale = args[0]
else:
scale, precision = args[:2]

self.precision = precision
self.scale = scale

Expand Down Expand Up @@ -607,27 +590,20 @@ class Array(NestedType):
inner: PolarsDataType | None = None
width: int

def __init__( # noqa: D417
self,
*args: Any,
inner: PolarsDataType | PythonDataType | None = None,
width: int | None = None,
):
def __init__(self, inner: PolarsDataType | PythonDataType, width: int):
"""
Fixed length list type.

Parameters
----------
width
The length of the arrays.
inner
The `DataType` of the values within each array.
width
The length of the arrays.

Examples
--------
>>> s = pl.Series(
... "a", [[1, 2], [4, 3]], dtype=pl.Array(inner=pl.Int64, width=2)
... )
>>> s = pl.Series("a", [[1, 2], [4, 3]], dtype=pl.Array(pl.Int64, 2))
>>> s
shape: (2,)
Series: 'a' [array[i64, 2]]
Expand All @@ -637,41 +613,15 @@ def __init__( # noqa: D417
]

"""
from polars.utils.deprecation import issue_deprecation_warning

if args:
# TODO: When removing this deprecation, update the `to_object`
# implementation in py-polars/src/conversion.rs to use `call1` instead of
# `call`
issue_deprecation_warning(
"`Array` parameters `width` and `inner` will change positions in the next breaking release."
" Use keyword arguments to keep current behavior and silence this warning.",
version="0.19.11",
)
if len(args) == 1:
width = args[0]
else:
width, inner = args[:2]
if width is None:
raise TypeError("`width` must be specified when initializing an `Array`")

if inner is None:
issue_deprecation_warning(
"The default value for the `inner` parameter of `Array` will be removed in the next breaking release."
" Pass `inner=pl.Null`to keep current behavior and silence this warning.",
version="0.19.11",
)
inner = Null

self.width = width
self.inner = polars.datatypes.py_type_to_dtype(inner)

def __eq__(self, other: PolarsDataType) -> bool: # type: ignore[override]
# This equality check allows comparison of type classes and type instances.
# If a parent type is not specific about its inner type, we infer it as equal:
# > fixed-size-list[i64] == fixed-size-list[i64] -> True
# > fixed-size-list[i64] == fixed-size-list[f32] -> False
# > fixed-size-list[i64] == fixed-size-list -> True
# > array[i64] == array[i64] -> True
# > array[i64] == array[f32] -> False
# > array[i64] == array -> True

# allow comparing object instances to class
if type(other) is DataTypeClass and issubclass(other, Array):
Expand Down
10 changes: 5 additions & 5 deletions py-polars/polars/expr/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def min(self) -> Expr:
--------
>>> df = pl.DataFrame(
... data={"a": [[1, 2], [4, 3]]},
... schema={"a": pl.Array(inner=pl.Int64, width=2)},
... schema={"a": pl.Array(pl.Int64, 2)},
... )
>>> df.select(pl.col("a").arr.min())
shape: (2, 1)
Expand All @@ -48,7 +48,7 @@ def max(self) -> Expr:
--------
>>> df = pl.DataFrame(
... data={"a": [[1, 2], [4, 3]]},
... schema={"a": pl.Array(inner=pl.Int64, width=2)},
... schema={"a": pl.Array(pl.Int64, 2)},
... )
>>> df.select(pl.col("a").arr.max())
shape: (2, 1)
Expand All @@ -72,7 +72,7 @@ def sum(self) -> Expr:
--------
>>> df = pl.DataFrame(
... data={"a": [[1, 2], [4, 3]]},
... schema={"a": pl.Array(inner=pl.Int64, width=2)},
... schema={"a": pl.Array(pl.Int64, 2)},
... )
>>> df.select(pl.col("a").arr.sum())
shape: (2, 1)
Expand Down Expand Up @@ -103,7 +103,7 @@ def unique(self, *, maintain_order: bool = False) -> Expr:
... {
... "a": [[1, 1, 2]],
... },
... schema={"a": pl.Array(inner=pl.Int64, width=3)},
... schema={"a": pl.Array(pl.Int64, 3)},
... )
>>> df.select(pl.col("a").arr.unique())
shape: (1, 1)
Expand Down Expand Up @@ -131,7 +131,7 @@ def to_list(self) -> Expr:
--------
>>> df = pl.DataFrame(
... data={"a": [[1, 2], [3, 4]]},
... schema={"a": pl.Array(inner=pl.Int8, width=2)},
... schema={"a": pl.Array(pl.Int8, 2)},
... )
>>> df.select(pl.col("a").arr.to_list())
shape: (2, 1)
Expand Down
14 changes: 5 additions & 9 deletions py-polars/polars/series/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ def min(self) -> Series:

Examples
--------
>>> s = pl.Series(
... "a", [[1, 2], [4, 3]], dtype=pl.Array(inner=pl.Int64, width=2)
... )
>>> s = pl.Series("a", [[1, 2], [4, 3]], dtype=pl.Array(pl.Int64, 2))
>>> s.arr.min()
shape: (2,)
Series: 'a' [i64]
Expand All @@ -43,9 +41,7 @@ def max(self) -> Series:

Examples
--------
>>> s = pl.Series(
... "a", [[1, 2], [4, 3]], dtype=pl.Array(inner=pl.Int64, width=2)
... )
>>> s = pl.Series("a", [[1, 2], [4, 3]], dtype=pl.Array(pl.Int64, 2))
>>> s.arr.max()
shape: (2,)
Series: 'a' [i64]
Expand All @@ -64,7 +60,7 @@ def sum(self) -> Series:
--------
>>> df = pl.DataFrame(
... data={"a": [[1, 2], [4, 3]]},
... schema={"a": pl.Array(inner=pl.Int64, width=2)},
... schema={"a": pl.Array(pl.Int64, 2)},
... )
>>> df.select(pl.col("a").arr.sum())
shape: (2, 1)
Expand Down Expand Up @@ -94,7 +90,7 @@ def unique(self, *, maintain_order: bool = False) -> Series:
... {
... "a": [[1, 1, 2]],
... },
... schema_overrides={"a": pl.Array(inner=pl.Int64, width=3)},
... schema_overrides={"a": pl.Array(pl.Int64, 3)},
... )
>>> df.select(pl.col("a").arr.unique())
shape: (1, 1)
Expand All @@ -119,7 +115,7 @@ def to_list(self) -> Series:

Examples
--------
>>> s = pl.Series([[1, 2], [3, 4]], dtype=pl.Array(inner=pl.Int8, width=2))
>>> s = pl.Series([[1, 2], [3, 4]], dtype=pl.Array(pl.Int8, 2))
>>> s.arr.to_list()
shape: (2,)
Series: '' [list[i8]]
Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/unit/dataframe/test_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def test_from_arrow(monkeypatch: Any) -> None:
"c": pl.Datetime("us"),
"d": pl.Datetime("ns"),
"e": pl.Int32,
"decimal1": pl.Decimal(precision=2, scale=1),
"decimal1": pl.Decimal(2, 1),
}
expected_data = [
(
Expand Down
55 changes: 21 additions & 34 deletions py-polars/tests/unit/datatypes/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_cast_list_array() -> None:
payload = [[1, 2, 3], [4, 2, 3]]
s = pl.Series(payload)

dtype = pl.Array(inner=pl.Int64, width=3)
dtype = pl.Array(pl.Int64, 3)
out = s.cast(dtype)
assert out.dtype == dtype
assert out.to_list() == payload
Expand All @@ -20,19 +20,19 @@ def test_cast_list_array() -> None:
pl.ComputeError,
match=r"not all elements have the specified width",
):
s.cast(pl.Array(inner=pl.Int64, width=2))
s.cast(pl.Array(pl.Int64, 2))


def test_array_construction() -> None:
payload = [[1, 2, 3], [4, 2, 3]]

dtype = pl.Array(inner=pl.Int64, width=3)
dtype = pl.Array(pl.Int64, 3)
s = pl.Series(payload, dtype=dtype)
assert s.dtype == dtype
assert s.to_list() == payload

# inner type
dtype = pl.Array(inner=pl.UInt8, width=2)
dtype = pl.Array(pl.UInt8, 2)
payload = [[1, 2], [3, 4]]
s = pl.Series(payload, dtype=dtype)
assert s.dtype == dtype
Expand All @@ -41,13 +41,13 @@ def test_array_construction() -> None:
# create using schema
df = pl.DataFrame(
schema={
"a": pl.Array(inner=pl.Float32, width=3),
"b": pl.Array(inner=pl.Datetime("ms"), width=5),
"a": pl.Array(pl.Float32, 3),
"b": pl.Array(pl.Datetime("ms"), 5),
}
)
assert df.dtypes == [
pl.Array(inner=pl.Float32, width=3),
pl.Array(inner=pl.Datetime("ms"), width=5),
pl.Array(pl.Float32, 3),
pl.Array(pl.Datetime("ms"), 5),
]
assert df.rows() == []

Expand All @@ -56,9 +56,7 @@ def test_array_in_group_by() -> None:
df = pl.DataFrame(
[
pl.Series("id", [1, 2]),
pl.Series(
"list", [[1, 2], [5, 5]], dtype=pl.Array(inner=pl.UInt8, width=2)
),
pl.Series("list", [[1, 2], [5, 5]], dtype=pl.Array(pl.UInt8, 2)),
]
)

Expand All @@ -68,7 +66,7 @@ def test_array_in_group_by() -> None:

df = pl.DataFrame(
{"a": [[1, 2], [2, 2], [1, 4]], "g": [1, 1, 2]},
schema={"a": pl.Array(inner=pl.Int64, width=2), "g": pl.Int64},
schema={"a": pl.Array(pl.Int64, 2), "g": pl.Int64},
)

out0 = df.group_by("g").agg(pl.col("a")).sort("g")
Expand All @@ -77,7 +75,7 @@ def test_array_in_group_by() -> None:
for out in [out0, out1]:
assert out.schema == {
"g": pl.Int64,
"a": pl.List(pl.Array(inner=pl.Int64, width=2)),
"a": pl.List(pl.Array(pl.Int64, 2)),
}
assert out.to_dict(as_series=False) == {
"g": [1, 2],
Expand All @@ -88,7 +86,7 @@ def test_array_in_group_by() -> None:
def test_array_invalid_operation() -> None:
s = pl.Series(
[[1, 2], [8, 9]],
dtype=pl.Array(inner=pl.Int32, width=2),
dtype=pl.Array(pl.Int32, 2),
)
with pytest.raises(
InvalidOperationError,
Expand All @@ -99,26 +97,26 @@ def test_array_invalid_operation() -> None:

def test_array_concat() -> None:
a_df = pl.DataFrame({"a": [[0, 1], [1, 0]]}).select(
pl.col("a").cast(pl.Array(inner=pl.Int32, width=2))
pl.col("a").cast(pl.Array(pl.Int32, 2))
)
b_df = pl.DataFrame({"a": [[1, 1], [0, 0]]}).select(
pl.col("a").cast(pl.Array(inner=pl.Int32, width=2))
pl.col("a").cast(pl.Array(pl.Int32, 2))
)
assert pl.concat([a_df, b_df]).to_dict(as_series=False) == {
"a": [[0, 1], [1, 0], [1, 1], [0, 0]]
}


def test_array_equal_and_not_equal() -> None:
left = pl.Series([[1, 2], [3, 5]], dtype=pl.Array(width=2, inner=pl.Int64))
right = pl.Series([[1, 2], [3, 1]], dtype=pl.Array(width=2, inner=pl.Int64))
left = pl.Series([[1, 2], [3, 5]], dtype=pl.Array(pl.Int64, 2))
right = pl.Series([[1, 2], [3, 1]], dtype=pl.Array(pl.Int64, 2))
assert_series_equal(left == right, pl.Series([True, False]))
assert_series_equal(left.eq_missing(right), pl.Series([True, False]))
assert_series_equal(left != right, pl.Series([False, True]))
assert_series_equal(left.ne_missing(right), pl.Series([False, True]))

left = pl.Series([[1, None], [3, None]], dtype=pl.Array(width=2, inner=pl.Int64))
right = pl.Series([[1, None], [3, 4]], dtype=pl.Array(width=2, inner=pl.Int64))
left = pl.Series([[1, None], [3, None]], dtype=pl.Array(pl.Int64, 2))
right = pl.Series([[1, None], [3, 4]], dtype=pl.Array(pl.Int64, 2))
assert_series_equal(left == right, pl.Series([True, False]))
assert_series_equal(left.eq_missing(right), pl.Series([True, False]))
assert_series_equal(left != right, pl.Series([False, True]))
Expand All @@ -127,19 +125,8 @@ def test_array_equal_and_not_equal() -> None:
# TODO: test eq_missing with nulled arrays, rather than null elements.


def test_array_init_deprecation() -> None:
with pytest.deprecated_call():
pl.Array(2)
with pytest.deprecated_call():
pl.Array(2, pl.Utf8)
with pytest.deprecated_call():
pl.Array(2, inner=pl.Utf8)
with pytest.deprecated_call():
pl.Array(width=2)


def test_array_list_supertype() -> None:
s1 = pl.Series([[1, 2], [3, 4]], dtype=pl.Array(width=2, inner=pl.Int64))
s1 = pl.Series([[1, 2], [3, 4]], dtype=pl.Array(pl.Int64, 2))
s2 = pl.Series([[1.0, 2.0], [3.0, 4.5]], dtype=pl.List(inner=pl.Float64))

result = s1 == s2
Expand All @@ -151,6 +138,6 @@ def test_array_list_supertype() -> None:
def test_array_in_list() -> None:
s = pl.Series(
[[[1, 2], [3, 4]], [[5, 6], [7, 8]]],
dtype=pl.List(pl.Array(inner=pl.Int8, width=2)),
dtype=pl.List(pl.Array(pl.Int8, 2)),
)
assert s.dtype == pl.List(pl.Array(inner=pl.Int8, width=2))
assert s.dtype == pl.List(pl.Array(pl.Int8, 2))
4 changes: 2 additions & 2 deletions py-polars/tests/unit/datatypes/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,8 +645,8 @@ def test_empty_struct() -> None:
pl.List,
pl.List(pl.Null),
pl.List(pl.Utf8),
pl.Array(inner=pl.Null, width=32),
pl.Array(inner=pl.UInt8, width=16),
pl.Array(pl.Null, 32),
pl.Array(pl.UInt8, 16),
pl.Struct,
pl.Struct([pl.Field("", pl.Null)]),
pl.Struct([pl.Field("x", pl.UInt32), pl.Field("y", pl.Float64)]),
Expand Down
Loading