From ffcf5e5dab56ac487c578491b491100c6070f168 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Fri, 14 Oct 2022 13:30:06 +0200 Subject: [PATCH 1/3] DEP: Enforce deprecation of names and dtype in index copy and codes and levels in midx.copy --- pandas/core/indexes/base.py | 33 ++----------------- pandas/core/indexes/multi.py | 44 ++----------------------- pandas/core/indexes/range.py | 19 ++--------- pandas/tests/indexes/multi/test_copy.py | 21 ------------ pandas/tests/indexes/test_any_index.py | 6 ---- pandas/tests/indexes/test_base.py | 13 -------- 6 files changed, 7 insertions(+), 129 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index cde221c77fe9b..168f88eb9bf32 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -50,7 +50,6 @@ Axes, Axis, DropKeep, - Dtype, DtypeObj, F, IgnoreRaise, @@ -1292,29 +1291,17 @@ def copy( self: _IndexT, name: Hashable | None = None, deep: bool = False, - dtype: Dtype | None = None, - names: Sequence[Hashable] | None = None, ) -> _IndexT: """ Make a copy of this object. - Name and dtype sets those attributes on the new object. + Name is set on the new object. Parameters ---------- name : Label, optional Set name for new object. deep : bool, default False - dtype : numpy dtype or pandas type, optional - Set dtype for new object. - - .. deprecated:: 1.2.0 - use ``astype`` method instead. - names : list-like, optional - Kept for compatibility with MultiIndex. Should not be used. - - .. deprecated:: 1.4.0 - use ``name`` instead. Returns ------- @@ -1326,29 +1313,13 @@ def copy( In most cases, there should be no functional difference from using ``deep``, but if ``deep`` is passed it will attempt to deepcopy. """ - if names is not None: - warnings.warn( - "parameter names is deprecated and will be removed in a future " - "version. Use the name parameter instead.", - FutureWarning, - stacklevel=find_stack_level(), - ) - name = self._validate_names(name=name, names=names, deep=deep)[0] + name = self._validate_names(name=name, deep=deep)[0] if deep: new_data = self._data.copy() new_index = type(self)._simple_new(new_data, name=name) else: new_index = self._rename(name=name) - - if dtype: - warnings.warn( - "parameter dtype is deprecated and will be removed in a future " - "version. Use the astype method instead.", - FutureWarning, - stacklevel=find_stack_level(), - ) - new_index = new_index.astype(dtype) return new_index @final diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 4ee3dcf59b96b..465d194c6b4bf 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -1174,9 +1174,6 @@ def _view(self) -> MultiIndex: def copy( # type: ignore[override] self, names=None, - dtype=None, - levels=None, - codes=None, deep: bool = False, name=None, ): @@ -1187,15 +1184,6 @@ def copy( # type: ignore[override] Parameters ---------- names : sequence, optional - dtype : numpy dtype or pandas type, optional - - .. deprecated:: 1.2.0 - levels : sequence, optional - - .. deprecated:: 1.2.0 - codes : sequence, optional - - .. deprecated:: 1.2.0 deep : bool, default False name : Label Kept for compatibility with 1-dimensional Index. Should not be used. @@ -1212,30 +1200,13 @@ def copy( # type: ignore[override] """ names = self._validate_names(name=name, names=names, deep=deep) keep_id = not deep - if levels is not None: - warnings.warn( - "parameter levels is deprecated and will be removed in a future " - "version. Use the set_levels method instead.", - FutureWarning, - stacklevel=find_stack_level(), - ) - keep_id = False - if codes is not None: - warnings.warn( - "parameter codes is deprecated and will be removed in a future " - "version. Use the set_codes method instead.", - FutureWarning, - stacklevel=find_stack_level(), - ) - keep_id = False + levels, codes = None, None if deep: from copy import deepcopy - if levels is None: - levels = deepcopy(self.levels) - if codes is None: - codes = deepcopy(self.codes) + levels = deepcopy(self.levels) + codes = deepcopy(self.codes) levels = levels if levels is not None else self.levels codes = codes if codes is not None else self.codes @@ -1251,15 +1222,6 @@ def copy( # type: ignore[override] new_index._cache.pop("levels", None) # GH32669 if keep_id: new_index._id = self._id - - if dtype: - warnings.warn( - "parameter dtype is deprecated and will be removed in a future " - "version. Use the astype method instead.", - FutureWarning, - stacklevel=find_stack_level(), - ) - new_index = new_index.astype(dtype) return new_index def __array__(self, dtype=None) -> np.ndarray: diff --git a/pandas/core/indexes/range.py b/pandas/core/indexes/range.py index 3846f565e96f8..64a8d0d1ed54c 100644 --- a/pandas/core/indexes/range.py +++ b/pandas/core/indexes/range.py @@ -456,24 +456,9 @@ def _view(self: RangeIndex) -> RangeIndex: return result @doc(Int64Index.copy) - def copy( - self, - name: Hashable = None, - deep: bool = False, - dtype: Dtype | None = None, - names=None, - ): - name = self._validate_names(name=name, names=names, deep=deep)[0] + def copy(self, name: Hashable = None, deep: bool = False): + name = self._validate_names(name=name, deep=deep)[0] new_index = self._rename(name=name) - - if dtype: - warnings.warn( - "parameter dtype is deprecated and will be removed in a future " - "version. Use the astype method instead.", - FutureWarning, - stacklevel=find_stack_level(), - ) - new_index = new_index.astype(dtype) return new_index def _minmax(self, meth: str): diff --git a/pandas/tests/indexes/multi/test_copy.py b/pandas/tests/indexes/multi/test_copy.py index 2b64845c919cf..e4126d22f247c 100644 --- a/pandas/tests/indexes/multi/test_copy.py +++ b/pandas/tests/indexes/multi/test_copy.py @@ -85,27 +85,6 @@ def test_copy_method_kwargs(deep, kwarg, value): assert getattr(idx_copy, kwarg) == value -@pytest.mark.parametrize("deep", [True, False]) -@pytest.mark.parametrize( - "param_name, param_value", - [ - ("levels", [["foo2", "bar2"], ["fizz2", "buzz2"]]), - ("codes", [[1, 0, 0, 0], [1, 1, 0, 0]]), - ], -) -def test_copy_deprecated_parameters(deep, param_name, param_value): - # gh-36685 - idx = MultiIndex( - levels=[["foo", "bar"], ["fizz", "buzz"]], - codes=[[0, 0, 0, 1], [0, 0, 1, 1]], - names=["first", "second"], - ) - with tm.assert_produces_warning(FutureWarning): - idx_copy = idx.copy(deep=deep, **{param_name: param_value}) - - assert [list(i) for i in getattr(idx_copy, param_name)] == param_value - - def test_copy_deep_false_retains_id(): # GH#47878 idx = MultiIndex( diff --git a/pandas/tests/indexes/test_any_index.py b/pandas/tests/indexes/test_any_index.py index b062dfb7a10ba..1745e5680c721 100644 --- a/pandas/tests/indexes/test_any_index.py +++ b/pandas/tests/indexes/test_any_index.py @@ -32,12 +32,6 @@ def test_hash_error(index): hash(index) -def test_copy_dtype_deprecated(index): - # GH#35853 - with tm.assert_produces_warning(FutureWarning): - index.copy(dtype=object) - - def test_mutability(index): if not len(index): return diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index ac76953c66a24..4b0821a50e09b 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -1305,19 +1305,6 @@ def test_copy_name2(self): assert index.name == "MyName" assert index2.name == "NewName" - with tm.assert_produces_warning(FutureWarning): - index3 = index.copy(names=["NewName"]) - tm.assert_index_equal(index, index3, check_names=False) - assert index.name == "MyName" - assert index.names == ["MyName"] - assert index3.name == "NewName" - assert index3.names == ["NewName"] - - def test_copy_names_deprecated(self, simple_index): - # GH44916 - with tm.assert_produces_warning(FutureWarning): - simple_index.copy(names=["a"]) - def test_unique_na(self): idx = Index([2, np.nan, 2, 1], name="my_index") expected = Index([2, np.nan, 1], name="my_index") From aa6634e1970326f58b132ccdbf14731ba484ccbb Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Wed, 19 Oct 2022 09:23:28 +0100 Subject: [PATCH 2/3] Add whatsnew --- doc/source/whatsnew/v2.0.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 80c8ad9a8b2eb..286c4ac4426c3 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -144,6 +144,7 @@ Deprecations Removal of prior version deprecations/changes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +- Removed arguments ``names`` and ``dtype`` from :meth:`Index.copy` and ``levels`` and ``codes`` from :meth:`MultiIndex.copy` (:issue:`35853`, :issue:`36685`) - Disallow passing non-round floats to :class:`Timestamp` with ``unit="M"`` or ``unit="Y"`` (:issue:`47266`) - From d3e696c899491e5810fde5c063d0ead4b4b0d5f3 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Wed, 19 Oct 2022 09:37:51 +0100 Subject: [PATCH 3/3] Fix whatsnew --- doc/source/whatsnew/v2.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 286c4ac4426c3..f8699b0b27d79 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -144,7 +144,7 @@ Deprecations Removal of prior version deprecations/changes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -- Removed arguments ``names`` and ``dtype`` from :meth:`Index.copy` and ``levels`` and ``codes`` from :meth:`MultiIndex.copy` (:issue:`35853`, :issue:`36685`) +- Remove arguments ``names`` and ``dtype`` from :meth:`Index.copy` and ``levels`` and ``codes`` from :meth:`MultiIndex.copy` (:issue:`35853`, :issue:`36685`) - Disallow passing non-round floats to :class:`Timestamp` with ``unit="M"`` or ``unit="Y"`` (:issue:`47266`) -