Skip to content

DEPR: deprecate dtype param in Index.copy #35853

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 2 commits into from
Aug 24, 2020
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ See :ref:`install.dependencies` and :ref:`install.optional_dependencies` for mor
Deprecations
~~~~~~~~~~~~
- Deprecated parameter ``inplace`` in :meth:`MultiIndex.set_codes` and :meth:`MultiIndex.set_levels` (:issue:`35626`)
-
- Deprecated parameter ``dtype`` in :~meth:`Index.copy` on method all index classes. Use the :meth:`Index.astype` method instead for changing dtype(:issue:`35853`)
-

.. ---------------------------------------------------------------------------
Expand Down
9 changes: 9 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,9 @@ def copy(self, name=None, deep=False, dtype=None, names=None):
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.

Expand All @@ -820,6 +823,12 @@ def copy(self, name=None, deep=False, dtype=None, names=None):
new_index = self._shallow_copy(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=2,
)
new_index = new_index.astype(dtype)
return new_index

Expand Down
19 changes: 14 additions & 5 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,6 @@ def _shallow_copy(
name=lib.no_default,
levels=None,
codes=None,
dtype=None,
Copy link
Contributor Author

@topper-123 topper-123 Aug 22, 2020

Choose a reason for hiding this comment

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

Other index types don't have dtype param in their _shallow_copy signature. Instead do dtype changing inside MultiIndex.copy.

sortorder=None,
names=lib.no_default,
_set_identity: bool = True,
Expand All @@ -1041,7 +1040,7 @@ def _shallow_copy(
names = name if name is not lib.no_default else self.names

if values is not None:
assert levels is None and codes is None and dtype is None
assert levels is None and codes is None
return MultiIndex.from_tuples(values, sortorder=sortorder, names=names)

levels = levels if levels is not None else self.levels
Expand All @@ -1050,7 +1049,6 @@ def _shallow_copy(
result = MultiIndex(
levels=levels,
codes=codes,
dtype=dtype,
sortorder=sortorder,
names=names,
verify_integrity=False,
Expand Down Expand Up @@ -1092,6 +1090,8 @@ def copy(
----------
names : sequence, optional
dtype : numpy dtype or pandas type, optional

.. deprecated:: 1.2.0
levels : sequence, optional
codes : sequence, optional
deep : bool, default False
Expand All @@ -1117,15 +1117,24 @@ def copy(
if codes is None:
codes = deepcopy(self.codes)

return self._shallow_copy(
new_index = self._shallow_copy(
levels=levels,
codes=codes,
names=names,
dtype=dtype,
sortorder=self.sortorder,
_set_identity=_set_identity,
)

if dtype:
warnings.warn(
"parameter dtype is deprecated and will be removed in a future "
"version. Use the astype method instead.",
FutureWarning,
stacklevel=2,
)
new_index = new_index.astype(dtype)
return new_index

def __array__(self, dtype=None) -> np.ndarray:
""" the array interface, return my values """
return self.values
Expand Down
11 changes: 9 additions & 2 deletions pandas/core/indexes/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,17 @@ def _shallow_copy(self, values=None, name: Label = no_default):

@doc(Int64Index.copy)
def copy(self, name=None, deep=False, dtype=None, names=None):
self._validate_dtype(dtype)

name = self._validate_names(name=name, names=names, deep=deep)[0]
new_index = self._shallow_copy(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=2,
)
new_index = new_index.astype(dtype)
return new_index

def _minmax(self, meth: str):
Expand Down
7 changes: 6 additions & 1 deletion pandas/tests/indexes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def test_copy_name(self, index):
s3 = s1 * s2
assert s3.index.name == "mario"

def test_name2(self, index):
def test_copy_name2(self, index):
# gh-35592
if isinstance(index, MultiIndex):
return
Expand All @@ -284,6 +284,11 @@ def test_name2(self, index):
with pytest.raises(TypeError, match=msg):
index.copy(name=[["mario"]])

def test_copy_dtype_deprecated(self, index):
# GH35853
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
index.copy(dtype=object)

def test_ensure_copied_data(self, index):
# Check the "copy" argument of each Index.__new__ is honoured
# GH12309
Expand Down
5 changes: 0 additions & 5 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,6 @@ def test_new_axis(self, index):
assert new_index.ndim == 2
assert isinstance(new_index, np.ndarray)

@pytest.mark.parametrize("index", ["int", "uint", "float"], indirect=True)
def test_copy_and_deepcopy(self, index):
new_copy2 = index.copy(dtype=int)
assert new_copy2.dtype.kind == "i"

def test_constructor_regular(self, index):
tm.assert_contains_all(index, index)

Expand Down
8 changes: 2 additions & 6 deletions pandas/tests/indexes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,7 @@ def test_has_duplicates(self, index):
"dtype",
["int64", "uint64", "float64", "category", "datetime64[ns]", "timedelta64[ns]"],
)
@pytest.mark.parametrize("copy", [True, False])
def test_astype_preserves_name(self, index, dtype, copy):
def test_astype_preserves_name(self, index, dtype):
# https://github.com/pandas-dev/pandas/issues/32013
if isinstance(index, MultiIndex):
index.names = ["idx" + str(i) for i in range(index.nlevels)]
Expand All @@ -384,10 +383,7 @@ def test_astype_preserves_name(self, index, dtype, copy):

try:
# Some of these conversions cannot succeed so we use a try / except
if copy:
result = index.copy(dtype=dtype)
else:
result = index.astype(dtype)
result = index.astype(dtype)
except (ValueError, TypeError, NotImplementedError, SystemError):
return

Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,15 +394,15 @@ def test_identical(self):
same_values_different_type = Index(i, dtype=object)
assert not i.identical(same_values_different_type)

i = index.copy(dtype=object)
i = index.astype(dtype=object)
i = i.rename("foo")
same_values = Index(i, dtype=object)
assert same_values.identical(i)

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

assert not index.copy(dtype=object).identical(index.copy(dtype=self._dtype))
assert not index.astype(dtype=object).identical(index.astype(dtype=self._dtype))

def test_union_noncomparable(self):
# corner case, non-Int64Index
Expand Down