Skip to content

DEPR: Index methods, to_time, Categorical constructor #49457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions asv_bench/benchmarks/index_cached_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,3 @@ def time_engine(self, index_type):

def time_inferred_type(self, index_type):
self.idx.inferred_type

def time_is_all_dates(self, index_type):
self.idx.is_all_dates
3 changes: 0 additions & 3 deletions doc/redirects.csv
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,6 @@ generated/pandas.Index.identical,../reference/api/pandas.Index.identical
generated/pandas.Index.inferred_type,../reference/api/pandas.Index.inferred_type
generated/pandas.Index.insert,../reference/api/pandas.Index.insert
generated/pandas.Index.intersection,../reference/api/pandas.Index.intersection
generated/pandas.Index.is_all_dates,../reference/api/pandas.Index.is_all_dates
generated/pandas.Index.is_boolean,../reference/api/pandas.Index.is_boolean
generated/pandas.Index.is_categorical,../reference/api/pandas.Index.is_categorical
generated/pandas.Index.is_floating,../reference/api/pandas.Index.is_floating
Expand All @@ -671,7 +670,6 @@ generated/pandas.Index.is_integer,../reference/api/pandas.Index.is_integer
generated/pandas.Index.is_interval,../reference/api/pandas.Index.is_interval
generated/pandas.Index.is_lexsorted_for_tuple,../reference/api/pandas.Index.is_lexsorted_for_tuple
generated/pandas.Index.is_monotonic_decreasing,../reference/api/pandas.Index.is_monotonic_decreasing
generated/pandas.Index.is_monotonic,../reference/api/pandas.Index.is_monotonic
generated/pandas.Index.is_monotonic_increasing,../reference/api/pandas.Index.is_monotonic_increasing
generated/pandas.Index.isna,../reference/api/pandas.Index.isna
generated/pandas.Index.isnull,../reference/api/pandas.Index.isnull
Expand Down Expand Up @@ -1068,7 +1066,6 @@ generated/pandas.Series.interpolate,../reference/api/pandas.Series.interpolate
generated/pandas.Series.is_copy,../reference/api/pandas.Series.is_copy
generated/pandas.Series.isin,../reference/api/pandas.Series.isin
generated/pandas.Series.is_monotonic_decreasing,../reference/api/pandas.Series.is_monotonic_decreasing
generated/pandas.Series.is_monotonic,../reference/api/pandas.Series.is_monotonic
generated/pandas.Series.is_monotonic_increasing,../reference/api/pandas.Series.is_monotonic_increasing
generated/pandas.Series.isna,../reference/api/pandas.Series.isna
generated/pandas.Series.isnull,../reference/api/pandas.Series.isnull
Expand Down
2 changes: 0 additions & 2 deletions doc/source/reference/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ Properties
:toctree: api/

Index.values
Index.is_monotonic
Index.is_monotonic_increasing
Index.is_monotonic_decreasing
Index.is_unique
Index.has_duplicates
Index.hasnans
Index.dtype
Index.inferred_type
Index.is_all_dates
Index.shape
Index.name
Index.names
Expand Down
1 change: 0 additions & 1 deletion doc/source/reference/series.rst
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ Computations / descriptive stats
Series.unique
Series.nunique
Series.is_unique
Series.is_monotonic
Series.is_monotonic_increasing
Series.is_monotonic_decreasing
Series.value_counts
Expand Down
7 changes: 7 additions & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ Deprecations
Removal of prior version deprecations/changes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Removed deprecated module ``pandas.core.index`` (:issue:`30193`)
- Removed deprecated alias ``pandas.core.tools.datetimes.to_time``, import the function directly from ``pandas.core.tools.times`` instead (:issue:`34145`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose we should move to_time to the top level eventually

- Removed deprecated :meth:`Categorical.to_dense`, use ``np.asarray(cat)`` instead (:issue:`32639`)
- Removed deprecated :meth:`Categorical.take_nd` (:issue:`27745`)
- Removed deprecated :meth:`Categorical.mode`, use ``Series(cat).mode()`` instead (:issue:`45033`)
Expand All @@ -182,6 +183,8 @@ Removal of prior version deprecations/changes
- Removed deprecated :meth:`.Styler.render` (:issue:`49397`)
- Removed deprecated argument ``null_color`` in :meth:`.Styler.highlight_null` (:issue:`49397`)
- Removed deprecated ``null_counts`` argument in :meth:`DataFrame.info`. Use ``show_counts`` instead (:issue:`37999`)
- Removed deprecated :meth:`Index.is_monotonic`, and :meth:`Series.is_monotonic`; use ``obj.is_monotonic_increasing`` instead (:issue:`45422`)
- Removed deprecated :meth:`Index.is_all_dates` (:issue:`36697`)
- Enforced deprecation disallowing passing a timezone-aware :class:`Timestamp` and ``dtype="datetime64[ns]"`` to :class:`Series` or :class:`DataFrame` constructors (:issue:`41555`)
- Enforced deprecation disallowing passing a sequence of timezone-aware values and ``dtype="datetime64[ns]"`` to to :class:`Series` or :class:`DataFrame` constructors (:issue:`41555`)
- Enforced deprecation disallowing unit-less "datetime64" dtype in :meth:`Series.astype` and :meth:`DataFrame.astype` (:issue:`47844`)
Expand Down Expand Up @@ -245,6 +248,8 @@ Removal of prior version deprecations/changes
- Disallow passing non-keyword arguments to :meth:`DataFrame.replace`, :meth:`Series.replace` except for ``to_replace`` and ``value`` (:issue:`47587`)
- Disallow passing non-keyword arguments to :meth:`DataFrame.sort_values` except for ``by`` (:issue:`41505`)
- Disallow passing non-keyword arguments to :meth:`Series.sort_values` (:issue:`41505`)
- Disallowed constructing :class:`Categorical` with scalar ``data`` (:issue:`38433`)
- Disallowed constructing :class:`CategoricalIndex` without passing ``data`` (:issue:`38944`)
- Removed :meth:`.Rolling.validate`, :meth:`.Expanding.validate`, and :meth:`.ExponentialMovingWindow.validate` (:issue:`43665`)
- Removed :attr:`Rolling.win_type` returning ``"freq"`` (:issue:`38963`)
- Removed :attr:`Rolling.is_datetimelike` (:issue:`38963`)
Expand Down Expand Up @@ -286,6 +291,8 @@ Removal of prior version deprecations/changes
- Changed behavior of empty data passed into :class:`Series`; the default dtype will be ``object`` instead of ``float64`` (:issue:`29405`)
- Changed the behavior of :func:`to_datetime` with argument "now" with ``utc=False`` to match ``Timestamp("now")`` (:issue:`18705`)
- 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`)
- Changed behavior of :meth:`Index.ravel` to return a view on the original :class:`Index` instead of a ``np.ndarray`` (:issue:`36900`)
- 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`)
- 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`)
- 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`)
- Changed the behavior of :class:`Series` constructor, it will no longer infer a datetime64 or timedelta64 dtype from string entries (:issue:`41731`)
Expand Down
10 changes: 1 addition & 9 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
cast,
overload,
)
from warnings import warn

import numpy as np

Expand All @@ -40,7 +39,6 @@
type_t,
)
from pandas.compat.numpy import function as nv
from pandas.util._exceptions import find_stack_level
from pandas.util._validators import validate_bool_kwarg

from pandas.core.dtypes.cast import (
Expand Down Expand Up @@ -384,13 +382,7 @@ def __init__(

if not is_list_like(values):
# GH#38433
warn(
"Allowing scalars in the Categorical constructor is deprecated "
"and will raise in a future version. Use `[value]` instead",
FutureWarning,
stacklevel=find_stack_level(),
)
values = [values]
raise TypeError("Categorical input must be list-like")

# null_mask indicates missing values we want to exclude from inference.
# This means: only missing values in list-likes (not arrays/ndframes).
Expand Down
23 changes: 0 additions & 23 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
final,
overload,
)
import warnings

import numpy as np

Expand All @@ -38,7 +37,6 @@
cache_readonly,
doc,
)
from pandas.util._exceptions import find_stack_level

from pandas.core.dtypes.common import (
is_categorical_dtype,
Expand Down Expand Up @@ -1056,27 +1054,6 @@ def is_unique(self) -> bool:
"""
return self.nunique(dropna=False) == len(self)

@property
def is_monotonic(self) -> bool:
"""
Return boolean if values in the object are monotonically increasing.

.. deprecated:: 1.5.0
is_monotonic is deprecated and will be removed in a future version.
Use is_monotonic_increasing instead.

Returns
-------
bool
"""
warnings.warn(
"is_monotonic is deprecated and will be removed in a future version. "
"Use is_monotonic_increasing instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
return self.is_monotonic_increasing

@property
def is_monotonic_increasing(self) -> bool:
"""
Expand Down
68 changes: 5 additions & 63 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -941,34 +941,19 @@ def dtype(self) -> DtypeObj:
return self._data.dtype

@final
def ravel(self, order: str_t = "C"):
def ravel(self, order: str_t = "C") -> Index:
"""
Return an ndarray of the flattened values of the underlying data.
Return a view on self.

Returns
-------
numpy.ndarray
Flattened array.
Index

See Also
--------
numpy.ndarray.ravel : Return a flattened array.
"""
warnings.warn(
"Index.ravel returning ndarray is deprecated; in a future version "
"this will return a view on self.",
FutureWarning,
stacklevel=find_stack_level(),
)
if needs_i8_conversion(self.dtype):
# Item "ndarray[Any, Any]" of "Union[ExtensionArray, ndarray[Any, Any]]"
# has no attribute "_ndarray"
values = self._data._ndarray # type: ignore[union-attr]
elif is_interval_dtype(self.dtype):
values = np.asarray(self._data)
else:
values = self._get_engine_target()
return values.ravel(order=order)
return self[:]

def view(self, cls=None):

Expand Down Expand Up @@ -1555,7 +1540,7 @@ def to_frame(
index : bool, default True
Set the index of the returned DataFrame as the original Index.

name : object, default None
name : object, defaults to index.name
The passed name should substitute for the index name (if it has
one).

Expand Down Expand Up @@ -1597,17 +1582,6 @@ def to_frame(
"""
from pandas import DataFrame

if name is None:
warnings.warn(
"Explicitly passing `name=None` currently preserves the Index's name "
"or uses a default name of 0. This behaviour is deprecated, and in "
"the future `None` will be used as the name of the resulting "
"DataFrame column.",
FutureWarning,
stacklevel=find_stack_level(),
)
name = lib.no_default

if name is lib.no_default:
name = self._get_level_names()
result = DataFrame({name: self._values.copy()})
Expand Down Expand Up @@ -2200,24 +2174,6 @@ def _can_hold_na(self) -> bool:
return False
return True

@final
@property
def is_monotonic(self) -> bool:
"""
Alias for is_monotonic_increasing.

.. deprecated:: 1.5.0
is_monotonic is deprecated and will be removed in a future version.
Use is_monotonic_increasing instead.
"""
warnings.warn(
"is_monotonic is deprecated and will be removed in a future version. "
"Use is_monotonic_increasing instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
return self.is_monotonic_increasing

@property
def is_monotonic_increasing(self) -> bool:
"""
Expand Down Expand Up @@ -2629,20 +2585,6 @@ def _is_all_dates(self) -> bool:
return False
return is_datetime_array(ensure_object(self._values))

@cache_readonly
@final
def is_all_dates(self) -> bool:
"""
Whether or not the index values only consist of dates.
"""
warnings.warn(
"Index.is_all_dates is deprecated, will be removed in a future version. "
"check index.inferred_type instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
return self._is_all_dates

@final
@cache_readonly
def _is_multi(self) -> bool:
Expand Down
12 changes: 1 addition & 11 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,8 @@ def __new__(

name = maybe_extract_name(name, data, cls)

if data is None:
# GH#38944
warnings.warn(
"Constructing a CategoricalIndex without passing data is "
"deprecated and will raise in a future version. "
"Use CategoricalIndex([], ...) instead.",
FutureWarning,
stacklevel=find_stack_level(),
)
data = []

if is_scalar(data):
# GH#38944 include None here, which pre-2.0 subbed in []
cls._raise_scalar_data_error(data)

data = Categorical(
Expand Down
11 changes: 0 additions & 11 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1738,17 +1738,6 @@ def to_frame(
"""
from pandas import DataFrame

if name is None:
warnings.warn(
"Explicitly passing `name=None` currently preserves the Index's name "
"or uses a default name of 0. This behaviour is deprecated, and in "
"the future `None` will be used as the name of the resulting "
"DataFrame column.",
FutureWarning,
stacklevel=find_stack_level(),
)
name = lib.no_default

if name is not lib.no_default:
if not is_list_like(name):
raise TypeError("'name' must be a list / sequence of column names.")
Expand Down
21 changes: 0 additions & 21 deletions pandas/core/tools/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
cast,
overload,
)
import warnings

import numpy as np

Expand All @@ -42,7 +41,6 @@
Timezone,
npt,
)
from pandas.util._exceptions import find_stack_level

from pandas.core.dtypes.common import (
ensure_object,
Expand Down Expand Up @@ -1294,27 +1292,8 @@ def calc_with_mask(carg, mask):
return None


def to_time(
arg,
format=None,
infer_time_format: bool = False,
errors: DateTimeErrorChoices = "raise",
):
# GH#34145
warnings.warn(
"`to_time` has been moved, should be imported from pandas.core.tools.times. "
"This alias will be removed in a future version.",
FutureWarning,
stacklevel=find_stack_level(),
)
from pandas.core.tools.times import to_time

return to_time(arg, format, infer_time_format, errors)


__all__ = [
"DateParseError",
"should_cache",
"to_datetime",
"to_time",
]
11 changes: 2 additions & 9 deletions pandas/tests/arrays/categorical/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@


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

def test_categorical_1d_only(self):
Expand Down Expand Up @@ -220,13 +220,6 @@ def test_constructor(self):
assert len(cat.codes) == 1
assert cat.codes[0] == 0

with tm.assert_produces_warning(FutureWarning):
# GH#38433
cat = Categorical(1)
assert len(cat.categories) == 1
assert cat.categories[0] == 1
assert len(cat.codes) == 1
assert cat.codes[0] == 0
# two arrays
# - when the first is an integer dtype and the second is not
# - when the resulting codes are all -1/NaN
Expand Down
5 changes: 0 additions & 5 deletions pandas/tests/indexes/categorical/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ def test_can_hold_identifiers(self):
key = idx[0]
assert idx._can_hold_identifiers_and_holds_name(key) is True

def test_pickle_compat_construction(self):
# Once the deprecation is enforced, we can use the parent class's test
with tm.assert_produces_warning(FutureWarning, match="without passing data"):
self._index_cls()

def test_insert(self, simple_index):

ci = simple_index
Expand Down
Loading