Skip to content

Commit aa051b8

Browse files
authored
DEP: Enforce deprecation of names and dtype in index copy and codes and levels in midx.copy (#49087)
* DEP: Enforce deprecation of names and dtype in index copy and codes and levels in midx.copy * Add whatsnew * Fix whatsnew
1 parent c152288 commit aa051b8

File tree

7 files changed

+8
-129
lines changed

7 files changed

+8
-129
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ Deprecations
144144

145145
Removal of prior version deprecations/changes
146146
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
147+
- Remove arguments ``names`` and ``dtype`` from :meth:`Index.copy` and ``levels`` and ``codes`` from :meth:`MultiIndex.copy` (:issue:`35853`, :issue:`36685`)
147148
- Disallow passing non-round floats to :class:`Timestamp` with ``unit="M"`` or ``unit="Y"`` (:issue:`47266`)
148149
- Removed :func:`is_extension_type` in favor of :func:`is_extension_array_dtype` (:issue:`29457`)
149150
- Remove :meth:`DataFrameGroupBy.pad` and :meth:`DataFrameGroupBy.backfill` (:issue:`45076`)

pandas/core/indexes/base.py

+2-31
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
Axes,
5151
Axis,
5252
DropKeep,
53-
Dtype,
5453
DtypeObj,
5554
F,
5655
IgnoreRaise,
@@ -1292,29 +1291,17 @@ def copy(
12921291
self: _IndexT,
12931292
name: Hashable | None = None,
12941293
deep: bool = False,
1295-
dtype: Dtype | None = None,
1296-
names: Sequence[Hashable] | None = None,
12971294
) -> _IndexT:
12981295
"""
12991296
Make a copy of this object.
13001297
1301-
Name and dtype sets those attributes on the new object.
1298+
Name is set on the new object.
13021299
13031300
Parameters
13041301
----------
13051302
name : Label, optional
13061303
Set name for new object.
13071304
deep : bool, default False
1308-
dtype : numpy dtype or pandas type, optional
1309-
Set dtype for new object.
1310-
1311-
.. deprecated:: 1.2.0
1312-
use ``astype`` method instead.
1313-
names : list-like, optional
1314-
Kept for compatibility with MultiIndex. Should not be used.
1315-
1316-
.. deprecated:: 1.4.0
1317-
use ``name`` instead.
13181305
13191306
Returns
13201307
-------
@@ -1326,29 +1313,13 @@ def copy(
13261313
In most cases, there should be no functional difference from using
13271314
``deep``, but if ``deep`` is passed it will attempt to deepcopy.
13281315
"""
1329-
if names is not None:
1330-
warnings.warn(
1331-
"parameter names is deprecated and will be removed in a future "
1332-
"version. Use the name parameter instead.",
1333-
FutureWarning,
1334-
stacklevel=find_stack_level(),
1335-
)
13361316

1337-
name = self._validate_names(name=name, names=names, deep=deep)[0]
1317+
name = self._validate_names(name=name, deep=deep)[0]
13381318
if deep:
13391319
new_data = self._data.copy()
13401320
new_index = type(self)._simple_new(new_data, name=name)
13411321
else:
13421322
new_index = self._rename(name=name)
1343-
1344-
if dtype:
1345-
warnings.warn(
1346-
"parameter dtype is deprecated and will be removed in a future "
1347-
"version. Use the astype method instead.",
1348-
FutureWarning,
1349-
stacklevel=find_stack_level(),
1350-
)
1351-
new_index = new_index.astype(dtype)
13521323
return new_index
13531324

13541325
@final

pandas/core/indexes/multi.py

+3-41
Original file line numberDiff line numberDiff line change
@@ -1176,9 +1176,6 @@ def _view(self) -> MultiIndex:
11761176
def copy( # type: ignore[override]
11771177
self,
11781178
names=None,
1179-
dtype=None,
1180-
levels=None,
1181-
codes=None,
11821179
deep: bool = False,
11831180
name=None,
11841181
):
@@ -1189,15 +1186,6 @@ def copy( # type: ignore[override]
11891186
Parameters
11901187
----------
11911188
names : sequence, optional
1192-
dtype : numpy dtype or pandas type, optional
1193-
1194-
.. deprecated:: 1.2.0
1195-
levels : sequence, optional
1196-
1197-
.. deprecated:: 1.2.0
1198-
codes : sequence, optional
1199-
1200-
.. deprecated:: 1.2.0
12011189
deep : bool, default False
12021190
name : Label
12031191
Kept for compatibility with 1-dimensional Index. Should not be used.
@@ -1214,30 +1202,13 @@ def copy( # type: ignore[override]
12141202
"""
12151203
names = self._validate_names(name=name, names=names, deep=deep)
12161204
keep_id = not deep
1217-
if levels is not None:
1218-
warnings.warn(
1219-
"parameter levels is deprecated and will be removed in a future "
1220-
"version. Use the set_levels method instead.",
1221-
FutureWarning,
1222-
stacklevel=find_stack_level(),
1223-
)
1224-
keep_id = False
1225-
if codes is not None:
1226-
warnings.warn(
1227-
"parameter codes is deprecated and will be removed in a future "
1228-
"version. Use the set_codes method instead.",
1229-
FutureWarning,
1230-
stacklevel=find_stack_level(),
1231-
)
1232-
keep_id = False
1205+
levels, codes = None, None
12331206

12341207
if deep:
12351208
from copy import deepcopy
12361209

1237-
if levels is None:
1238-
levels = deepcopy(self.levels)
1239-
if codes is None:
1240-
codes = deepcopy(self.codes)
1210+
levels = deepcopy(self.levels)
1211+
codes = deepcopy(self.codes)
12411212

12421213
levels = levels if levels is not None else self.levels
12431214
codes = codes if codes is not None else self.codes
@@ -1253,15 +1224,6 @@ def copy( # type: ignore[override]
12531224
new_index._cache.pop("levels", None) # GH32669
12541225
if keep_id:
12551226
new_index._id = self._id
1256-
1257-
if dtype:
1258-
warnings.warn(
1259-
"parameter dtype is deprecated and will be removed in a future "
1260-
"version. Use the astype method instead.",
1261-
FutureWarning,
1262-
stacklevel=find_stack_level(),
1263-
)
1264-
new_index = new_index.astype(dtype)
12651227
return new_index
12661228

12671229
def __array__(self, dtype=None) -> np.ndarray:

pandas/core/indexes/range.py

+2-17
Original file line numberDiff line numberDiff line change
@@ -456,24 +456,9 @@ def _view(self: RangeIndex) -> RangeIndex:
456456
return result
457457

458458
@doc(Int64Index.copy)
459-
def copy(
460-
self,
461-
name: Hashable = None,
462-
deep: bool = False,
463-
dtype: Dtype | None = None,
464-
names=None,
465-
):
466-
name = self._validate_names(name=name, names=names, deep=deep)[0]
459+
def copy(self, name: Hashable = None, deep: bool = False):
460+
name = self._validate_names(name=name, deep=deep)[0]
467461
new_index = self._rename(name=name)
468-
469-
if dtype:
470-
warnings.warn(
471-
"parameter dtype is deprecated and will be removed in a future "
472-
"version. Use the astype method instead.",
473-
FutureWarning,
474-
stacklevel=find_stack_level(),
475-
)
476-
new_index = new_index.astype(dtype)
477462
return new_index
478463

479464
def _minmax(self, meth: str):

pandas/tests/indexes/multi/test_copy.py

-21
Original file line numberDiff line numberDiff line change
@@ -85,27 +85,6 @@ def test_copy_method_kwargs(deep, kwarg, value):
8585
assert getattr(idx_copy, kwarg) == value
8686

8787

88-
@pytest.mark.parametrize("deep", [True, False])
89-
@pytest.mark.parametrize(
90-
"param_name, param_value",
91-
[
92-
("levels", [["foo2", "bar2"], ["fizz2", "buzz2"]]),
93-
("codes", [[1, 0, 0, 0], [1, 1, 0, 0]]),
94-
],
95-
)
96-
def test_copy_deprecated_parameters(deep, param_name, param_value):
97-
# gh-36685
98-
idx = MultiIndex(
99-
levels=[["foo", "bar"], ["fizz", "buzz"]],
100-
codes=[[0, 0, 0, 1], [0, 0, 1, 1]],
101-
names=["first", "second"],
102-
)
103-
with tm.assert_produces_warning(FutureWarning):
104-
idx_copy = idx.copy(deep=deep, **{param_name: param_value})
105-
106-
assert [list(i) for i in getattr(idx_copy, param_name)] == param_value
107-
108-
10988
def test_copy_deep_false_retains_id():
11089
# GH#47878
11190
idx = MultiIndex(

pandas/tests/indexes/test_any_index.py

-6
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ def test_hash_error(index):
3232
hash(index)
3333

3434

35-
def test_copy_dtype_deprecated(index):
36-
# GH#35853
37-
with tm.assert_produces_warning(FutureWarning):
38-
index.copy(dtype=object)
39-
40-
4135
def test_mutability(index):
4236
if not len(index):
4337
return

pandas/tests/indexes/test_base.py

-13
Original file line numberDiff line numberDiff line change
@@ -1305,19 +1305,6 @@ def test_copy_name2(self):
13051305
assert index.name == "MyName"
13061306
assert index2.name == "NewName"
13071307

1308-
with tm.assert_produces_warning(FutureWarning):
1309-
index3 = index.copy(names=["NewName"])
1310-
tm.assert_index_equal(index, index3, check_names=False)
1311-
assert index.name == "MyName"
1312-
assert index.names == ["MyName"]
1313-
assert index3.name == "NewName"
1314-
assert index3.names == ["NewName"]
1315-
1316-
def test_copy_names_deprecated(self, simple_index):
1317-
# GH44916
1318-
with tm.assert_produces_warning(FutureWarning):
1319-
simple_index.copy(names=["a"])
1320-
13211308
def test_unique_na(self):
13221309
idx = Index([2, np.nan, 2, 1], name="my_index")
13231310
expected = Index([2, np.nan, 1], name="my_index")

0 commit comments

Comments
 (0)