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
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
importpandasaspddf=pd.DataFrame(dict(i=[1,2,3], j=[1,1,2], x=[10, 100, 1000])).set_index(["i", "j"])
key= (1, 1)
# Returns DataFrame as expected:result1=df.xs(key, drop_level=False, level=list(range(len(key))))
# Returns Series, but DataFrame was expected:result2=df.xs(key, drop_level=False)
Issue Description
The drop_level=False option in DataFrame.xs apparently has no effect when level= is left at its default option. The documentation states that level= being unset should be equivalent to something like level=level=list(range(len(key))) when key is a non-string sequence.
Expected Behavior
I expected drop_level=False to have the same effect regardless of whether level= was specified.
Thanks for the report! Confirmed on main - when drop_level=False we should be returning a DataFrame in all cases (as otherwise the index levels are indeed dropped). Further investigations and PRs to fix are welcome!
rhshadrach
added
Indexing
Related to indexing on series/frames, not to indexes themselves
and removed
Indexing
Related to indexing on series/frames, not to indexes themselves
Needs Triage
Issue that has not been reviewed by a pandas team member
labels
Nov 6, 2024
Pandas version checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
Issue Description
The
drop_level=False
option inDataFrame.xs
apparently has no effect whenlevel=
is left at its default option. The documentation states thatlevel=
being unset should be equivalent to something likelevel=level=list(range(len(key)))
whenkey
is a non-string sequence.Expected Behavior
I expected
drop_level=False
to have the same effect regardless of whetherlevel=
was specified.Installed Versions
Output from
pd.show_versions()
The text was updated successfully, but these errors were encountered: