Skip to content

Commit

Permalink
DEPR: is_period, is_interval (#56038)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored Nov 18, 2023
1 parent b348eac commit 1230529
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 13 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ For example:
Other Deprecations
^^^^^^^^^^^^^^^^^^
- Changed :meth:`Timedelta.resolution_string` to return ``h``, ``min``, ``s``, ``ms``, ``us``, and ``ns`` instead of ``H``, ``T``, ``S``, ``L``, ``U``, and ``N``, for compatibility with respective deprecations in frequency aliases (:issue:`52536`)
- Deprecated :func:`pandas.api.types.is_interval` and :func:`pandas.api.types.is_period`, use ``isinstance(obj, pd.Interval)`` and ``isinstance(obj, pd.Period)`` instead (:issue:`55264`)
- Deprecated :func:`read_gbq` and :meth:`DataFrame.to_gbq`. Use ``pandas_gbq.read_gbq`` and ``pandas_gbq.to_gbq`` instead https://pandas-gbq.readthedocs.io/en/latest/api.html (:issue:`55525`)
- Deprecated :meth:`.DataFrameGroupBy.fillna` and :meth:`.SeriesGroupBy.fillna`; use :meth:`.DataFrameGroupBy.ffill`, :meth:`.DataFrameGroupBy.bfill` for forward and backward filling or :meth:`.DataFrame.fillna` to fill with a single value (or the Series equivalents) (:issue:`55718`)
- Deprecated :meth:`Index.format`, use ``index.astype(str)`` or ``index.map(formatter)`` instead (:issue:`55413`)
Expand Down
31 changes: 27 additions & 4 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ from numpy cimport (
)

cnp.import_array()
from pandas._libs.interval import Interval


cdef extern from "pandas/parser/pd_parser.h":
Expand Down Expand Up @@ -228,7 +229,7 @@ def is_scalar(val: object) -> bool:
# Note: PyNumber_Check check includes Decimal, Fraction, numbers.Number
return (PyNumber_Check(val)
or is_period_object(val)
or is_interval(val)
or isinstance(val, Interval)
or is_offset_object(val))


Expand Down Expand Up @@ -1161,6 +1162,17 @@ cpdef bint is_decimal(object obj):


cpdef bint is_interval(object obj):
import warnings

from pandas.util._exceptions import find_stack_level

warnings.warn(
# GH#55264
"is_interval is deprecated and will be removed in a future version. "
"Use isinstance(obj, pd.Interval) instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
return getattr(obj, "_typ", "_typ") == "interval"


Expand All @@ -1172,6 +1184,17 @@ def is_period(val: object) -> bool:
-------
bool
"""
import warnings

from pandas.util._exceptions import find_stack_level

warnings.warn(
# GH#55264
"is_period is deprecated and will be removed in a future version. "
"Use isinstance(obj, pd.Period) instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
return is_period_object(val)


Expand Down Expand Up @@ -1694,7 +1717,7 @@ def infer_dtype(value: object, skipna: bool = True) -> str:
if is_period_array(values, skipna=skipna):
return "period"

elif is_interval(val):
elif isinstance(val, Interval):
if is_interval_array(values):
return "interval"

Expand Down Expand Up @@ -2169,7 +2192,7 @@ cpdef bint is_interval_array(ndarray values):
for i in range(n):
val = values[i]

if is_interval(val):
if isinstance(val, Interval):
if closed is None:
closed = val.closed
numeric = (
Expand Down Expand Up @@ -2630,7 +2653,7 @@ def maybe_convert_objects(ndarray[object] objects,
except (ValueError, TypeError):
seen.object_ = True
break
elif is_interval(val):
elif isinstance(val, Interval):
if convert_non_numeric:
seen.interval_ = True
break
Expand Down
10 changes: 7 additions & 3 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@

from pandas._config import using_pyarrow_string_dtype

from pandas._libs import lib
from pandas._libs import (
Interval,
Period,
lib,
)
from pandas._libs.missing import (
NA,
NAType,
Expand Down Expand Up @@ -850,9 +854,9 @@ def infer_dtype_from_scalar(val) -> tuple[DtypeObj, Any]:
elif is_complex(val):
dtype = np.dtype(np.complex128)

if lib.is_period(val):
if isinstance(val, Period):
dtype = PeriodDtype(freq=val.freq)
elif lib.is_interval(val):
elif isinstance(val, Interval):
subtype = infer_dtype_from_scalar(val.left)[0]
dtype = IntervalDtype(subtype=subtype, closed=val.closed)

Expand Down
3 changes: 2 additions & 1 deletion pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
)
from pandas._libs.tslibs import (
BaseOffset,
Period,
Timedelta,
Timestamp,
to_offset,
Expand Down Expand Up @@ -561,7 +562,7 @@ def _maybe_convert_i8(self, key):
if scalar:
# Timestamp/Timedelta
key_dtype, key_i8 = infer_dtype_from_scalar(key)
if lib.is_period(key):
if isinstance(key, Period):
key_i8 = key.ordinal
elif isinstance(key_i8, Timestamp):
key_i8 = key_i8._value
Expand Down
22 changes: 17 additions & 5 deletions pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -1622,11 +1622,23 @@ def test_to_object_array_width(self):
tm.assert_numpy_array_equal(out, expected)

def test_is_period(self):
assert lib.is_period(Period("2011-01", freq="M"))
assert not lib.is_period(PeriodIndex(["2011-01"], freq="M"))
assert not lib.is_period(Timestamp("2011-01"))
assert not lib.is_period(1)
assert not lib.is_period(np.nan)
# GH#55264
msg = "is_period is deprecated and will be removed in a future version"
with tm.assert_produces_warning(FutureWarning, match=msg):
assert lib.is_period(Period("2011-01", freq="M"))
assert not lib.is_period(PeriodIndex(["2011-01"], freq="M"))
assert not lib.is_period(Timestamp("2011-01"))
assert not lib.is_period(1)
assert not lib.is_period(np.nan)

def test_is_interval(self):
# GH#55264
msg = "is_interval is deprecated and will be removed in a future version"
item = Interval(1, 2)
with tm.assert_produces_warning(FutureWarning, match=msg):
assert lib.is_interval(item)
assert not lib.is_interval(pd.IntervalIndex([item]))
assert not lib.is_interval(pd.IntervalIndex([item])._engine)

def test_categorical(self):
# GH 8974
Expand Down

0 comments on commit 1230529

Please sign in to comment.