Skip to content

CLN: remove unnecessary Categorical._validate_setitem_key #37068

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
Oct 12, 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
28 changes: 1 addition & 27 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from pandas._config import get_option

from pandas._libs import NaT, algos as libalgos, hashtable as htable, lib
from pandas._libs import NaT, algos as libalgos, hashtable as htable
from pandas._typing import ArrayLike, Dtype, Ordered, Scalar
from pandas.compat.numpy import function as nv
from pandas.util._decorators import cache_readonly, deprecate_kwarg
Expand Down Expand Up @@ -1907,32 +1907,6 @@ def _validate_setitem_value(self, value):

return self._unbox_listlike(rvalue)

def _validate_setitem_key(self, key):
if lib.is_integer(key):
# set by position
pass

elif isinstance(key, tuple):
# tuple of indexers (dataframe)
# only allow 1 dimensional slicing, but can
# in a 2-d case be passed (slice(None),....)
if len(key) == 2:
if not com.is_null_slice(key[0]):
raise AssertionError("invalid slicing for a 1-ndim categorical")
key = key[1]
elif len(key) == 1:
key = key[0]
else:
raise AssertionError("invalid slicing for a 1-ndim categorical")

elif isinstance(key, slice):
# slicing in Series or Categorical
pass

# else: array of True/False in Series or Categorical

return super()._validate_setitem_key(key)

def _reverse_indexer(self) -> Dict[Hashable, np.ndarray]:
"""
Compute the inverse of a categorical, returning
Expand Down
14 changes: 5 additions & 9 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2008,25 +2008,21 @@ class NumericBlock(Block):
_can_hold_na = True


class FloatOrComplexBlock(NumericBlock):
__slots__ = ()


class FloatBlock(FloatOrComplexBlock):
class FloatBlock(NumericBlock):
__slots__ = ()
is_float = True

def _can_hold_element(self, element: Any) -> bool:
tipo = maybe_infer_dtype_type(element)
if tipo is not None:
return issubclass(tipo.type, (np.floating, np.integer)) and not issubclass(
tipo.type, (np.datetime64, np.timedelta64)
tipo.type, np.timedelta64
)
return isinstance(
element, (float, int, np.floating, np.int_)
) and not isinstance(
element,
(bool, np.bool_, datetime, timedelta, np.datetime64, np.timedelta64),
(bool, np.bool_, np.timedelta64),
)

def to_native_types(
Expand Down Expand Up @@ -2063,7 +2059,7 @@ def to_native_types(
return self.make_block(res)


class ComplexBlock(FloatOrComplexBlock):
class ComplexBlock(NumericBlock):
__slots__ = ()
is_complex = True

Expand All @@ -2086,7 +2082,7 @@ def _can_hold_element(self, element: Any) -> bool:
if tipo is not None:
return (
issubclass(tipo.type, np.integer)
and not issubclass(tipo.type, (np.datetime64, np.timedelta64))
and not issubclass(tipo.type, np.timedelta64)
and self.dtype.itemsize >= tipo.itemsize
)
# We have not inferred an integer from the dtype
Expand Down