Skip to content

Assignment via .loc #27286

Closed
Closed
@cruzzoe

Description

@cruzzoe

Code Sample, a copy-pastable example if possible

import pandas as pd

df = pd.DataFrame({'Data': ['afoo abc: agsegsegs', 'b def: eafsegsg', 'c ghi:']})
df2 = df

df2.loc[:, 'foo2']  = df2.Data.fillna('').astype(str).str.extract(r'.*(\w{3}).*')
print df2['foo2']

Problem description

When printing df2['foo2'] I get:
0 NaN
1 NaN
2 NaN

If instead I change the line assigning to 'foo2' to:

df2['foo2']  = df2.Data.fillna('').astype(str).str.extract(r'.*(\w{3}).*')

When printing df2['foo2'] I get:

0 egs
1 gsg
2 ghi

I would have expected df.loc[:, 'foo2'] and df['foo2'] assignments to behave the same way.

Output of pd.show_versions()

pandas: 0.23.4
numpy: 1.15.4
python: 2.7.14.final.0

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions