Skip to content

Inconsistent retrieval of DataFrame columns as Series. #33675

Closed
@geppi

Description

@geppi
  1. Retrieve a DataFrame column by attribute as a Series.
  2. Modify an element of this series.
  3. As expected the modification has no effect on the original DataFrame.
  4. But it changes the Series that is retrieved from a column by attribute or with the 'loc' method.
  5. In contrast the Series retrieved from a column using the 'iloc' method is still the original.

This is at least inconsistent. However, I would also expect to see no change of the retrieved Series under point 4.

import pandas as pd
pd.show_versions()
INSTALLED VERSIONS
------------------
commit           : None
python           : 3.7.4.final.0
python-bits      : 64
OS               : Windows
OS-release       : 10
machine          : AMD64
processor        : Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder        : little
LC_ALL           : None
LANG             : None
LOCALE           : None.None

pandas           : 1.0.3
numpy            : 1.18.1
pytz             : 2019.3
dateutil         : 2.8.1
pip              : 20.0.2
setuptools       : 40.8.0
Cython           : None
pytest           : None
hypothesis       : None
sphinx           : None
blosc            : None
feather          : None
xlsxwriter       : 1.2.8
lxml.etree       : 4.5.0
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : 2.10.3
IPython          : 7.11.1
pandas_datareader: None
bs4              : None
bottleneck       : None
fastparquet      : None
gcsfs            : None
lxml.etree       : 4.5.0
matplotlib       : 3.1.3
numexpr          : None
odfpy            : None
openpyxl         : 3.0.3
pandas_gbq       : None
pyarrow          : None
pytables         : None
pytest           : None
pyxlsb           : None
s3fs             : None
scipy            : None
sqlalchemy       : None
tables           : None
tabulate         : None
xarray           : None
xlrd             : None
xlwt             : None
xlsxwriter       : 1.2.8
numba            : None
df = pd.DataFrame([[1,2], [3,4]], index= ['a', 'b'], columns=['A', 'B'])
df
A B
a 1 2
b 3 4
s = df.A
s
a    1
b    3
Name: A, dtype: int64
s['c'] = 5
s
a    1
b    3
c    5
Name: A, dtype: int64
df
A B
a 1 2
b 3 4
df.A
a    1
b    3
c    5
Name: A, dtype: int64
df.loc[:,'A']
a    1
b    3
c    5
Name: A, dtype: int64
df.iloc[:,0]
a    1
b    3
Name: A, dtype: int64
s['b'] = 2
s
a    1
b    2
c    5
Name: A, dtype: int64
df
A B
a 1 2
b 3 4
df.A
a    1
b    2
c    5
Name: A, dtype: int64
df.loc[:,'A']
a    1
b    2
c    5
Name: A, dtype: int64
df.iloc[:,0]
a    1
b    3
Name: A, dtype: int64

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated 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