Skip to content

REGR: Adding a row via DataFrame.at #48296

@rhshadrach

Description

@rhshadrach
Member
df = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})
df.at[2] = [5, 6]

On 1.4.x, this gives

     a    b
0  1.0  3.0
1  2.0  4.0
2  5.0  6.0

whereas on 1.5.0rc0 it gives pandas.errors.InvalidIndexError: slice(None, None, None). I didn't any mention of this in the whatsnew, and a git bisect points at #47074; cc @jorisvandenbossche.

The docs of DataFrame.at explicitly say a KeyError is raised when the label is not in the frame. It seems to me this operation should not be supported, and can be considered a bugfix, but would be better to raise KeyError: label 2 not found.

Activity

added
IndexingRelated to indexing on series/frames, not to indexes themselves
RegressionFunctionality that used to work in a prior pandas version
Needs DiscussionRequires discussion from core team before further action
on Aug 29, 2022
added this to the 1.5 milestone on Aug 29, 2022
phofl

phofl commented on Aug 29, 2022

@phofl
Member

Hm I think InvalidIndexError is ok too, since at is for one value only and this tries to update 2

although the message is not optimal

rhshadrach

rhshadrach commented on Aug 29, 2022

@rhshadrach
MemberAuthor

I see; the slice(None, None, None) is referring to the columns - thanks. That makes sense, but this doesn't raise on main:

df = pd.DataFrame({'a': [1, 2]})
df.at[3, 'a'] = 7

and instead adds a row. I believe this should raise a KeyError, and think it would be preferable for the OP example to also raise a KeyError. If the key were to exist, InvalidIndexError in the OP makes sense to me (perhaps with a better message if possible).

I plan to add a note in the whatsnew in the indexing bugfixes section to close this issue and open a new issue on changing the behavior of .at, but I think the behavior change should wait until after the 1.5. release.

phofl

phofl commented on Aug 29, 2022

@phofl
Member

This is probably related to #48224 somehow. Maybe you could combine them when opening a new issue

added
Needs TestsUnit test(s) needed to prevent regressions
and removed
Needs DiscussionRequires discussion from core team before further action
on Sep 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselvesNeeds TestsUnit test(s) needed to prevent regressionsRegressionFunctionality that used to work in a prior pandas version

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      Participants

      @rhshadrach@phofl

      Issue actions

        REGR: Adding a row via DataFrame.at · Issue #48296 · pandas-dev/pandas