Skip to content

Commit c633579

Browse files
jbrockmendeljreback
authored andcommitted
CLN/REF: indexing typing, prune unreachable branches (#27351)
1 parent 84136d5 commit c633579

File tree

4 files changed

+114
-183
lines changed

4 files changed

+114
-183
lines changed

pandas/core/frame.py

+28-47
Original file line numberDiff line numberDiff line change
@@ -2889,11 +2889,11 @@ def _set_value(self, index, col, value, takeable=False):
28892889

28902890
_set_value.__doc__ = set_value.__doc__
28912891

2892-
def _ixs(self, i, axis=0):
2892+
def _ixs(self, i: int, axis: int = 0):
28932893
"""
28942894
Parameters
28952895
----------
2896-
i : int, slice, or sequence of integers
2896+
i : int
28972897
axis : int
28982898
28992899
Notes
@@ -2902,59 +2902,40 @@ def _ixs(self, i, axis=0):
29022902
"""
29032903
# irow
29042904
if axis == 0:
2905-
if isinstance(i, slice):
2906-
return self[i]
2907-
else:
2908-
label = self.index[i]
2909-
if isinstance(label, Index):
2910-
# a location index by definition
2911-
result = self.take(i, axis=axis)
2912-
copy = True
2913-
else:
2914-
new_values = self._data.fast_xs(i)
2915-
if is_scalar(new_values):
2916-
return new_values
2917-
2918-
# if we are a copy, mark as such
2919-
copy = (
2920-
isinstance(new_values, np.ndarray) and new_values.base is None
2921-
)
2922-
result = self._constructor_sliced(
2923-
new_values,
2924-
index=self.columns,
2925-
name=self.index[i],
2926-
dtype=new_values.dtype,
2927-
)
2928-
result._set_is_copy(self, copy=copy)
2929-
return result
2905+
label = self.index[i]
2906+
new_values = self._data.fast_xs(i)
2907+
if is_scalar(new_values):
2908+
return new_values
2909+
2910+
# if we are a copy, mark as such
2911+
copy = isinstance(new_values, np.ndarray) and new_values.base is None
2912+
result = self._constructor_sliced(
2913+
new_values,
2914+
index=self.columns,
2915+
name=self.index[i],
2916+
dtype=new_values.dtype,
2917+
)
2918+
result._set_is_copy(self, copy=copy)
2919+
return result
29302920

29312921
# icol
29322922
else:
29332923
label = self.columns[i]
2934-
if isinstance(i, slice):
2935-
# need to return view
2936-
lab_slice = slice(label[0], label[-1])
2937-
return self.loc[:, lab_slice]
2938-
else:
2939-
if isinstance(label, Index):
2940-
return self.take(i, axis=1)
29412924

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

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

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

2954-
# this is a cached value, mark it so
2955-
result._set_as_cached(label, self)
2956-
2957-
return result
2938+
return result
29582939

29592940
def __getitem__(self, key):
29602941
key = lib.item_from_zerodim(key)

pandas/core/generic.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -3495,7 +3495,7 @@ def __delitem__(self, key):
34953495
deleted = False
34963496

34973497
maybe_shortcut = False
3498-
if hasattr(self, "columns") and isinstance(self.columns, MultiIndex):
3498+
if self.ndim == 2 and isinstance(self.columns, MultiIndex):
34993499
try:
35003500
maybe_shortcut = key not in self.columns._engine
35013501
except TypeError:
@@ -5231,9 +5231,6 @@ def _dir_additions(self):
52315231
}
52325232
return super()._dir_additions().union(additions)
52335233

5234-
# ----------------------------------------------------------------------
5235-
# Getting and setting elements
5236-
52375234
# ----------------------------------------------------------------------
52385235
# Consolidation of internals
52395236

0 commit comments

Comments
 (0)