Skip to content

Commit b1d85ae

Browse files
committed
DEPR: deprecate dtype param in Index.copy
1 parent 068e654 commit b1d85ae

File tree

8 files changed

+42
-22
lines changed

8 files changed

+42
-22
lines changed

doc/source/whatsnew/v1.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ See :ref:`install.dependencies` and :ref:`install.optional_dependencies` for mor
143143
Deprecations
144144
~~~~~~~~~~~~
145145
- Deprecated parameter ``inplace`` in :meth:`MultiIndex.set_codes` and :meth:`MultiIndex.set_levels` (:issue:`35626`)
146-
-
146+
- Deprecated parameter ``dtype`` in :~meth:`Index.copy` on all index classes. Use :meth:`Index.astype` method (:issue:`35626`)
147147
-
148148

149149
.. ---------------------------------------------------------------------------

pandas/core/indexes/base.py

+9
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,9 @@ def copy(self, name=None, deep=False, dtype=None, names=None):
800800
deep : bool, default False
801801
dtype : numpy dtype or pandas type, optional
802802
Set dtype for new object.
803+
804+
.. deprecated:: 1.2.0
805+
use ``astype`` method instead.
803806
names : list-like, optional
804807
Kept for compatibility with MultiIndex. Should not be used.
805808
@@ -820,6 +823,12 @@ def copy(self, name=None, deep=False, dtype=None, names=None):
820823
new_index = self._shallow_copy(name=name)
821824

822825
if dtype:
826+
warnings.warn(
827+
"parameter dtype is deprecated and will be removed in a future version."
828+
" Use the astype method instead.",
829+
FutureWarning,
830+
stacklevel=2,
831+
)
823832
new_index = new_index.astype(dtype)
824833
return new_index
825834

pandas/core/indexes/multi.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,6 @@ def _shallow_copy(
10301030
name=lib.no_default,
10311031
levels=None,
10321032
codes=None,
1033-
dtype=None,
10341033
sortorder=None,
10351034
names=lib.no_default,
10361035
_set_identity: bool = True,
@@ -1041,7 +1040,7 @@ def _shallow_copy(
10411040
names = name if name is not lib.no_default else self.names
10421041

10431042
if values is not None:
1044-
assert levels is None and codes is None and dtype is None
1043+
assert levels is None and codes is None
10451044
return MultiIndex.from_tuples(values, sortorder=sortorder, names=names)
10461045

10471046
levels = levels if levels is not None else self.levels
@@ -1050,7 +1049,6 @@ def _shallow_copy(
10501049
result = MultiIndex(
10511050
levels=levels,
10521051
codes=codes,
1053-
dtype=dtype,
10541052
sortorder=sortorder,
10551053
names=names,
10561054
verify_integrity=False,
@@ -1092,6 +1090,8 @@ def copy(
10921090
----------
10931091
names : sequence, optional
10941092
dtype : numpy dtype or pandas type, optional
1093+
1094+
.. deprecated:: 1.2.0
10951095
levels : sequence, optional
10961096
codes : sequence, optional
10971097
deep : bool, default False
@@ -1117,15 +1117,24 @@ def copy(
11171117
if codes is None:
11181118
codes = deepcopy(self.codes)
11191119

1120-
return self._shallow_copy(
1120+
new_index = self._shallow_copy(
11211121
levels=levels,
11221122
codes=codes,
11231123
names=names,
1124-
dtype=dtype,
11251124
sortorder=self.sortorder,
11261125
_set_identity=_set_identity,
11271126
)
11281127

1128+
if dtype:
1129+
warnings.warn(
1130+
"parameter dtype is deprecated and will be removed in a future version."
1131+
" Use the astype method instead.",
1132+
FutureWarning,
1133+
stacklevel=2,
1134+
)
1135+
new_index = new_index.astype(dtype)
1136+
return new_index
1137+
11291138
def __array__(self, dtype=None) -> np.ndarray:
11301139
""" the array interface, return my values """
11311140
return self.values

pandas/core/indexes/range.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,17 @@ def _shallow_copy(self, values=None, name: Label = no_default):
390390

391391
@doc(Int64Index.copy)
392392
def copy(self, name=None, deep=False, dtype=None, names=None):
393-
self._validate_dtype(dtype)
394-
395393
name = self._validate_names(name=name, names=names, deep=deep)[0]
396394
new_index = self._shallow_copy(name=name)
395+
396+
if dtype:
397+
warnings.warn(
398+
"parameter dtype is deprecated and will be removed in a future version."
399+
" Use the astype method instead.",
400+
FutureWarning,
401+
stacklevel=2,
402+
)
403+
new_index = new_index.astype(dtype)
397404
return new_index
398405

399406
def _minmax(self, meth: str):

pandas/tests/indexes/common.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def test_copy_name(self, index):
270270
s3 = s1 * s2
271271
assert s3.index.name == "mario"
272272

273-
def test_name2(self, index):
273+
def test_copy_name2(self, index):
274274
# gh-35592
275275
if isinstance(index, MultiIndex):
276276
return
@@ -284,6 +284,10 @@ def test_name2(self, index):
284284
with pytest.raises(TypeError, match=msg):
285285
index.copy(name=[["mario"]])
286286

287+
def test_copy_dtype_deprecated(self, index):
288+
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
289+
index.copy(dtype=object)
290+
287291
def test_ensure_copied_data(self, index):
288292
# Check the "copy" argument of each Index.__new__ is honoured
289293
# GH12309

pandas/tests/indexes/test_base.py

-5
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,6 @@ def test_new_axis(self, index):
6262
assert new_index.ndim == 2
6363
assert isinstance(new_index, np.ndarray)
6464

65-
@pytest.mark.parametrize("index", ["int", "uint", "float"], indirect=True)
66-
def test_copy_and_deepcopy(self, index):
67-
new_copy2 = index.copy(dtype=int)
68-
assert new_copy2.dtype.kind == "i"
69-
7065
def test_constructor_regular(self, index):
7166
tm.assert_contains_all(index, index)
7267

pandas/tests/indexes/test_common.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,7 @@ def test_has_duplicates(self, index):
374374
"dtype",
375375
["int64", "uint64", "float64", "category", "datetime64[ns]", "timedelta64[ns]"],
376376
)
377-
@pytest.mark.parametrize("copy", [True, False])
378-
def test_astype_preserves_name(self, index, dtype, copy):
377+
def test_astype_preserves_name(self, index, dtype):
379378
# https://github.com/pandas-dev/pandas/issues/32013
380379
if isinstance(index, MultiIndex):
381380
index.names = ["idx" + str(i) for i in range(index.nlevels)]
@@ -384,10 +383,7 @@ def test_astype_preserves_name(self, index, dtype, copy):
384383

385384
try:
386385
# Some of these conversions cannot succeed so we use a try / except
387-
if copy:
388-
result = index.copy(dtype=dtype)
389-
else:
390-
result = index.astype(dtype)
386+
result = index.astype(dtype)
391387
except (ValueError, TypeError, NotImplementedError, SystemError):
392388
return
393389

pandas/tests/indexes/test_numeric.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -394,15 +394,15 @@ def test_identical(self):
394394
same_values_different_type = Index(i, dtype=object)
395395
assert not i.identical(same_values_different_type)
396396

397-
i = index.copy(dtype=object)
397+
i = index.astype(dtype=object)
398398
i = i.rename("foo")
399399
same_values = Index(i, dtype=object)
400400
assert same_values.identical(i)
401401

402402
assert not i.identical(index)
403403
assert Index(same_values, name="foo", dtype=object).identical(i)
404404

405-
assert not index.copy(dtype=object).identical(index.copy(dtype=self._dtype))
405+
assert not index.astype(dtype=object).identical(index.astype(dtype=self._dtype))
406406

407407
def test_union_noncomparable(self):
408408
# corner case, non-Int64Index

0 commit comments

Comments
 (0)