Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iloc does not return a view on single slice #16593

Closed
margaret opened this issue Jun 3, 2017 · 2 comments
Closed

iloc does not return a view on single slice #16593

margaret opened this issue Jun 3, 2017 · 2 comments
Labels
Bug Copy / view semantics Indexing Related to indexing on series/frames, not to indexes themselves

Comments

@margaret
Copy link
Contributor

margaret commented Jun 3, 2017

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()

INSTALLED VERSIONS ------------------ commit: None python: 3.6.0.final.0 python-bits: 64 OS: Darwin OS-release: 15.6.0 machine: x86_64 processor: i386 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: en_US.UTF-8

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

@jreback jreback added Compat pandas objects compatability with Numpy or Python functions Difficulty Intermediate Indexing Related to indexing on series/frames, not to indexes themselves labels Jun 5, 2017
@jreback jreback added this to the Next Major Release milestone Jun 5, 2017
@mroeschke mroeschke added Bug and removed Compat pandas objects compatability with Numpy or Python functions labels Apr 10, 2020
@jbrockmendel
Copy link
Member

Is the issue that you're not getting the same Series object? Because you are getting a view on the underlying data.

@jbrockmendel
Copy link
Member

Closing as not a bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Copy / view semantics Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

No branches or pull requests

4 participants