Skip to content

GOTCHA: You can't use dot notation to add a column to a DataFrame #7175

Closed
@AllenDowney

Description

@AllenDowney

I ran into a nasty gotcha I have not seen mentioned in the documentation. In general, you can refer to DataFrame columns like a dictionary key or like an object attribute. But the behavior is inconsistent. In particular, you can modify an existing column using dot notation, but you can't add a new column.

The following example demonstrates.

d = {'one' : pd.Series([1., 2., 3.], index=['a', 'b', 'c']),
     'two' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)

# this adds a column to df
df['three'] = df.two + 1

# this successfully modifies an existing column
df.one += 1

# this seems to work but does not add a column to df
df.four = df.two + 2

print df.three
print df.four
print df

Is this behavior unavoidable?

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocsError ReportingIncorrect or improved errors from pandas

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions