Skip to content
Open
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
23 changes: 14 additions & 9 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,19 @@ def _infer_selection(self, key, subset: Series | DataFrame):
Infer the `selection` to pass to our constructor in _gotitem.
"""
# Shared by Rolling and Resample
selection = None
if subset.ndim == 2 and (
(lib.is_scalar(key) and key in subset) or lib.is_list_like(key)
):
selection = key
elif subset.ndim == 1 and lib.is_scalar(key) and key == subset.name:
selection = key
return selection

# Avoid repeated calls to lib.is_scalar
is_scalar = lib.is_scalar(key)
if subset.ndim == 2:
if is_scalar:
# Check key in subset only if necessary
if key in subset:
return key
elif lib.is_list_like(key):
return key
elif subset.ndim == 1 and is_scalar and key == subset.name:
return key
return None

def aggregate(self, func, *args, **kwargs):
raise AbstractMethodError(self)
Expand Down Expand Up @@ -1263,7 +1268,7 @@ def _memory_usage(self, deep: bool = False) -> int:

v = self.array.nbytes
if deep and is_object_dtype(self.dtype) and not PYPY:
values = cast(np.ndarray, self._values)
values = cast("np.ndarray", self._values)
v += lib.memory_usage_of_objects(values)
return v

Expand Down