Skip to content

BUG? Indexing + assignment does not work if member selected after indexing #3340

Closed
@dengemann

Description

@dengemann

Hi there,

this is one that has puzzled me for quite some time, but somehow it never asked about it. It seems whether an conditional assignment to a df column is performed or not depends on the order of indexing / member getting inside the left-hand expression of the assignment statement. See this snippet:

import pandas as pd

# create some data
data = {'foo': list('abcde'),
        'bar': range(5)}
df = pd.DataFrame(data)

# create arbitrary index.
my_idx = df.bar > 2
print df[my_idx]

# assign to a  member which was accessed after indexing.
df[my_idx].foo = 'X'

# see there is no change
print df

# assign to member which was accessed before indexing.
df.foo[my_idx] = 'X'

# now it has worked
print df

Is there a deliberate reason for this behaviour? My feeling about this is that it may confuse new users and may be hard to track down, as this goes silently and looks like correct code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugEnhancementIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions