Assignment via .loc · Issue #27286 · pandas-dev/pandas (original) (raw)

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