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
Please take this question lightly as asked from curiosity:
As I was trying to see how the slicing in MultiIndex works, I came across the following situation ↓
# Simple MultiIndex Creationindex=pd.MultiIndex.from_product([['a', 'c', 'b'], [1, 2]])
# Making Series with that MultiIndexdata=pd.Series(np.random.randint(10, size=6), index=index)
Returns:
a 1 5
2 0
c 1 8
2 6
b 1 6
2 3
dtype: int32
NOTE that the indices are not in the sorted order ie. a, c, b is the order which will result in the expected error that we want while slicing.
# When we do slicingdata.loc["a":"c"]
Errors like:
UnsortedIndexError
----> 1 data.loc["a":"c"]
UnsortedIndexError: 'Key length (1) was greater than MultiIndex lexsort depth (0)'
That's expected. But now, after doing the following steps:
# Making a DataFramedata=data.unstack()
# Redindexing - to unsort the indices like beforedata=data.reindex(["a", "c", "b"])
# Which looks like 12a50c86b63# Then again making seriesdata=data.stack()
# Reindex Again!data=data.reindex(["a", "c", "b"], level=0)
# Which looks like beforea1520c1826b1623dtype: int32
The Problem
So, now the process is: Series → Unstack → DataFrame → Stack → Series
Now, if I do the slicing like before (still on with the indices unsorted) we don't get any error!
# The same slicingdata.loc["a":"c"]
Results without an error:
a 1 5
2 0
c 1 8
2 6
dtype: int32
Even if the data.index.is_monotonic → False. Then still why can we slice?
So the question is: WHY?.
I hope you got the understanding of the situation here. Because see, the same series which was before giving the error, after the unstack and stack operation is not giving any error.
So is that a bug, or a new concept that I am missing here?
Thanks!
Aayush ∞ Shah
The text was updated successfully, but these errors were encountered:
I don't know whether it is relevent to #44752 I encountered, but one thing shared in common is that the _lexsort_depth (or is_lexsort) is not set right. In your example:
Please take this question lightly as asked from curiosity:
As I was trying to see how the slicing in MultiIndex works, I came across the following situation ↓
Returns:
NOTE that the indices are not in the sorted order ie.
a, c, b
is the order which will result in the expected error that we want while slicing.Errors like:
That's expected. But now, after doing the following steps:
The Problem
So, now the process is:
Series → Unstack → DataFrame → Stack → Series
Now, if I do the slicing like before (still on with the indices unsorted) we don't get any error!
Results without an error:
Even if the
data.index.is_monotonic
→False
. Then still why can we slice?So the question is: WHY?.
So is that a bug, or a new concept that I am missing here?
Thanks!
Aayush ∞ Shah
The text was updated successfully, but these errors were encountered: