Skip to content

ENH: InvalidIndexError using at for multiple assignment #51866

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

Open
2 tasks done
sjfleming opened this issue Mar 9, 2023 · 6 comments
Open
2 tasks done

ENH: InvalidIndexError using at for multiple assignment #51866

sjfleming opened this issue Mar 9, 2023 · 6 comments
Labels
Enhancement Error Reporting Incorrect or improved errors from pandas Indexing Related to indexing on series/frames, not to indexes themselves

Comments

@sjfleming
Copy link

Research

  • I have searched the [pandas] tag on StackOverflow for similar questions.

  • I have asked my usage related question on StackOverflow.

Link to question on StackOverflow

https://stackoverflow.com/questions/75688441/pandas-invalidindexerror-during-multiple-assignment-using-at

Question about pandas

This might not be a bug, so I'm not opening the issue as a bug.

I used to rely on at for multiple assignment in the following way:

import pandas as pd
import numpy as np

df = pd.DataFrame(data={'a': [2, 4, 1, 5, 56, 6, 1, 2, 1, 0], 
                        'b': [3, 4, 5, 2, 6, 7, 5, 3, 5, 6]}, 
                  index=range(10))
df.at[df['b'] == 6, 'b'] = np.nan  # np.nan is not special. any float here will produce the same thing.

df

which produces (my desired output) in pandas versions from 1.2.3 to 1.3.5 (I did not test before 1.2.3):

    a    b
0   2  3.0
1   4  4.0
2   1  5.0
3   5  2.0
4  56  NaN
5   6  7.0
6   1  5.0
7   2  3.0
8   1  5.0
9   0  NaN

As of version 1.4.0 (and above), the same code produces the following error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/sfleming/opt/anaconda3/envs/test_installs/lib/python3.8/site-packages/pandas/core/indexing.py", line 2273, in __setitem__
    return super().__setitem__(key, value)
  File "/Users/sfleming/opt/anaconda3/envs/test_installs/lib/python3.8/site-packages/pandas/core/indexing.py", line 2228, in __setitem__
    self.obj._set_value(*key, value=value, takeable=self._takeable)
  File "/Users/sfleming/opt/anaconda3/envs/test_installs/lib/python3.8/site-packages/pandas/core/frame.py", line 3871, in _set_value
    loc = self.index.get_loc(index)
  File "/Users/sfleming/opt/anaconda3/envs/test_installs/lib/python3.8/site-packages/pandas/core/indexes/range.py", line 388, in get_loc
    self._check_indexing_error(key)
  File "/Users/sfleming/opt/anaconda3/envs/test_installs/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 5637, in _check_indexing_error
    raise InvalidIndexError(key)
pandas.errors.InvalidIndexError: 0    False
1    False
2    False
3    False
4     True
5    False
6    False
7    False
8    False
9     True
Name: b, dtype: bool

Is this a bug? Should this work?

If this is the desired behavior, a more descriptive error message would help others debug much faster.

@sjfleming sjfleming added Needs Triage Issue that has not been reviewed by a pandas team member Usage Question labels Mar 9, 2023
@MarcoGorelli
Copy link
Member

when I try this I get

InvalidIndexError: You can only assign a scalar value not a <class 'float'>

so it looks like a clear error message has already been added

@MarcoGorelli
Copy link
Member

closing as a duplicate of #48729 then

@sjfleming
Copy link
Author

sjfleming commented Mar 9, 2023

Hmm, I'm not sure I agree with you. (Also not sure why I'm not actually seeing that error message.) "You can only assign a scalar value not a <class 'float'>" does not tell me that the problem is that .at can only modify one dataframe element at a time. A 'float' is a scalar value, so I find it confusing.

I gather that the new way to do this kind of multiple-element assignment is using .loc

@MarcoGorelli
Copy link
Member

not sure why I'm not actually seeing that error message

you might be on an old version of pandas, please try upgrading


but you're right, the error message probably can be improved for when you're trying to set a scalar but with a mask - reopening then

@MarcoGorelli MarcoGorelli reopened this Mar 9, 2023
@LFBJC
Copy link

LFBJC commented Sep 5, 2023

@MarcoGorelli I'd suggest something like this: "Tried to set a scalar value with a mask, please use a scalar index or use df.loc instead". What do you think?

@MarcoGorelli
Copy link
Member

i haven't looked at this closely, but it it's possible to add that error message in such a way that it wouldn't be incorrect in some cases, then that looks good

interested in opening a pull request?

@mroeschke mroeschke added Indexing Related to indexing on series/frames, not to indexes themselves Error Reporting Incorrect or improved errors from pandas and removed Usage Question Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 17, 2024
@mroeschke mroeschke changed the title QST: InvalidIndexError using at for multiple assignment in v1.4.0+ ENH: InvalidIndexError using at for multiple assignment Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Error Reporting Incorrect or improved errors from pandas Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

No branches or pull requests

4 participants