-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
API: Change MultiIndex.levels/codes to use tuple
instead of FrozenList
?
#53531
Comments
Hi, While reading about Frozenlist I found a StackOverflow conversation (Link) and an answer from @jreback regarding the same.
And If planning to change to tuple is it the correct way? - def levels(self) -> FrozenList:
+ def levels(self) -> tuple:
# Use cache_readonly to ensure that self.get_locs doesn't repeatedly
# create new IndexEngine
# https://github.com/pandas-dev/pandas/issues/31648
result = [x._rename(name=name) for x, name in zip(self._levels, self._names)]
for level in result:
# disallow midx.levels[0].name = "foo"
level._no_setting_name = True
- return FrozenList(result)
+ return tuple(result) expected output >>> import pandas as pd
>>> import numpy as np
>>> arrays = [
... np.array(["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"]),
... np.array(["one", "two", "one", "two", "one", "two", "one", "two"]),
... ]
>>> tuples = list(zip(*arrays))
>>> index = pd.MultiIndex.from_tuples(tuples, names=["first", "second"])
>>> df = pd.DataFrame(np.random.randn(3, 8), index=["A", "B", "C"], columns=index)
>>> df.columns.levels
(Index(['bar', 'baz', 'foo', 'qux'], dtype='object', name='first'), Index(['one', 'two'], dtype='object', name='second'))
>>> |
Nice find @srkds! I'm not sure if any of those reason are still entirely applicable at this point. I would say if the custom FrozenList methods are not used extensively (e.g.
Yeah exactly |
Yeah sounds good.
So going for submitting a PR. |
take |
@srkds I would wait until there's a little more feedback before submitting a PR. Maybe there's a reason someone else might bring up why we should still use |
Yes, I agree with you👍 |
I think it can be done for levels/codes. So I'm +1, if there's no surprises after removing FrozenList. We still will use >>> mi = pd.MultiIndex.from_frame(df, names=[(1,), (4,)])
>>> mi.names
FrozenList([(1,), (4,)]) |
Looks pretty feasible internally based on #53582, but I think this may need to a hard 3.0 break as I don't think there's a clean way to deprecate this |
Yeah, agree with that. |
+1 FrozenList was introduced in #4039; specifically from #4039 (comment):
and #4039 (comment)
|
The two methods FrozenList implements that tuple does not are |
The doc of FrozenList says
which a
tuple
satisfies. Not sure why a tuple wasn't use originally but it may be simpler to use them instead of a customFrozenList
that acts like a tuple.The text was updated successfully, but these errors were encountered: