Description
Code Sample, a copy-pastable example if possible
>>> df = pd.DataFrame(np.random.randn(10, 4))
>>> df.loc[:,0] is df[0]
True
>>> df.iloc[:,0] is df[0]
False
Problem description
Found when working on #16443.
Also the same in 0.19.2. I think it's this line where Series.from_array is called in DataFrame._ixs
that's creating a new object, since section
here is what's being returned. Weird though since default for from_array
is copy=False
-> df.iloc[:,0]
/Users/margaret/Documents/Projects/pandas/pandas/core/indexing.py(1330)__getitem__()
-> return self._getitem_tuple(key)
/Users/margaret/Documents/Projects/pandas/pandas/core/indexing.py(1669)_getitem_tuple()
-> return self._getitem_lowerdim(tup)
/Users/margaret/Documents/Projects/pandas/pandas/core/indexing.py(968)_getitem_lowerdim()
-> section = self._getitem_axis(key, axis=i)
/Users/margaret/Documents/Projects/pandas/pandas/core/indexing.py(1756)_getitem_axis()
-> return self._get_loc(key, axis=axis)
/Users/margaret/Documents/Projects/pandas/pandas/core/indexing.py(139)_get_loc()
-> return self.obj._ixs(key, axis=axis)
> /Users/margaret/Documents/Projects/pandas/pandas/core/frame.py(2096)_ixs()
-> result = self._constructor_sliced.from_array(values,
Unlike loc
which ultimately just calls df[0]
-> df.loc[:,0]
/Users/margaret/Documents/Projects/pandas/pandas/core/indexing.py(1329)__getitem__()
-> return self._getitem_tuple(key)
/Users/margaret/Documents/Projects/pandas/pandas/core/indexing.py(836)_getitem_tuple()
-> return self._getitem_lowerdim(tup)
/Users/margaret/Documents/Projects/pandas/pandas/core/indexing.py(967)_getitem_lowerdim()
-> section = self._getitem_axis(key, axis=i)
/Users/margaret/Documents/Projects/pandas/pandas/core/indexing.py(1556)_getitem_axis()
-> return self._get_label(key, axis=axis)
/Users/margaret/Documents/Projects/pandas/pandas/core/indexing.py(136)_get_label()
-> return self.obj._xs(label, axis=axis)
> /Users/margaret/Documents/Projects/pandas/pandas/core/generic.py(1932)xs()
-> return self[key]
Expected Output
Both loc and iloc should both return views here, so df.iloc[:,] is df[0]
should return True
Output of pd.show_versions()
pandas: 0.20.1
pytest: 3.1.0
pip: 9.0.1
setuptools: 27.2.0
Cython: None
numpy: 1.12.1
scipy: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
pandas_gbq: None
pandas_datareader: None