API: Inconsistent behavior with setting slices of Series indexed by MultiIndex #20414
Labels
API Design
good first issue
Indexing
Related to indexing on series/frames, not to indexes themselves
MultiIndex
Needs Tests
Unit test(s) needed to prevent regressions
Milestone
Code Sample, a copy-pastable example if possible
Problem description
This is a bit related to #10440 . In the above code, if we slice where we fix the value of the third level, then we can change the slice to a constant. We can also add that slice to a Series that has an Index that matches the first 2 levels.
In v0.22.0 of pandas, the result of the lines
is (as shown above)
But in the development version 0.23 of pandas, the "correct" result is given:
So I then would expect that the last 2 examples, using
s4
, would work in v0.23 development, because the only difference is that I am fixing the value of the first level in the slice, as opposed to the last level of the slice. But in both of those cases, I get this error (independent of the pandas version):So there is a bit of an inconsistency in that a slice that fixes the last level allows the addition and assignment operations to work (and it is better with v0.23 development version than in v0.22 because the
NaN
values go away), but a slice that fixes the first level does not allow the operations to work.I'm not sure if this is a bug, or by design, or if the documentation needs to be clarified as to which type of slicing will allow the "setting" operation to work as expected. There is a line in the docs (http://pandas.pydata.org/pandas-docs/stable/advanced.html#using-slicers) that says "You can use a right-hand-side of an alignable object as well." At least to me, it's not clear what objects are considered "alignable".
In any case, the expected behavior should be clear in the documentation, and, IMHO, if you fix the value of the first index or the last index, the behavior should be consistent.
Expected Output
Output of
pd.show_versions()
INSTALLED VERSIONS
commit: None
python: 3.6.4.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None
pandas: 0.22.0
pytest: 3.3.2
pip: 9.0.1
setuptools: 38.4.0
Cython: 0.27.3
numpy: 1.14.0
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: 1.6.6
patsy: 0.5.0
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.4
feather: None
matplotlib: 2.1.2
openpyxl: 2.4.10
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.2
lxml: 4.1.1
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.2.1
pymysql: 0.7.11.None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
The text was updated successfully, but these errors were encountered: