-
-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
Inherit pandas classes from abc.collections classes? #12056
Comments
I think you have misunderstood
So perhaps we can think about whether we want to make |
Cheers @kawochen. It looks like In [21]: df = pd.DataFrame()
In [22]: isinstance(df, collections.Mapping)
Out[22]: False
In [23]: isinstance(df, collections.Sized)
Out[23]: True |
@MaximilianR nope it doesn't have |
I agree |
@kawochen have a look at the In [27]: collections.Mapping.__subclasshook__??
Signature: collections.Mapping.__subclasshook__(C)
Source:
@classmethod
def __subclasshook__(cls, C):
if cls is Sized:
if any("__len__" in B.__dict__ for B in C.__mro__):
return True
return NotImplemented
File: /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/_collections_abc.py
Type: method So I think for these, you would need to inherit from |
oh you are right. It looks like you'd need to call |
Or add that as one of the classes |
Hmm |
That's true. It does break the contract a bit. I still think it's better than what exists now, albeit not perfect. And |
I also think that I think the following would work: Sequence.register(pandas.DataFrame) To my understanding it exposes everything necessary. |
Making DataFrame a subclass of Mapping would be problematic due to the semantics of DataFrame__len__. |
I don't think this is actually possible as we have some named properties e.g. index which have confliciing meaning with .index() iterables. |
What do people think about inheriting pandas classes from
abc.collections
classes?I think the main ones are
DataFrame
&Series
inheriting fromMapping
andIndex
fromSequence
.This would enable other libraries to infer classes' behavior in a tighter way, for example to work out whether an object is list-like but not dict-like, they can check the types, rather than
hasattr(obj, '__iter__') and not hasattr(obj, 'values')
. And it doesn't cost anything.But as far as I'm aware, other libraries in the pydata ecosystem don't do this, so this would be out of the norm.
https://docs.python.org/3/library/collections.abc.html
The text was updated successfully, but these errors were encountered: