Skip to content

BUG: Series.loc raising KeyError for Iterator indexer in case of setitem #39623

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 14 commits into from
Apr 16, 2021

Conversation

phofl
Copy link
Member

@phofl phofl commented Feb 6, 2021

@phofl phofl added Bug Indexing Related to indexing on series/frames, not to indexes themselves labels Feb 6, 2021
@jbrockmendel
Copy link
Member

can you add any relevant cases to SetitemCastingEquivalents

@jreback jreback added this to the 1.3 milestone Feb 7, 2021
@phofl
Copy link
Member Author

phofl commented Feb 7, 2021

The functions in there don't work with a generator.

Are you referring to adding a new function or to add a parametrization? Parametrization won't work with a generator, while adding a new function would cause a lot of special casing I think

@jbrockmendel
Copy link
Member

Are you referring to adding a new function or to add a parametrization? Parametrization won't work with a generator, while adding a new function would cause a lot of special casing I think

i was thinking adding to test_slice_key a check with (x for x in indkey) and in test_int_key one for (x for x in [key])

@phofl
Copy link
Member Author

phofl commented Feb 13, 2021

At thx. I though you were talking about adding another function.

Should a Generator work for iloc?

IndexError: .iloc requires numeric indexers, got <generator object <genexpr> at 0x7f86ac39a890>

iloc.getitem raises this, so I assume no?

@jbrockmendel
Copy link
Member

Should a Generator work for iloc?

i dont know if we have a specific policy for that. my only opinion here is that loc/iloc/__getitem__/__setitem__ should be consistent

@jreback
Copy link
Contributor

jreback commented Feb 15, 2021

At thx. I though you were talking about adding another function.

Should a Generator work for iloc?

IndexError: .iloc requires numeric indexers, got <generator object <genexpr> at 0x7f86ac39a890>

iloc.getitem raises this, so I assume no?

I don't think we accept iterator / generators for indexers, though we do in the constructors. I think its ok that we do, but agree that we should generally test these. Can you open an issue for this.

@jreback
Copy link
Contributor

jreback commented Feb 15, 2021

err rather, I agree with @jbrockmendel that we should systemcially test all setter & getter indexing methods in order to close this (rather than just adding in a specific place).

@phofl
Copy link
Member Author

phofl commented Feb 19, 2021

This would support Iterators for iloc and loc consistently

@jreback
Copy link
Contributor

jreback commented Feb 27, 2021

can you merge master

@phofl
Copy link
Member Author

phofl commented Mar 2, 2021

Done

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

lgtm. @jbrockmendel if you think we are covering all of the testing needed here (can merge this and followup if needed as well).

@@ -570,6 +573,9 @@ def test_slice_key(self, obj, key, expected, val, indexer_sli, is_inplace):
indkey = np.array(ilkey)
self.check_indexer(obj, indkey, expected, val, indexer_sli, is_inplace)

genkey = (x for x in indkey)
self.check_indexer(obj, genkey, expected, val, indexer_sli, is_inplace)

Copy link
Member

Choose a reason for hiding this comment

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

these tests are good, but only cover Series. do we need analogous DataFrame tests?

Copy link
Member

Choose a reason for hiding this comment

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

@phofl i think this was the main outstanding question. if the answer is "no" then this should be good to go (merge master just to be on the safe side)

Copy link
Member Author

@phofl phofl Mar 29, 2021

Choose a reason for hiding this comment

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

Sorry for the long wait. I have more time on my handy in the coming weeks and months again :)

One-dimensional indexers on DataFrames work, but two dimensionals like

df = DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})

indexer = (x for x in [1, 2])
df.iloc[indexer, 1]

still don't, because we access the tuple elements to check things before doing anything, which leaves an empty generator behind. Only way I am seeing currently is converting this directly in setitem and getitem. Thoughts @jbrockmendel ?

Copy link
Member

Choose a reason for hiding this comment

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

Only way I am seeing currently is converting this directly in setitem and getitem

I expect this is probably right

Copy link
Member Author

@phofl phofl Mar 31, 2021

Choose a reason for hiding this comment

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

Will Put something up then tomorrow or the day after

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

lgtm can merge master and ping on green

@phofl
Copy link
Member Author

phofl commented Apr 2, 2021

I have added support for more dimensional iloc indexers. This should cover all cases now

@jreback
Copy link
Contributor

jreback commented Apr 9, 2021

can you rebase

@phofl
Copy link
Member Author

phofl commented Apr 9, 2021

Done

@jbrockmendel
Copy link
Member

probably needs another rebase, but i think this was ready

@phofl
Copy link
Member Author

phofl commented Apr 15, 2021

rebased

@jreback jreback merged commit cc0ddf6 into pandas-dev:master Apr 16, 2021
@jreback
Copy link
Contributor

jreback commented Apr 16, 2021

thanks @phofl very nice

@phofl phofl deleted the 39614 branch April 16, 2021 08:13
yeshsurya pushed a commit to yeshsurya/pandas that referenced this pull request Apr 21, 2021
JulianWgs pushed a commit to JulianWgs/pandas that referenced this pull request Jul 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging this pull request may close these issues.

QST: Why is indexing with a reversed list possible but not assigning a new value?
3 participants