-
-
Notifications
You must be signed in to change notification settings - Fork 19.1k
ENH: set __module__ on IndexSlice #60269
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
Changes from all commits
8f9604b
fdb9f5c
269289c
c6b5dbc
b4ca522
84c8258
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,10 @@ | |
LossySetitemError, | ||
) | ||
from pandas.errors.cow import _chained_assignment_msg | ||
from pandas.util._decorators import doc | ||
from pandas.util._decorators import ( | ||
doc, | ||
set_module, | ||
) | ||
|
||
from pandas.core.dtypes.cast import ( | ||
can_hold_element, | ||
|
@@ -98,7 +101,8 @@ | |
|
||
|
||
# the public IndexSlicerMaker | ||
class _IndexSlice: | ||
@set_module("pandas") | ||
class IndexSlice: | ||
""" | ||
Create an object to more easily perform multi-index slicing. | ||
|
||
|
@@ -145,9 +149,6 @@ def __getitem__(self, arg): | |
return arg | ||
|
||
|
||
IndexSlice = _IndexSlice() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wondering if we should keep a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From a quick github search I don't find much public use, but for example the stubs use the current import to get the class for type annotations There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no problem. can do that if you are happy with the rest of it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
hmm. I'll look in more detail (probs tomorrow now) before making the changes since if the class is needed for type annotations then perhaps the class should be exposed in pandas.api.typing and the repr should point to that. |
||
|
||
|
||
class IndexingMixin: | ||
""" | ||
Mixin for adding .loc/.iloc/.at/.iat to Dataframes and Series. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure about this....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The actual
IndexSlice
object is defined a bit lower asIndexSlice = _IndexSlice()
, so we would need to update the__module__
of that object, I thinkThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See
pandas/pandas/core/indexing.py
Line 148 in d3c595e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although, for a class instance, that doesn't actually seem to work:
In this case we might need to add a
def __repr__
to the_IndexSlice
class if we want to improve this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so it looks like the way this would be done is
instance.__class__.__module__ = 'new_module_name'
which changes the module attribute for the class which is what has been done anyway.the output for a class is of the form
<class 'pandas.core.indexing._IndexSlice'>
whereas the output for a class instance is of the form
<pandas.core.indexing._IndexSlice object at 0x7fda1576c280>
now changing the
__module__
attribute on the class (as done already in this PR) gives<pandas._IndexSlice object at 0x7f6442deff70>
which is not correct since `_IndexSlice is not a top level class.I'm assuming that if we want to change this, we would want
<pandas.IndexSlice object at 0x7f6442deff70>
? This would be technically incorrect also?in another discussion it was mentioned that the repr is not being used in IPython?
It appears that changing the
__name__
attribute on the_IndexSlice
class (or alias) doesn't get the desired result either.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that was about function objects, but here we have an instance of a class, and that should use its repr (that's how we define repr of other pandas objects)
But it seems you have found another solution anyway