Skip to content

Commit 16554c6

Browse files
committed
refactoring
1 parent 45f4bc7 commit 16554c6

File tree

1 file changed

+14
-28
lines changed

1 file changed

+14
-28
lines changed

pandas/core/frame.py

+14-28
Original file line numberDiff line numberDiff line change
@@ -3488,44 +3488,30 @@ def _get_info_slice(obj, indexer):
34883488
)
34893489
)
34903490

3491-
# empty include/exclude -> defaults to True
3492-
# three cases (we've already raised if both are empty)
3493-
# case 1: empty include, nonempty exclude
3494-
# we have True, True, ... True for include, same for exclude
3495-
# in the loop below we get the excluded
3496-
# and when we call '&' below we get only the excluded
3497-
# case 2: nonempty include, empty exclude
3498-
# same as case 1, but with include
3499-
# case 3: both nonempty
3500-
# the "union" of the logic of case 1 and case 2:
3501-
# we get the included and excluded, and return their logical and
3502-
include_these = Series(not bool(include), index=self.columns)
3503-
exclude_these = Series(not bool(exclude), index=self.columns)
3504-
3491+
# We raise when both include and exclude are empty
3492+
# Hence, we can just shrink the columns we want to keep
3493+
keep_these = Series(True, index=self.columns)
35053494
unique_dtypes = self.dtypes.unique()
35063495

3507-
if include:
3508-
included_dtypes = [
3496+
def filter_unique_dtypes_on(selection, unqiue_dtypes):
3497+
result = [
35093498
dtype
35103499
for dtype in unique_dtypes
35113500
if any(
3512-
issubclass(dtype.type, included_type) for included_type in include
3501+
issubclass(dtype.type, selected_type) for selected_type in selection
35133502
)
35143503
]
3515-
include_these |= self.dtypes.isin(included_dtypes)
3504+
return result
3505+
3506+
if include:
3507+
included_dtypes = filter_unique_dtypes_on(include, unique_dtypes)
3508+
keep_these &= self.dtypes.isin(included_dtypes)
35163509

35173510
if exclude:
3518-
excluded_dtypes = [
3519-
dtype
3520-
for dtype in unique_dtypes
3521-
if any(
3522-
issubclass(dtype.type, excluded_type) for excluded_type in exclude
3523-
)
3524-
]
3525-
exclude_these |= ~self.dtypes.isin(excluded_dtypes)
3511+
excluded_dtypes = filter_unique_dtypes_on(exclude, unique_dtypes)
3512+
keep_these &= ~self.dtypes.isin(excluded_dtypes)
35263513

3527-
dtype_indexer = include_these & exclude_these
3528-
return self.loc[_get_info_slice(self, dtype_indexer)]
3514+
return self.loc[_get_info_slice(self, keep_these)]
35293515

35303516
def insert(self, loc, column, value, allow_duplicates=False):
35313517
"""

0 commit comments

Comments
 (0)