You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
>>>d=pd.DataFrame({'a': range(5), 'b': range(2,7), 'c': range(3,8)})
>>>dabc00231134224533564467>>>d.loc[0:2, 0:2] =42# note that loc is used with an integer slice! should this raise?>>>d# this is somehow the expected resultabc04242314242424242533564467>>>d.iloc[0:2, 0:2] # expected resultab0424214242>>>d.loc[0:2, 0:2] # this is the same syntax as above, but raises
...
TypeError: cannotdosliceindexingon<class'pandas.core.indexes.base.Index'>withtheseindexers [0] of<class'int'>
Problem description
I'd expect a symmetry in d.loc.__getitem__ and d.loc.__setitem__. At the moment only getitem raises if integer slices are passed. Usage of .loc was probably a mistake, instead .iloc should have been used. Even if it works correctly, it is confusing for users.
Probably this is simply a missing exception in the special case of d.loc.__setitem__ being called with integer slices.
See #13831 for a very similar problems with pd.Series.
Expected Output
Assuming d.loc.__getitem__ should raise for integer slices (as it does), then also d.loc.__setitem__ should raise:
>>> d.loc[0:2, 0:2]
...
SomException: int & loc don't go together
@lumbric Thanks a lot for the report. That's certainly a bug as there should indeed be a symmetry between setitem/getitem (although not always, you can set with new labels to expand a dataframe in certain cases), and in any case: it is interpreting the 0:2 as positional which is the guarantee of loc that this should not happen.
It only seems to be the case for slicing, as a list of integers correctly raises:
In [9]: d.loc[0:2, [0, 1]] = 42
...
KeyError: "None of [Int64Index([0, 1], dtype='int64')] are in the [columns]"
Code Sample
Problem description
I'd expect a symmetry in
d.loc.__getitem__
andd.loc.__setitem__
. At the moment only getitem raises if integer slices are passed. Usage of.loc
was probably a mistake, instead.iloc
should have been used. Even if it works correctly, it is confusing for users.Probably this is simply a missing exception in the special case of
d.loc.__setitem__
being called with integer slices.See #13831 for a very similar problems with
pd.Series
.Expected Output
Assuming
d.loc.__getitem__
should raise for integer slices (as it does), then alsod.loc.__setitem__
should raise:Output of
pd.show_versions()
pandas: 0.24.2
pytest: 3.6.4
pip: 9.0.1
setuptools: 40.8.0
Cython: None
numpy: 1.16.2
scipy: 1.2.1
pyarrow: None
xarray: 0.11.3
IPython: 5.5.0
sphinx: 1.7.9
patsy: 0.4.1+dev
dateutil: 2.8.0
pytz: 2019.1
blosc: 1.5.1
bottleneck: 1.2.1
tables: 3.4.4
numexpr: 2.6.5
feather: None
matplotlib: 2.2.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml.etree: 4.2.5
bs4: 4.5.0
html5lib: 0.999999999
sqlalchemy: None
pymysql: None
psycopg2: 2.7.5 (dt dec pq3 ext lo64)
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None
The text was updated successfully, but these errors were encountered: