From 400cf12597142ac37b1aa5bcd9bd5b3352305c8f Mon Sep 17 00:00:00 2001 From: Alexander Beedie Date: Fri, 29 Sep 2023 12:48:47 +0400 Subject: [PATCH] fix(rust,python): Fix edge-case where the Array dtype could (internally) be considered numeric (#11398) Co-authored-by: Stijn de Gooijer --- crates/polars-core/src/datatypes/dtype.rs | 36 +++++++------------- py-polars/tests/unit/datatypes/test_array.py | 13 +++++++ py-polars/tests/unit/test_interop.py | 21 ++++++++---- 3 files changed, 40 insertions(+), 30 deletions(-) diff --git a/crates/polars-core/src/datatypes/dtype.rs b/crates/polars-core/src/datatypes/dtype.rs index 6043fa31504e..9866cb9456b7 100644 --- a/crates/polars-core/src/datatypes/dtype.rs +++ b/crates/polars-core/src/datatypes/dtype.rs @@ -163,29 +163,19 @@ impl DataType { /// Check if this [`DataType`] is a numeric type. pub fn is_numeric(&self) -> bool { - // allow because it cannot be replaced when object feature is activated - #[allow(clippy::match_like_matches_macro)] - match self { - DataType::Utf8 - | DataType::List(_) - | DataType::Date - | DataType::Datetime(_, _) - | DataType::Duration(_) - | DataType::Time - | DataType::Boolean - | DataType::Unknown - | DataType::Null => false, - DataType::Binary => false, - #[cfg(feature = "object")] - DataType::Object(_) => false, - #[cfg(feature = "dtype-categorical")] - DataType::Categorical(_) => false, - #[cfg(feature = "dtype-struct")] - DataType::Struct(_) => false, - #[cfg(feature = "dtype-decimal")] - DataType::Decimal(_, _) => false, - _ => true, - } + matches!( + self, + DataType::UInt8 + | DataType::UInt16 + | DataType::UInt32 + | DataType::UInt64 + | DataType::Int8 + | DataType::Int16 + | DataType::Int32 + | DataType::Int64 + | DataType::Float32 + | DataType::Float64 + ) } pub fn is_float(&self) -> bool { diff --git a/py-polars/tests/unit/datatypes/test_array.py b/py-polars/tests/unit/datatypes/test_array.py index 2eb2fedde73c..748ab37e261a 100644 --- a/py-polars/tests/unit/datatypes/test_array.py +++ b/py-polars/tests/unit/datatypes/test_array.py @@ -1,6 +1,7 @@ import pytest import polars as pl +from polars.exceptions import InvalidOperationError from polars.testing import assert_series_equal @@ -79,6 +80,18 @@ def test_array_in_group_by() -> None: assert out.to_dict(False) == {"g": [1, 2], "a": [[[1, 2], [2, 2]], [[1, 4]]]} +def test_array_invalid_operation() -> None: + s = pl.Series( + [[1, 2], [8, 9]], + dtype=pl.Array(width=2, inner=pl.Int32), + ) + with pytest.raises( + InvalidOperationError, + match=r"`sign` operation not supported for dtype `array\[", + ): + s.sign() + + def test_array_concat() -> None: a_df = pl.DataFrame({"a": [[0, 1], [1, 0]]}).select( pl.col("a").cast(pl.Array(width=2, inner=pl.Int32)) diff --git a/py-polars/tests/unit/test_interop.py b/py-polars/tests/unit/test_interop.py index f47da7d46f57..d3981cc8528a 100644 --- a/py-polars/tests/unit/test_interop.py +++ b/py-polars/tests/unit/test_interop.py @@ -1,6 +1,5 @@ from __future__ import annotations -import warnings from datetime import date, datetime, time from typing import Any, cast @@ -121,13 +120,21 @@ def test_from_pandas() -> None: for col, dtype in overrides.items(): assert out.schema[col] == dtype - # empty and/or all null values, no pandas dtype - with warnings.catch_warnings(): - warnings.simplefilter("ignore", Warning) - for nulls in ([], [None], [None, None], [None, None, None]): - srs = pl.from_pandas(pd.Series(nulls)) - assert nulls == srs.to_list() +@pytest.mark.parametrize( + "nulls", + [ + [], + [None], + [None, None], + [None, None, None], + ], +) +def test_from_pandas_nulls(nulls: list[None]) -> None: + # empty and/or all null values, no pandas dtype + ps = pd.Series(nulls) + s = pl.from_pandas(ps) + assert nulls == s.to_list() def test_from_pandas_nan_to_null() -> None: