-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: iloc.__setitem__ with duplicate columns #32477
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
jreback
merged 17 commits into
pandas-dev:master
from
jbrockmendel:setitem-with-indexer
Mar 11, 2020
Merged
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
4330984
Fix iloc setitem with duplicate columns for scalar case
jbrockmendel 620c5b9
Fix setitem in more cases
jbrockmendel 08f9df3
BUG: fix setitem_with_indexer
jbrockmendel d21d92d
Merge branch 'master' of https://github.com/pandas-dev/pandas into se…
jbrockmendel 5b4711d
comment, no copy needed
jbrockmendel 13d3c19
Merge branch 'master' of https://github.com/pandas-dev/pandas into se…
jbrockmendel ed9bc77
Merge branch 'master' of https://github.com/pandas-dev/pandas into se…
jbrockmendel e686fe2
Merge branch 'master' of https://github.com/pandas-dev/pandas into se…
jbrockmendel e5e470e
Merge branch 'master' of https://github.com/pandas-dev/pandas into se…
jbrockmendel 9063047
update check
jbrockmendel e74f7ce
Merge branch 'master' of https://github.com/pandas-dev/pandas into se…
jbrockmendel 8a797be
Merge branch 'master' of https://github.com/pandas-dev/pandas into se…
jbrockmendel 280418e
comment
jbrockmendel 4a847e5
whatsnew
jbrockmendel 540d3ea
fix test on 32bit platform
jbrockmendel b75cf9f
Merge branch 'master' of https://github.com/pandas-dev/pandas into se…
jbrockmendel 3a17ba8
comments
jbrockmendel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1615,6 +1615,12 @@ def _setitem_with_indexer(self, indexer, value): | |
info_idx = [info_idx] | ||
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))) | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ilocs = ri[info_idx] | ||
|
||
plane_indexer = indexer[:1] | ||
lplane_indexer = length_of_indexer(plane_indexer[0], self.obj.index) | ||
# lplane_indexer gives the expected length of obj[indexer[0]] | ||
|
@@ -1632,9 +1638,10 @@ def _setitem_with_indexer(self, indexer, value): | |
"length than the value" | ||
) | ||
|
||
def setter(item, v): | ||
ser = self.obj[item] | ||
pi = plane_indexer[0] if lplane_indexer == 1 else plane_indexer | ||
pi = plane_indexer[0] if lplane_indexer == 1 else plane_indexer | ||
|
||
def isetter(loc, v): | ||
ser = self.obj._ixs(loc, axis=1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you comment that this is not a positional setter |
||
|
||
# perform the equivalent of a setitem on the info axis | ||
# as we have a null slice or a slice with full bounds | ||
|
@@ -1654,7 +1661,7 @@ def setter(item, v): | |
ser._maybe_update_cacher(clear=True) | ||
|
||
# reset the sliced object if unique | ||
self.obj[item] = ser | ||
self.obj._iset_item(loc, ser) | ||
|
||
# we need an iterable, with a ndim of at least 1 | ||
# eg. don't pass through np.array(0) | ||
|
@@ -1664,8 +1671,10 @@ def setter(item, v): | |
if isinstance(value, ABCDataFrame): | ||
sub_indexer = list(indexer) | ||
multiindex_indexer = isinstance(labels, ABCMultiIndex) | ||
# TODO: we are implicitly assuming value.columns is unique | ||
|
||
for item in labels: | ||
for loc in ilocs: | ||
item = item_labels[loc] | ||
if item in value: | ||
sub_indexer[info_axis] = item | ||
v = self._align_series( | ||
|
@@ -1674,7 +1683,7 @@ def setter(item, v): | |
else: | ||
v = np.nan | ||
|
||
setter(item, v) | ||
isetter(loc, v) | ||
|
||
# we have an equal len ndarray/convertible to our labels | ||
# hasattr first, to avoid coercing to ndarray without reason. | ||
|
@@ -1685,44 +1694,44 @@ def setter(item, v): | |
# note that this coerces the dtype if we are mixed | ||
# GH 7551 | ||
value = np.array(value, dtype=object) | ||
if len(labels) != value.shape[1]: | ||
if len(ilocs) != value.shape[1]: | ||
raise ValueError( | ||
"Must have equal len keys and value " | ||
"when setting with an ndarray" | ||
) | ||
|
||
for i, item in enumerate(labels): | ||
|
||
for i, loc in enumerate(ilocs): | ||
# setting with a list, re-coerces | ||
setter(item, value[:, i].tolist()) | ||
isetter(loc, value[:, i].tolist()) | ||
|
||
elif ( | ||
len(labels) == 1 | ||
and lplane_indexer == len(value) | ||
and not is_scalar(plane_indexer[0]) | ||
): | ||
# we have an equal len list/ndarray | ||
setter(labels[0], value) | ||
# We only get here with len(labels) == len(ilocs) == 1 | ||
isetter(ilocs[0], value) | ||
|
||
elif lplane_indexer == 0 and len(value) == len(self.obj.index): | ||
# We get here in one case via .loc with a all-False mask | ||
pass | ||
|
||
else: | ||
# per-label values | ||
if len(labels) != len(value): | ||
if len(ilocs) != len(value): | ||
raise ValueError( | ||
"Must have equal len keys and value " | ||
"when setting with an iterable" | ||
) | ||
|
||
for item, v in zip(labels, value): | ||
setter(item, v) | ||
for loc, v in zip(ilocs, value): | ||
isetter(loc, v) | ||
else: | ||
|
||
# scalar | ||
for item in labels: | ||
setter(item, value) | ||
# scalar value | ||
for loc in ilocs: | ||
isetter(loc, value) | ||
|
||
else: | ||
if isinstance(indexer, tuple): | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure i unserstand this comment