Skip to content

CLN: remove unused categories/ordered handling in astype #28646

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
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
14 changes: 4 additions & 10 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5772,7 +5772,7 @@ def _to_dict_of_blocks(self, copy=True):
for k, v, in self._data.to_dict(copy=copy).items()
}

def astype(self, dtype, copy=True, errors="raise", **kwargs):
def astype(self, dtype, copy=True, errors="raise"):
"""
Cast a pandas object to a specified dtype ``dtype``.

Expand All @@ -5795,8 +5795,6 @@ def astype(self, dtype, copy=True, errors="raise", **kwargs):

.. versionadded:: 0.20.0

**kwargs : keyword arguments to pass on to the constructor

Returns
-------
casted : same type as caller
Expand Down Expand Up @@ -5882,7 +5880,7 @@ def astype(self, dtype, copy=True, errors="raise", **kwargs):
"the key in Series dtype mappings."
)
new_type = dtype[self.name]
return self.astype(new_type, copy, errors, **kwargs)
return self.astype(new_type, copy, errors)

for col_name in dtype.keys():
if col_name not in self:
Expand All @@ -5894,9 +5892,7 @@ def astype(self, dtype, copy=True, errors="raise", **kwargs):
for col_name, col in self.items():
if col_name in dtype:
results.append(
col.astype(
dtype=dtype[col_name], copy=copy, errors=errors, **kwargs
)
col.astype(dtype=dtype[col_name], copy=copy, errors=errors)
)
else:
results.append(results.append(col.copy() if copy else col))
Expand All @@ -5911,9 +5907,7 @@ def astype(self, dtype, copy=True, errors="raise", **kwargs):

else:
# else, only a single dtype is given
new_data = self._data.astype(
dtype=dtype, copy=copy, errors=errors, **kwargs
)
new_data = self._data.astype(dtype=dtype, copy=copy, errors=errors)
return self._constructor(new_data).__finalize__(self)

# GH 19920: retain column metadata after concat
Expand Down
14 changes: 1 addition & 13 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,18 +574,6 @@ def _astype(self, dtype, copy=False, errors="raise", **kwargs):
# may need to convert to categorical
if self.is_categorical_astype(dtype):

# deprecated 17636
for deprecated_arg in ("categories", "ordered"):
if deprecated_arg in kwargs:
raise ValueError(
"Got an unexpected argument: {}".format(deprecated_arg)
)

categories = kwargs.get("categories", None)
ordered = kwargs.get("ordered", None)
if com.any_not_none(categories, ordered):
dtype = CategoricalDtype(categories, ordered)

if is_categorical_dtype(self.values):
# GH 10696/18593: update an existing categorical efficiently
return self.make_block(self.values.astype(dtype, copy=copy))
Expand Down Expand Up @@ -621,7 +609,7 @@ def _astype(self, dtype, copy=False, errors="raise", **kwargs):
# _astype_nansafe works fine with 1-d only
vals1d = values.ravel()
try:
values = astype_nansafe(vals1d, dtype, copy=True, **kwargs)
values = astype_nansafe(vals1d, dtype, copy=True)
except (ValueError, TypeError):
# e.g. astype_nansafe can fail on object-dtype of strings
# trying to convert to float
Expand Down
7 changes: 3 additions & 4 deletions pandas/tests/series/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,10 @@ def test_astype_dict_like(self, dtype_class):
with pytest.raises(KeyError, match=msg):
s.astype(dt5)

def test_astype_categories_deprecation_raises(self):

# deprecated 17636
def test_astype_categories_raises(self):
# deprecated 17636, removed in GH-27141
s = Series(["a", "b", "a"])
with pytest.raises(ValueError, match="Got an unexpected"):
with pytest.raises(TypeError, match="got an unexpected"):
s.astype("category", categories=["a", "b"], ordered=True)

@pytest.mark.parametrize(
Expand Down