Skip to content

Commit 1c1ddf8

Browse files
jbrockmendelmroeschke
authored andcommitted
DEPR: Index methods, to_time, Categorical constructor (pandas-dev#49457)
* DEPR: Index methods, to_time, Categorical constructor * update doc * Update doc/source/whatsnew/v2.0.0.rst Co-authored-by: Matthew Roeschke <[email protected]> Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 5492e71 commit 1c1ddf8

File tree

20 files changed

+27
-211
lines changed

20 files changed

+27
-211
lines changed

asv_bench/benchmarks/index_cached_properties.py

-3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,3 @@ def time_engine(self, index_type):
7070

7171
def time_inferred_type(self, index_type):
7272
self.idx.inferred_type
73-
74-
def time_is_all_dates(self, index_type):
75-
self.idx.is_all_dates

doc/redirects.csv

-3
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,6 @@ generated/pandas.Index.identical,../reference/api/pandas.Index.identical
661661
generated/pandas.Index.inferred_type,../reference/api/pandas.Index.inferred_type
662662
generated/pandas.Index.insert,../reference/api/pandas.Index.insert
663663
generated/pandas.Index.intersection,../reference/api/pandas.Index.intersection
664-
generated/pandas.Index.is_all_dates,../reference/api/pandas.Index.is_all_dates
665664
generated/pandas.Index.is_boolean,../reference/api/pandas.Index.is_boolean
666665
generated/pandas.Index.is_categorical,../reference/api/pandas.Index.is_categorical
667666
generated/pandas.Index.is_floating,../reference/api/pandas.Index.is_floating
@@ -671,7 +670,6 @@ generated/pandas.Index.is_integer,../reference/api/pandas.Index.is_integer
671670
generated/pandas.Index.is_interval,../reference/api/pandas.Index.is_interval
672671
generated/pandas.Index.is_lexsorted_for_tuple,../reference/api/pandas.Index.is_lexsorted_for_tuple
673672
generated/pandas.Index.is_monotonic_decreasing,../reference/api/pandas.Index.is_monotonic_decreasing
674-
generated/pandas.Index.is_monotonic,../reference/api/pandas.Index.is_monotonic
675673
generated/pandas.Index.is_monotonic_increasing,../reference/api/pandas.Index.is_monotonic_increasing
676674
generated/pandas.Index.isna,../reference/api/pandas.Index.isna
677675
generated/pandas.Index.isnull,../reference/api/pandas.Index.isnull
@@ -1068,7 +1066,6 @@ generated/pandas.Series.interpolate,../reference/api/pandas.Series.interpolate
10681066
generated/pandas.Series.is_copy,../reference/api/pandas.Series.is_copy
10691067
generated/pandas.Series.isin,../reference/api/pandas.Series.isin
10701068
generated/pandas.Series.is_monotonic_decreasing,../reference/api/pandas.Series.is_monotonic_decreasing
1071-
generated/pandas.Series.is_monotonic,../reference/api/pandas.Series.is_monotonic
10721069
generated/pandas.Series.is_monotonic_increasing,../reference/api/pandas.Series.is_monotonic_increasing
10731070
generated/pandas.Series.isna,../reference/api/pandas.Series.isna
10741071
generated/pandas.Series.isnull,../reference/api/pandas.Series.isnull

doc/source/reference/indexing.rst

-2
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,13 @@ Properties
2525
:toctree: api/
2626

2727
Index.values
28-
Index.is_monotonic
2928
Index.is_monotonic_increasing
3029
Index.is_monotonic_decreasing
3130
Index.is_unique
3231
Index.has_duplicates
3332
Index.hasnans
3433
Index.dtype
3534
Index.inferred_type
36-
Index.is_all_dates
3735
Index.shape
3836
Index.name
3937
Index.names

doc/source/reference/series.rst

-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ Computations / descriptive stats
167167
Series.unique
168168
Series.nunique
169169
Series.is_unique
170-
Series.is_monotonic
171170
Series.is_monotonic_increasing
172171
Series.is_monotonic_decreasing
173172
Series.value_counts

doc/source/whatsnew/v2.0.0.rst

+7
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ Removal of prior version deprecations/changes
167167
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
168168
- Removed deprecated :class:`CategoricalBlock`, :meth:`Block.is_categorical`, require datetime64 and timedelta64 values to be wrapped in :class:`DatetimeArray` or :class:`TimedeltaArray` before passing to :meth:`Block.make_block_same_class`, require ``DatetimeTZBlock.values`` to have the correct ndim when passing to the :class:`BlockManager` constructor, and removed the "fastpath" keyword from the :class:`SingleBlockManager` constructor (:issue:`40226`, :issue:`40571`)
169169
- Removed deprecated module ``pandas.core.index`` (:issue:`30193`)
170+
- Removed deprecated alias ``pandas.core.tools.datetimes.to_time``, import the function directly from ``pandas.core.tools.times`` instead (:issue:`34145`)
170171
- Removed deprecated :meth:`Categorical.to_dense`, use ``np.asarray(cat)`` instead (:issue:`32639`)
171172
- Removed deprecated :meth:`Categorical.take_nd` (:issue:`27745`)
172173
- Removed deprecated :meth:`Categorical.mode`, use ``Series(cat).mode()`` instead (:issue:`45033`)
@@ -191,6 +192,8 @@ Removal of prior version deprecations/changes
191192
- Removed deprecated argument ``null_color`` in :meth:`.Styler.highlight_null` (:issue:`49397`)
192193
- Removed deprecated argument ``check_less_precise`` in :meth:`.testing.assert_frame_equal`, :meth:`.testing.assert_extension_array_equal`, :meth:`.testing.assert_series_equal`, :meth:`.testing.assert_index_equal` (:issue:`30562`)
193194
- Removed deprecated ``null_counts`` argument in :meth:`DataFrame.info`. Use ``show_counts`` instead (:issue:`37999`)
195+
- Removed deprecated :meth:`Index.is_monotonic`, and :meth:`Series.is_monotonic`; use ``obj.is_monotonic_increasing`` instead (:issue:`45422`)
196+
- Removed deprecated :meth:`Index.is_all_dates` (:issue:`36697`)
194197
- Enforced deprecation disallowing passing a timezone-aware :class:`Timestamp` and ``dtype="datetime64[ns]"`` to :class:`Series` or :class:`DataFrame` constructors (:issue:`41555`)
195198
- Enforced deprecation disallowing passing a sequence of timezone-aware values and ``dtype="datetime64[ns]"`` to to :class:`Series` or :class:`DataFrame` constructors (:issue:`41555`)
196199
- Enforced deprecation disallowing unit-less "datetime64" dtype in :meth:`Series.astype` and :meth:`DataFrame.astype` (:issue:`47844`)
@@ -258,6 +261,8 @@ Removal of prior version deprecations/changes
258261
- Disallow passing non-keyword arguments to :meth:`DataFrame.replace`, :meth:`Series.replace` except for ``to_replace`` and ``value`` (:issue:`47587`)
259262
- Disallow passing non-keyword arguments to :meth:`DataFrame.sort_values` except for ``by`` (:issue:`41505`)
260263
- Disallow passing non-keyword arguments to :meth:`Series.sort_values` (:issue:`41505`)
264+
- Disallowed constructing :class:`Categorical` with scalar ``data`` (:issue:`38433`)
265+
- Disallowed constructing :class:`CategoricalIndex` without passing ``data`` (:issue:`38944`)
261266
- Removed :meth:`.Rolling.validate`, :meth:`.Expanding.validate`, and :meth:`.ExponentialMovingWindow.validate` (:issue:`43665`)
262267
- Removed :attr:`Rolling.win_type` returning ``"freq"`` (:issue:`38963`)
263268
- Removed :attr:`Rolling.is_datetimelike` (:issue:`38963`)
@@ -306,6 +311,8 @@ Removal of prior version deprecations/changes
306311
- Changed the behavior of :meth:`DatetimeIndex.union`, :meth:`DatetimeIndex.intersection`, and :meth:`DatetimeIndex.symmetric_difference` with mismatched timezones to convert to UTC instead of casting to object dtype (:issue:`39328`)
307312
- Changed the behavior of :func:`to_datetime` with argument "now" with ``utc=False`` to match ``Timestamp("now")`` (:issue:`18705`)
308313
- Changed behavior of :meth:`SparseArray.astype` when given a dtype that is not explicitly ``SparseDtype``, cast to the exact requested dtype rather than silently using a ``SparseDtype`` instead (:issue:`34457`)
314+
- Changed behavior of :meth:`Index.ravel` to return a view on the original :class:`Index` instead of a ``np.ndarray`` (:issue:`36900`)
315+
- Changed behavior of :meth:`Index.to_frame` with explicit ``name=None`` to use ``None`` for the column name instead of the index's name or default ``0`` (:issue:`45523`)
309316
- Changed behavior of :class:`DataFrame` constructor given floating-point ``data`` and an integer ``dtype``, when the data cannot be cast losslessly, the floating point dtype is retained, matching :class:`Series` behavior (:issue:`41170`)
310317
- Changed behavior of :class:`Index` constructor when given a ``np.ndarray`` with object-dtype containing numeric entries; this now retains object dtype rather than inferring a numeric dtype, consistent with :class:`Series` behavior (:issue:`42870`)
311318
- Changed behavior of :class:`DataFrame` constructor when passed a ``dtype`` (other than int) that the data cannot be cast to; it now raises instead of silently ignoring the dtype (:issue:`41733`)

pandas/core/arrays/categorical.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
cast,
1616
overload,
1717
)
18-
from warnings import warn
1918

2019
import numpy as np
2120

@@ -40,7 +39,6 @@
4039
type_t,
4140
)
4241
from pandas.compat.numpy import function as nv
43-
from pandas.util._exceptions import find_stack_level
4442
from pandas.util._validators import validate_bool_kwarg
4543

4644
from pandas.core.dtypes.cast import (
@@ -384,13 +382,7 @@ def __init__(
384382

385383
if not is_list_like(values):
386384
# GH#38433
387-
warn(
388-
"Allowing scalars in the Categorical constructor is deprecated "
389-
"and will raise in a future version. Use `[value]` instead",
390-
FutureWarning,
391-
stacklevel=find_stack_level(),
392-
)
393-
values = [values]
385+
raise TypeError("Categorical input must be list-like")
394386

395387
# null_mask indicates missing values we want to exclude from inference.
396388
# This means: only missing values in list-likes (not arrays/ndframes).

pandas/core/base.py

-23
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
final,
1818
overload,
1919
)
20-
import warnings
2120

2221
import numpy as np
2322

@@ -38,7 +37,6 @@
3837
cache_readonly,
3938
doc,
4039
)
41-
from pandas.util._exceptions import find_stack_level
4240

4341
from pandas.core.dtypes.common import (
4442
is_categorical_dtype,
@@ -1062,27 +1060,6 @@ def is_unique(self) -> bool:
10621060
"""
10631061
return self.nunique(dropna=False) == len(self)
10641062

1065-
@property
1066-
def is_monotonic(self) -> bool:
1067-
"""
1068-
Return boolean if values in the object are monotonically increasing.
1069-
1070-
.. deprecated:: 1.5.0
1071-
is_monotonic is deprecated and will be removed in a future version.
1072-
Use is_monotonic_increasing instead.
1073-
1074-
Returns
1075-
-------
1076-
bool
1077-
"""
1078-
warnings.warn(
1079-
"is_monotonic is deprecated and will be removed in a future version. "
1080-
"Use is_monotonic_increasing instead.",
1081-
FutureWarning,
1082-
stacklevel=find_stack_level(),
1083-
)
1084-
return self.is_monotonic_increasing
1085-
10861063
@property
10871064
def is_monotonic_increasing(self) -> bool:
10881065
"""

pandas/core/indexes/base.py

+5-63
Original file line numberDiff line numberDiff line change
@@ -938,34 +938,19 @@ def dtype(self) -> DtypeObj:
938938
return self._data.dtype
939939

940940
@final
941-
def ravel(self, order: str_t = "C"):
941+
def ravel(self, order: str_t = "C") -> Index:
942942
"""
943-
Return an ndarray of the flattened values of the underlying data.
943+
Return a view on self.
944944
945945
Returns
946946
-------
947-
numpy.ndarray
948-
Flattened array.
947+
Index
949948
950949
See Also
951950
--------
952951
numpy.ndarray.ravel : Return a flattened array.
953952
"""
954-
warnings.warn(
955-
"Index.ravel returning ndarray is deprecated; in a future version "
956-
"this will return a view on self.",
957-
FutureWarning,
958-
stacklevel=find_stack_level(),
959-
)
960-
if needs_i8_conversion(self.dtype):
961-
# Item "ndarray[Any, Any]" of "Union[ExtensionArray, ndarray[Any, Any]]"
962-
# has no attribute "_ndarray"
963-
values = self._data._ndarray # type: ignore[union-attr]
964-
elif is_interval_dtype(self.dtype):
965-
values = np.asarray(self._data)
966-
else:
967-
values = self._get_engine_target()
968-
return values.ravel(order=order)
953+
return self[:]
969954

970955
def view(self, cls=None):
971956

@@ -1552,7 +1537,7 @@ def to_frame(
15521537
index : bool, default True
15531538
Set the index of the returned DataFrame as the original Index.
15541539
1555-
name : object, default None
1540+
name : object, defaults to index.name
15561541
The passed name should substitute for the index name (if it has
15571542
one).
15581543
@@ -1594,17 +1579,6 @@ def to_frame(
15941579
"""
15951580
from pandas import DataFrame
15961581

1597-
if name is None:
1598-
warnings.warn(
1599-
"Explicitly passing `name=None` currently preserves the Index's name "
1600-
"or uses a default name of 0. This behaviour is deprecated, and in "
1601-
"the future `None` will be used as the name of the resulting "
1602-
"DataFrame column.",
1603-
FutureWarning,
1604-
stacklevel=find_stack_level(),
1605-
)
1606-
name = lib.no_default
1607-
16081582
if name is lib.no_default:
16091583
name = self._get_level_names()
16101584
result = DataFrame({name: self._values.copy()})
@@ -2197,24 +2171,6 @@ def _can_hold_na(self) -> bool:
21972171
return False
21982172
return True
21992173

2200-
@final
2201-
@property
2202-
def is_monotonic(self) -> bool:
2203-
"""
2204-
Alias for is_monotonic_increasing.
2205-
2206-
.. deprecated:: 1.5.0
2207-
is_monotonic is deprecated and will be removed in a future version.
2208-
Use is_monotonic_increasing instead.
2209-
"""
2210-
warnings.warn(
2211-
"is_monotonic is deprecated and will be removed in a future version. "
2212-
"Use is_monotonic_increasing instead.",
2213-
FutureWarning,
2214-
stacklevel=find_stack_level(),
2215-
)
2216-
return self.is_monotonic_increasing
2217-
22182174
@property
22192175
def is_monotonic_increasing(self) -> bool:
22202176
"""
@@ -2626,20 +2582,6 @@ def _is_all_dates(self) -> bool:
26262582
return False
26272583
return is_datetime_array(ensure_object(self._values))
26282584

2629-
@cache_readonly
2630-
@final
2631-
def is_all_dates(self) -> bool:
2632-
"""
2633-
Whether or not the index values only consist of dates.
2634-
"""
2635-
warnings.warn(
2636-
"Index.is_all_dates is deprecated, will be removed in a future version. "
2637-
"check index.inferred_type instead.",
2638-
FutureWarning,
2639-
stacklevel=find_stack_level(),
2640-
)
2641-
return self._is_all_dates
2642-
26432585
@final
26442586
@cache_readonly
26452587
def _is_multi(self) -> bool:

pandas/core/indexes/category.py

+1-11
Original file line numberDiff line numberDiff line change
@@ -216,18 +216,8 @@ def __new__(
216216

217217
name = maybe_extract_name(name, data, cls)
218218

219-
if data is None:
220-
# GH#38944
221-
warnings.warn(
222-
"Constructing a CategoricalIndex without passing data is "
223-
"deprecated and will raise in a future version. "
224-
"Use CategoricalIndex([], ...) instead.",
225-
FutureWarning,
226-
stacklevel=find_stack_level(),
227-
)
228-
data = []
229-
230219
if is_scalar(data):
220+
# GH#38944 include None here, which pre-2.0 subbed in []
231221
cls._raise_scalar_data_error(data)
232222

233223
data = Categorical(

pandas/core/indexes/multi.py

-11
Original file line numberDiff line numberDiff line change
@@ -1738,17 +1738,6 @@ def to_frame(
17381738
"""
17391739
from pandas import DataFrame
17401740

1741-
if name is None:
1742-
warnings.warn(
1743-
"Explicitly passing `name=None` currently preserves the Index's name "
1744-
"or uses a default name of 0. This behaviour is deprecated, and in "
1745-
"the future `None` will be used as the name of the resulting "
1746-
"DataFrame column.",
1747-
FutureWarning,
1748-
stacklevel=find_stack_level(),
1749-
)
1750-
name = lib.no_default
1751-
17521741
if name is not lib.no_default:
17531742
if not is_list_like(name):
17541743
raise TypeError("'name' must be a list / sequence of column names.")

pandas/core/tools/datetimes.py

-21
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
cast,
1616
overload,
1717
)
18-
import warnings
1918

2019
import numpy as np
2120

@@ -42,7 +41,6 @@
4241
Timezone,
4342
npt,
4443
)
45-
from pandas.util._exceptions import find_stack_level
4644

4745
from pandas.core.dtypes.common import (
4846
ensure_object,
@@ -1294,27 +1292,8 @@ def calc_with_mask(carg, mask):
12941292
return None
12951293

12961294

1297-
def to_time(
1298-
arg,
1299-
format=None,
1300-
infer_time_format: bool = False,
1301-
errors: DateTimeErrorChoices = "raise",
1302-
):
1303-
# GH#34145
1304-
warnings.warn(
1305-
"`to_time` has been moved, should be imported from pandas.core.tools.times. "
1306-
"This alias will be removed in a future version.",
1307-
FutureWarning,
1308-
stacklevel=find_stack_level(),
1309-
)
1310-
from pandas.core.tools.times import to_time
1311-
1312-
return to_time(arg, format, infer_time_format, errors)
1313-
1314-
13151295
__all__ = [
13161296
"DateParseError",
13171297
"should_cache",
13181298
"to_datetime",
1319-
"to_time",
13201299
]

pandas/tests/arrays/categorical/test_constructors.py

+2-9
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333

3434

3535
class TestCategoricalConstructors:
36-
def test_categorical_scalar_deprecated(self):
36+
def test_categorical_disallows_scalar(self):
3737
# GH#38433
38-
with tm.assert_produces_warning(FutureWarning):
38+
with pytest.raises(TypeError, match="Categorical input must be list-like"):
3939
Categorical("A", categories=["A", "B"])
4040

4141
def test_categorical_1d_only(self):
@@ -220,13 +220,6 @@ def test_constructor(self):
220220
assert len(cat.codes) == 1
221221
assert cat.codes[0] == 0
222222

223-
with tm.assert_produces_warning(FutureWarning):
224-
# GH#38433
225-
cat = Categorical(1)
226-
assert len(cat.categories) == 1
227-
assert cat.categories[0] == 1
228-
assert len(cat.codes) == 1
229-
assert cat.codes[0] == 0
230223
# two arrays
231224
# - when the first is an integer dtype and the second is not
232225
# - when the resulting codes are all -1/NaN

pandas/tests/indexes/categorical/test_category.py

-5
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ def test_can_hold_identifiers(self):
3838
key = idx[0]
3939
assert idx._can_hold_identifiers_and_holds_name(key) is True
4040

41-
def test_pickle_compat_construction(self):
42-
# Once the deprecation is enforced, we can use the parent class's test
43-
with tm.assert_produces_warning(FutureWarning, match="without passing data"):
44-
self._index_cls()
45-
4641
def test_insert(self, simple_index):
4742

4843
ci = simple_index

0 commit comments

Comments
 (0)