Skip to content

Commit 1230529

Browse files
authored
DEPR: is_period, is_interval (#56038)
1 parent b348eac commit 1230529

File tree

5 files changed

+54
-13
lines changed

5 files changed

+54
-13
lines changed

doc/source/whatsnew/v2.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ For example:
262262
Other Deprecations
263263
^^^^^^^^^^^^^^^^^^
264264
- 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`)
265+
- 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`)
265266
- 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`)
266267
- 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`)
267268
- Deprecated :meth:`Index.format`, use ``index.astype(str)`` or ``index.map(formatter)`` instead (:issue:`55413`)

pandas/_libs/lib.pyx

+27-4
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ from numpy cimport (
6565
)
6666

6767
cnp.import_array()
68+
from pandas._libs.interval import Interval
6869

6970

7071
cdef extern from "pandas/parser/pd_parser.h":
@@ -228,7 +229,7 @@ def is_scalar(val: object) -> bool:
228229
# Note: PyNumber_Check check includes Decimal, Fraction, numbers.Number
229230
return (PyNumber_Check(val)
230231
or is_period_object(val)
231-
or is_interval(val)
232+
or isinstance(val, Interval)
232233
or is_offset_object(val))
233234

234235

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

11621163

11631164
cpdef bint is_interval(object obj):
1165+
import warnings
1166+
1167+
from pandas.util._exceptions import find_stack_level
1168+
1169+
warnings.warn(
1170+
# GH#55264
1171+
"is_interval is deprecated and will be removed in a future version. "
1172+
"Use isinstance(obj, pd.Interval) instead.",
1173+
FutureWarning,
1174+
stacklevel=find_stack_level(),
1175+
)
11641176
return getattr(obj, "_typ", "_typ") == "interval"
11651177

11661178

@@ -1172,6 +1184,17 @@ def is_period(val: object) -> bool:
11721184
-------
11731185
bool
11741186
"""
1187+
import warnings
1188+
1189+
from pandas.util._exceptions import find_stack_level
1190+
1191+
warnings.warn(
1192+
# GH#55264
1193+
"is_period is deprecated and will be removed in a future version. "
1194+
"Use isinstance(obj, pd.Period) instead.",
1195+
FutureWarning,
1196+
stacklevel=find_stack_level(),
1197+
)
11751198
return is_period_object(val)
11761199

11771200

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

1697-
elif is_interval(val):
1720+
elif isinstance(val, Interval):
16981721
if is_interval_array(values):
16991722
return "interval"
17001723

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

2172-
if is_interval(val):
2195+
if isinstance(val, Interval):
21732196
if closed is None:
21742197
closed = val.closed
21752198
numeric = (
@@ -2630,7 +2653,7 @@ def maybe_convert_objects(ndarray[object] objects,
26302653
except (ValueError, TypeError):
26312654
seen.object_ = True
26322655
break
2633-
elif is_interval(val):
2656+
elif isinstance(val, Interval):
26342657
if convert_non_numeric:
26352658
seen.interval_ = True
26362659
break

pandas/core/dtypes/cast.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020

2121
from pandas._config import using_pyarrow_string_dtype
2222

23-
from pandas._libs import lib
23+
from pandas._libs import (
24+
Interval,
25+
Period,
26+
lib,
27+
)
2428
from pandas._libs.missing import (
2529
NA,
2630
NAType,
@@ -850,9 +854,9 @@ def infer_dtype_from_scalar(val) -> tuple[DtypeObj, Any]:
850854
elif is_complex(val):
851855
dtype = np.dtype(np.complex128)
852856

853-
if lib.is_period(val):
857+
if isinstance(val, Period):
854858
dtype = PeriodDtype(freq=val.freq)
855-
elif lib.is_interval(val):
859+
elif isinstance(val, Interval):
856860
subtype = infer_dtype_from_scalar(val.left)[0]
857861
dtype = IntervalDtype(subtype=subtype, closed=val.closed)
858862

pandas/core/indexes/interval.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
)
2323
from pandas._libs.tslibs import (
2424
BaseOffset,
25+
Period,
2526
Timedelta,
2627
Timestamp,
2728
to_offset,
@@ -561,7 +562,7 @@ def _maybe_convert_i8(self, key):
561562
if scalar:
562563
# Timestamp/Timedelta
563564
key_dtype, key_i8 = infer_dtype_from_scalar(key)
564-
if lib.is_period(key):
565+
if isinstance(key, Period):
565566
key_i8 = key.ordinal
566567
elif isinstance(key_i8, Timestamp):
567568
key_i8 = key_i8._value

pandas/tests/dtypes/test_inference.py

+17-5
Original file line numberDiff line numberDiff line change
@@ -1622,11 +1622,23 @@ def test_to_object_array_width(self):
16221622
tm.assert_numpy_array_equal(out, expected)
16231623

16241624
def test_is_period(self):
1625-
assert lib.is_period(Period("2011-01", freq="M"))
1626-
assert not lib.is_period(PeriodIndex(["2011-01"], freq="M"))
1627-
assert not lib.is_period(Timestamp("2011-01"))
1628-
assert not lib.is_period(1)
1629-
assert not lib.is_period(np.nan)
1625+
# GH#55264
1626+
msg = "is_period is deprecated and will be removed in a future version"
1627+
with tm.assert_produces_warning(FutureWarning, match=msg):
1628+
assert lib.is_period(Period("2011-01", freq="M"))
1629+
assert not lib.is_period(PeriodIndex(["2011-01"], freq="M"))
1630+
assert not lib.is_period(Timestamp("2011-01"))
1631+
assert not lib.is_period(1)
1632+
assert not lib.is_period(np.nan)
1633+
1634+
def test_is_interval(self):
1635+
# GH#55264
1636+
msg = "is_interval is deprecated and will be removed in a future version"
1637+
item = Interval(1, 2)
1638+
with tm.assert_produces_warning(FutureWarning, match=msg):
1639+
assert lib.is_interval(item)
1640+
assert not lib.is_interval(pd.IntervalIndex([item]))
1641+
assert not lib.is_interval(pd.IntervalIndex([item])._engine)
16301642

16311643
def test_categorical(self):
16321644
# GH 8974

0 commit comments

Comments
 (0)