Skip to content

REF: No need to delegate to index check of whether an int is an int #21982

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 2 commits into from
Jul 20, 2018

Conversation

toobaz
Copy link
Member

@toobaz toobaz commented Jul 19, 2018

  • tests passed
  • passes git diff upstream/master -u -- "*.py" | flake8 --diff
  • whatsnew entry

Just fixing the following nonsensical error:

In [2]: pd.Series(range(10)).iloc['a']
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-4a32011255fe> in <module>()
----> 1 pd.Series(range(10)).iloc['a']
[...]
TypeError: cannot do positional indexing on <class 'pandas.core.indexes.range.RangeIndex'> with these indexers [a] of <class 'str'>

(you just cannot do positional indexing with a str, regardless of the index)

... and adding a docstring while I was at it.

@toobaz toobaz added Indexing Related to indexing on series/frames, not to indexes themselves Error Reporting Incorrect or improved errors from pandas labels Jul 19, 2018
@codecov
Copy link

codecov bot commented Jul 20, 2018

Codecov Report

Merging #21982 into master will decrease coverage by <.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #21982      +/-   ##
==========================================
- Coverage   91.96%   91.96%   -0.01%     
==========================================
  Files         166      166              
  Lines       50334    50333       -1     
==========================================
- Hits        46292    46291       -1     
  Misses       4042     4042
Flag Coverage Δ
#multiple 90.36% <ø> (-0.01%) ⬇️
#single 42.21% <ø> (-0.02%) ⬇️
Impacted Files Coverage Δ
pandas/core/indexing.py 93.82% <ø> (+0.08%) ⬆️
pandas/core/indexes/interval.py 94.12% <0%> (-0.19%) ⬇️
pandas/tseries/offsets.py 97.15% <0%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b9c1786...34cdc8e. Read the comment docs.

@toobaz toobaz force-pushed the iloc_with_non_int branch from eec4dd5 to 06141c0 Compare July 20, 2018 06:50
Copy link
Member

@jorisvandenbossche jorisvandenbossche left a comment

Choose a reason for hiding this comment

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

Good change! (I have also always been annoyed if I saw that error :-))

@@ -2215,8 +2233,6 @@ def _getitem_axis(self, key, axis=None):

# a single integer
else:
key = self._convert_scalar_indexer(key, axis)
Copy link
Member

Choose a reason for hiding this comment

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

Can you check if there is any remaining occasion where _convert_scalar_indexer is used for iloc (in principle it shouldn't?). Because if not, _convert_scalar_indexer has some redundant code

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, there is, and it was related to a commit I pushed but then removed, because unfortunately it broke some tests and I don't have time to check them in the next days. So I went for the obvious change only, will add the rest in some other PR (which I think will be quite large, as I will also touch code in indexes).

Copy link
Member

Choose a reason for hiding this comment

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

OK. Can you point to where it is currently used? (to have this on record)
Is it worth to add a todo comment there?

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 rebase my other PR on this once merged, push it and open an issue referring to it

@@ -32,6 +32,7 @@ Bug Fixes

- Bug where calling :func:`DataFrameGroupBy.agg` with a list of functions including ``ohlc`` as the non-initial element would raise a ``ValueError`` (:issue:`21716`)
- Bug in ``roll_quantile`` caused a memory leak when calling ``.rolling(...).quantile(q)`` with ``q`` in (0,1) (:issue:`21965`)
- Passing a non-int scalar to ``.iloc`` now raises a more appropriate error message (:issue:`21982`)
Copy link
Member

Choose a reason for hiding this comment

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

Not sure if it is worth to add a whatsnew for this.

Copy link
Member Author

Choose a reason for hiding this comment

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

OK, removing

@toobaz
Copy link
Member Author

toobaz commented Jul 20, 2018

See #21989 for the more general issue. Ready to merge if there are no objections.

@@ -50,7 +50,7 @@ def test_scalar_error(self):
def f():
s.iloc[3.0]
tm.assert_raises_regex(TypeError,
'cannot do positional indexing',
'Cannot index by location index',
Copy link
Contributor

Choose a reason for hiding this comment

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

side issue, we have 2 different versions of this error message (the original also happens), so these should be the same (future PR). I don't think we actually need to distinguish them.

Copy link
Member Author

Choose a reason for hiding this comment

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

We already have both messages in the code (I did not write a new message).

The only difference is that now this case raises the correct one.

@jreback jreback added this to the 0.24.0 milestone Jul 20, 2018
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.

can you add the test in top of the PR?

toobaz added a commit to toobaz/pandas that referenced this pull request Jul 20, 2018
@pep8speaks
Copy link

pep8speaks commented Jul 20, 2018

Hello @toobaz! Thanks for updating the PR.

Cheers ! There are no PEP8 issues in this Pull Request. 🍻

Comment last updated on July 20, 2018 at 13:23 Hours UTC

@toobaz toobaz force-pushed the iloc_with_non_int branch from 86c2733 to 34cdc8e Compare July 20, 2018 13:23
@jreback
Copy link
Contributor

jreback commented Jul 20, 2018

lgtm. merge on green.

@toobaz toobaz merged commit f8f0e61 into pandas-dev:master Jul 20, 2018
@toobaz toobaz deleted the iloc_with_non_int branch July 20, 2018 19:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Error Reporting Incorrect or improved errors from pandas Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants