Skip to content

Commit

Permalink
CLN/REF: indexing typing, prune unreachable branches (pandas-dev#27351)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and jreback committed Jul 12, 2019
1 parent 84136d5 commit c633579
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 183 deletions.
75 changes: 28 additions & 47 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2889,11 +2889,11 @@ def _set_value(self, index, col, value, takeable=False):

_set_value.__doc__ = set_value.__doc__

def _ixs(self, i, axis=0):
def _ixs(self, i: int, axis: int = 0):
"""
Parameters
----------
i : int, slice, or sequence of integers
i : int
axis : int
Notes
Expand All @@ -2902,59 +2902,40 @@ def _ixs(self, i, axis=0):
"""
# irow
if axis == 0:
if isinstance(i, slice):
return self[i]
else:
label = self.index[i]
if isinstance(label, Index):
# a location index by definition
result = self.take(i, axis=axis)
copy = True
else:
new_values = self._data.fast_xs(i)
if is_scalar(new_values):
return new_values

# if we are a copy, mark as such
copy = (
isinstance(new_values, np.ndarray) and new_values.base is None
)
result = self._constructor_sliced(
new_values,
index=self.columns,
name=self.index[i],
dtype=new_values.dtype,
)
result._set_is_copy(self, copy=copy)
return result
label = self.index[i]
new_values = self._data.fast_xs(i)
if is_scalar(new_values):
return new_values

# if we are a copy, mark as such
copy = isinstance(new_values, np.ndarray) and new_values.base is None
result = self._constructor_sliced(
new_values,
index=self.columns,
name=self.index[i],
dtype=new_values.dtype,
)
result._set_is_copy(self, copy=copy)
return result

# icol
else:
label = self.columns[i]
if isinstance(i, slice):
# need to return view
lab_slice = slice(label[0], label[-1])
return self.loc[:, lab_slice]
else:
if isinstance(label, Index):
return self.take(i, axis=1)

index_len = len(self.index)
# if the values returned are not the same length
# as the index (iow a not found value), iget returns
# a 0-len ndarray. This is effectively catching
# a numpy error (as numpy should really raise)
values = self._data.iget(i)

# if the values returned are not the same length
# as the index (iow a not found value), iget returns
# a 0-len ndarray. This is effectively catching
# a numpy error (as numpy should really raise)
values = self._data.iget(i)
if len(self.index) and not len(values):
values = np.array([np.nan] * len(self.index), dtype=object)
result = self._box_col_values(values, label)

if index_len and not len(values):
values = np.array([np.nan] * index_len, dtype=object)
result = self._box_col_values(values, label)
# this is a cached value, mark it so
result._set_as_cached(label, self)

# this is a cached value, mark it so
result._set_as_cached(label, self)

return result
return result

def __getitem__(self, key):
key = lib.item_from_zerodim(key)
Expand Down
5 changes: 1 addition & 4 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3495,7 +3495,7 @@ def __delitem__(self, key):
deleted = False

maybe_shortcut = False
if hasattr(self, "columns") and isinstance(self.columns, MultiIndex):
if self.ndim == 2 and isinstance(self.columns, MultiIndex):
try:
maybe_shortcut = key not in self.columns._engine
except TypeError:
Expand Down Expand Up @@ -5231,9 +5231,6 @@ def _dir_additions(self):
}
return super()._dir_additions().union(additions)

# ----------------------------------------------------------------------
# Getting and setting elements

# ----------------------------------------------------------------------
# Consolidation of internals

Expand Down
Loading

0 comments on commit c633579

Please sign in to comment.