Skip to content

CLN/REF: indexing typing, prune unreachable branches #27351

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 17 commits into from
Jul 12, 2019
Merged
Show file tree
Hide file tree
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
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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since axis is a pretty common keyword can you put that as Axis in pandas._typing? We probably also want to add str as a type for it since it should accept "index" and "columns" as well

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also can we add the return type here? -> 'DataFrame'?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take a look and see what we can determine about return types. For Axis/str/int, in all these cases, I've put int because these are private methods and only ever get ints.

"""
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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the future change to use ABCMultiIndex

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