Skip to content

df.loc[:, 'col'] returning a view, but df.loc[df.index, 'col'] returning a copy #15631

Open
@STguerin

Description

@STguerin

Code Sample, a copy-pastable example if possible

x = pd.DataFrame(zip(range(4), range(4)), columns=['a', 'b'])
print(x)
   a  b
0  0  0
1  1  1
2  2  2
3  3  3

q = x.loc[:, 'a']
q += 2
print(x)
   a  b
0  2  0
1  3  1
2  4  2
3  5  3

x = pd.DataFrame(zip(range(4), range(4)), columns=['a', 'b'])
print(x)
   a  b
0  0  0
1  1  1
2  2  2
3  3  3

q = x.loc[x.index, 'a']
q += 2
print(x)
   a  b
0  0  0
1  1  1
2  2  2
3  3  3

Problem description

[df.loc[:, 'col'] returning a view, but df.loc[df.index, 'col'] returning a copy, intended? how can I make sure it is returning a copy?]

Expected Output

I thought .loc[] was always returning a copy

pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.5.2.final.0 python-bits: 64 OS: Windows OS-release: 7 machine: AMD64 processor: Intel64 Family 6 Model 94 Stepping 3, GenuineIntel byteorder: little LC_ALL: None LANG: None pandas: 0.18.1 nose: 1.3.7 pip: 8.1.2 setuptools: 27.2.0 Cython: 0.24.1 numpy: 1.11.1 scipy: 0.18.1 statsmodels: 0.6.1 xarray: None IPython: 5.1.0 sphinx: 1.4.6 patsy: 0.4.1 dateutil: 2.5.3 pytz: 2016.6.1 blosc: None bottleneck: 1.1.0 tables: 3.2.2 numexpr: 2.6.1 matplotlib: 1.5.3 openpyxl: 2.3.2 xlrd: 1.0.0 xlwt: 1.1.2 xlsxwriter: 0.9.3 lxml: 3.6.4 bs4: 4.5.1 html5lib: None httplib2: None apiclient: None sqlalchemy: 1.0.13 pymysql: None psycopg2: None jinja2: 2.8 boto: 2.42.0 pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugCopy / view semanticsIndexingRelated to indexing on series/frames, not to indexes themselvesNeeds DiscussionRequires discussion from core team before further action

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions