Skip to content

Commit

Permalink
Deprecate Series.is_temporal
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego committed Nov 14, 2023
1 parent 39d4866 commit b08ea35
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 33 deletions.
60 changes: 32 additions & 28 deletions py-polars/polars/series/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3961,33 +3961,6 @@ def is_between(
.to_series()
)

def is_temporal(self, excluding: OneOrMoreDataTypes | None = None) -> bool:
"""
Check if this Series datatype is temporal.
Parameters
----------
excluding
Optionally exclude one or more temporal dtypes from matching.
Examples
--------
>>> from datetime import date
>>> s = pl.Series([date(2021, 1, 1), date(2021, 1, 2), date(2021, 1, 3)])
>>> s.is_temporal()
True
>>> s.is_temporal(excluding=[pl.Date])
False
"""
if excluding is not None:
if not isinstance(excluding, Iterable):
excluding = [excluding]
if self.dtype in excluding:
return False

return self.dtype.is_temporal()

def is_boolean(self) -> bool:
"""
Check if this Series is a Boolean.
Expand Down Expand Up @@ -4122,7 +4095,7 @@ def raise_no_zero_copy() -> None:
use_pyarrow
and _PYARROW_AVAILABLE
and self.dtype != Object
and not self.is_temporal(excluding=Time)
and (self.dtype == Time or not self.dtype.is_temporal())
):
return self.to_arrow().to_numpy(
*args, zero_copy_only=zero_copy_only, writable=writable
Expand Down Expand Up @@ -6944,6 +6917,37 @@ def is_numeric(self) -> bool:
"""
return self.dtype.is_numeric()

@deprecate_function("Use `Series.dtype.is_temporal()` instead.", version="0.19.13")
def is_temporal(self, excluding: OneOrMoreDataTypes | None = None) -> bool:
"""
Check if this Series datatype is temporal.
.. deprecated:: 0.19.13
Use `Series.dtype.is_temporal()` instead.
Parameters
----------
excluding
Optionally exclude one or more temporal dtypes from matching.
Examples
--------
>>> from datetime import date
>>> s = pl.Series([date(2021, 1, 1), date(2021, 1, 2), date(2021, 1, 3)])
>>> s.is_temporal() # doctest: +SKIP
True
>>> s.is_temporal(excluding=[pl.Date]) # doctest: +SKIP
False
"""
if excluding is not None:
if not isinstance(excluding, Iterable):
excluding = [excluding]
if self.dtype in excluding:
return False

return self.dtype.is_temporal()

# Keep the `list` and `str` properties below at the end of the definition of Series,
# as to not confuse mypy with the type annotation `str` and `list`

Expand Down
6 changes: 1 addition & 5 deletions py-polars/tests/unit/datatypes/test_temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2673,11 +2673,7 @@ def test_series_is_temporal() -> None:
pl.Datetime("ns", "Europe/Amsterdam"),
}:
s = pl.Series([None], dtype=tp)
assert s.is_temporal() is True

s = pl.Series([datetime(2023, 2, 14, 11, 12, 13)], dtype=pl.Datetime)
for tp in (pl.Datetime, [pl.Datetime], [pl.Time, pl.Datetime]): # type: ignore[assignment]
assert s.is_temporal(excluding=tp) is False
assert s.dtype.is_temporal() is True


@pytest.mark.parametrize(
Expand Down
2 changes: 2 additions & 0 deletions py-polars/tests/unit/series/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,8 @@ def test_series_is_dtype_deprecated() -> None:
assert s.is_numeric() is True
with pytest.deprecated_call():
assert s.is_integer() is False
with pytest.deprecated_call():
assert s.is_temporal() is False


def test_series_head_tail_limit() -> None:
Expand Down

0 comments on commit b08ea35

Please sign in to comment.