Skip to content

Commit

Permalink
fix(rust,python): Fix edge-case where the Array dtype could (internal…
Browse files Browse the repository at this point in the history
…ly) be considered numeric (pola-rs#11398)

Co-authored-by: Stijn de Gooijer <stijn@degooijer.io>
  • Loading branch information
alexander-beedie and stinodego authored Sep 29, 2023
1 parent 8f0fae4 commit 400cf12
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 30 deletions.
36 changes: 13 additions & 23 deletions crates/polars-core/src/datatypes/dtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions py-polars/tests/unit/datatypes/test_array.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

import polars as pl
from polars.exceptions import InvalidOperationError
from polars.testing import assert_series_equal


Expand Down Expand Up @@ -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))
Expand Down
21 changes: 14 additions & 7 deletions py-polars/tests/unit/test_interop.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import warnings
from datetime import date, datetime, time
from typing import Any, cast

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 400cf12

Please sign in to comment.