Skip to content

REF: share common code in ArrayManager.get_numeric_data/get_bool_data #40030

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
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: 13 additions & 10 deletions pandas/core/internals/array_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,31 +480,34 @@ def is_view(self) -> bool:
def is_single_block(self) -> bool:
return False

def _get_data_subset(self, predicate: Callable) -> ArrayManager:
indices = [i for i, arr in enumerate(self.arrays) if predicate(arr)]
arrays = [self.arrays[i] for i in indices]
# TODO copy?
new_axes = [self._axes[0], self._axes[1][np.array(indices, dtype="int64")]]
return type(self)(arrays, new_axes, verify_integrity=False)

def get_bool_data(self, copy: bool = False) -> ArrayManager:
"""
Select columns that are bool-dtype.

Parameters
----------
copy : bool, default False
Whether to copy the blocks
"""
mask = np.array([is_bool_dtype(t) for t in self.get_dtypes()], dtype="object")
arrays = [self.arrays[i] for i in np.nonzero(mask)[0]]
# TODO copy?
new_axes = [self._axes[0], self._axes[1][mask]]
return type(self)(arrays, new_axes)
return self._get_data_subset(lambda arr: is_bool_dtype(arr.dtype))

def get_numeric_data(self, copy: bool = False) -> ArrayManager:
"""
Select columns that have a numeric dtype.

Parameters
----------
copy : bool, default False
Whether to copy the blocks
"""
mask = np.array([is_numeric_dtype(t) for t in self.get_dtypes()])
arrays = [self.arrays[i] for i in np.nonzero(mask)[0]]
# TODO copy?
new_axes = [self._axes[0], self._axes[1][mask]]
return type(self)(arrays, new_axes)
return self._get_data_subset(lambda arr: is_numeric_dtype(arr.dtype))

def copy(self: T, deep=True) -> T:
"""
Expand Down