Skip to content

Commit e97d879

Browse files
committed
Follow up PR: pandas-dev#28097 Simplify branch statement
1 parent 08ab156 commit e97d879

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

pandas/core/indexes/base.py

+11-14
Original file line numberDiff line numberDiff line change
@@ -814,19 +814,13 @@ def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs):
814814
if kwargs:
815815
nv.validate_take(tuple(), kwargs)
816816
indices = ensure_platform_int(indices)
817-
if self._can_hold_na:
818-
taken = self._assert_take_fillable(
819-
self.values,
820-
indices,
821-
allow_fill=allow_fill,
822-
fill_value=fill_value,
823-
na_value=self._na_value,
824-
)
825-
else:
826-
if allow_fill and fill_value is not None:
827-
msg = "Unable to fill values because {0} cannot contain NA"
828-
raise ValueError(msg.format(self.__class__.__name__))
829-
taken = self.values.take(indices)
817+
taken = self._assert_take_fillable(
818+
self.values,
819+
indices,
820+
allow_fill=allow_fill,
821+
fill_value=fill_value,
822+
na_value=self._na_value,
823+
)
830824
return self._shallow_copy(taken)
831825

832826
def _assert_take_fillable(
@@ -839,7 +833,10 @@ def _assert_take_fillable(
839833

840834
# only fill if we are passing a non-None fill_value
841835
if allow_fill and fill_value is not None:
842-
if (indices < -1).any():
836+
if not self._can_hold_na and len(values):
837+
msg = "Unable to fill values because {0} cannot contain NA"
838+
raise ValueError(msg.format(self.__class__.__name__))
839+
elif (indices < -1).any():
843840
msg = (
844841
"When allow_fill=True and fill_value is not None, "
845842
"all indices must be >= -1"

pandas/core/indexes/multi.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -1308,10 +1308,7 @@ def _get_grouper_for_level(self, mapper, level):
13081308
# Remove unobserved levels from level_index
13091309
level_index = level_index.take(uniques)
13101310

1311-
if len(level_index):
1312-
grouper = level_index.take(codes)
1313-
else:
1314-
grouper = level_index.take(codes, fill_value=True)
1311+
grouper = level_index.take(codes, fill_value=True)
13151312

13161313
return grouper, codes, level_index
13171314

0 commit comments

Comments
 (0)