Skip to content
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

REF: helper to ensure column indexer is iterable #37475

Merged
Merged
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
19 changes: 15 additions & 4 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1648,10 +1648,7 @@ def _setitem_with_indexer(self, indexer, value):
labels = item_labels[info_idx]

# Ensure we have something we can iterate over
ilocs = info_idx
if isinstance(info_idx, slice):
ri = Index(range(len(self.obj.columns)))
ilocs = ri[info_idx]
ilocs = self._ensure_iterable_column_indexer(indexer[1])

plane_indexer = indexer[:1]
lplane_indexer = length_of_indexer(plane_indexer[0], self.obj.index)
Expand Down Expand Up @@ -1900,6 +1897,20 @@ def _setitem_with_indexer_missing(self, indexer, value):
self.obj._mgr = self.obj.append(value)._mgr
self.obj._maybe_update_cacher(clear=True)

def _ensure_iterable_column_indexer(self, column_indexer):
Copy link
Contributor

Choose a reason for hiding this comment

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

typ at some point

"""
Ensure that our column indexer is something that can be iterated over.
"""
# Ensure we have something we can iterate over
if is_integer(column_indexer):
ilocs = [column_indexer]
elif isinstance(column_indexer, slice):
ri = Index(range(len(self.obj.columns)))
ilocs = ri[column_indexer]
else:
ilocs = column_indexer
return ilocs

def _align_series(self, indexer, ser: "Series", multiindex_indexer: bool = False):
"""
Parameters
Expand Down