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

BUG: MultiIndex assignment via loc #35878

Closed
3 tasks done
matthewgilbert opened this issue Aug 24, 2020 · 2 comments · Fixed by #35914
Closed
3 tasks done

BUG: MultiIndex assignment via loc #35878

matthewgilbert opened this issue Aug 24, 2020 · 2 comments · Fixed by #35914
Labels
Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@matthewgilbert
Copy link
Contributor

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


The behavior of assignment via loc seems to have changed as of 1.0.0.

import pandas

TS = pandas.Timestamp

s = pandas.DataFrame([
    ["A1", TS("2020-06-15"), -573.75],
    ["A2", TS("2020-06-15"), -198.75],
    ["A3", TS("2020-06-15"), -1.25],
], columns=["sid", "date", "price"]).set_index(["sid", "date"])["price"]
s.loc[[]] = 0
print(s)
sid  date      
A1   2020-06-15    0.0
A2   2020-06-15    0.0
A3   2020-06-15    0.0
Name: price, dtype: float64

I would expect this to behave the same as in in 0.25.3 which would assign nothing. I assume this is a bug because I can't find any discussion of the changed behavior in the Changelog.

Note that selection or assignment without a MultiIndex behavior appears to be unchanged, i.e.

print(s.loc[[]])
Series([], dtype: int64)
s = pandas.DataFrame([
    ["A1", -573.75],
    ["A2", -198.75],
    ["A3", -1.25],
], columns=["sid", "price"]).set_index(["sid"])["price"]
s.loc[[]] = 0
print(s)
sid
A1   -573.75
A2   -198.75
A3     -1.25
Name: price, dtype: float64

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 068e654
python : 3.8.5.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-42-generic
Version : #46~18.04.1-Ubuntu SMP Fri Jul 10 07:21:24 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.2.0.dev0+141.g068e6542b
numpy : 1.19.1
pytz : 2020.1
dateutil : 2.8.1
pip : 20.2.2
setuptools : 49.6.0.post20200814
Cython : 0.29.21
pytest : 6.0.1
hypothesis : 5.27.0
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 7.17.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None

@matthewgilbert matthewgilbert added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 24, 2020
@dsaxton dsaxton added MultiIndex Regression Functionality that used to work in a prior pandas version and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 25, 2020
@dsaxton
Copy link
Member

dsaxton commented Aug 26, 2020

Thanks @matthewgilbert, I think this boils down to a problem with MultiIndex.get_loc. Currently we have

import pandas as pd                                                                                                                                                                                  

idx = pd.MultiIndex.from_tuples([("a", 1), ("b", 2)])                                                                                                                                                
idx.get_loc([])                                                                                                                                                                                      
# slice(0, 2, None)

which is obviously wrong. I think @jbrockmendel has investigated this before, e.g., could be related to comments here #31358. In the example above it seems idx.get_loc([]) should actually be raising (doesn't seem like valid input given the docstring / general behavior of the method)?

@jbrockmendel
Copy link
Member

Agreed get_loc with a list should always raise

@jreback jreback added this to the 1.1.2 milestone Sep 5, 2020
@jreback jreback added the Indexing Related to indexing on series/frames, not to indexes themselves label Sep 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants