Skip to content

Commit

Permalink
Follow up PR: pandas-dev#28097 Simplify branch statement
Browse files Browse the repository at this point in the history
  • Loading branch information
proost committed Oct 28, 2019
1 parent 08ab156 commit e97d879
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
25 changes: 11 additions & 14 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -814,19 +814,13 @@ def take(self, indices, axis=0, allow_fill=True, fill_value=None, **kwargs):
if kwargs:
nv.validate_take(tuple(), kwargs)
indices = ensure_platform_int(indices)
if self._can_hold_na:
taken = self._assert_take_fillable(
self.values,
indices,
allow_fill=allow_fill,
fill_value=fill_value,
na_value=self._na_value,
)
else:
if allow_fill and fill_value is not None:
msg = "Unable to fill values because {0} cannot contain NA"
raise ValueError(msg.format(self.__class__.__name__))
taken = self.values.take(indices)
taken = self._assert_take_fillable(
self.values,
indices,
allow_fill=allow_fill,
fill_value=fill_value,
na_value=self._na_value,
)
return self._shallow_copy(taken)

def _assert_take_fillable(
Expand All @@ -839,7 +833,10 @@ def _assert_take_fillable(

# only fill if we are passing a non-None fill_value
if allow_fill and fill_value is not None:
if (indices < -1).any():
if not self._can_hold_na and len(values):
msg = "Unable to fill values because {0} cannot contain NA"
raise ValueError(msg.format(self.__class__.__name__))
elif (indices < -1).any():
msg = (
"When allow_fill=True and fill_value is not None, "
"all indices must be >= -1"
Expand Down
5 changes: 1 addition & 4 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1308,10 +1308,7 @@ def _get_grouper_for_level(self, mapper, level):
# Remove unobserved levels from level_index
level_index = level_index.take(uniques)

if len(level_index):
grouper = level_index.take(codes)
else:
grouper = level_index.take(codes, fill_value=True)
grouper = level_index.take(codes, fill_value=True)

return grouper, codes, level_index

Expand Down

0 comments on commit e97d879

Please sign in to comment.