-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
REF/API: Stricter extension checking. #22031
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 3 commits
0a3994b
b908a2f
34ff2cd
ec31716
94318e1
e92d75f
7cfbebf
2731c70
4d94a3b
bc24cd7
08d1c40
a97cec4
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 |
---|---|---|
|
@@ -1688,38 +1688,69 @@ def is_extension_type(arr): | |
return False | ||
|
||
|
||
def is_extension_array_dtype(arr_or_dtype): | ||
"""Check if an object is a pandas extension array type. | ||
def is_extension_array_dtype(arr): | ||
"""Check if an array object is a pandas extension array type. | ||
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. Can you clarify (maybe in extended summary paragraph or in parameters section) that this is "array-like" (also Series and Index, in practice anything that has a dtype property) |
||
|
||
Parameters | ||
---------- | ||
arr_or_dtype : object | ||
arr : object | ||
|
||
Returns | ||
------- | ||
bool | ||
|
||
Notes | ||
----- | ||
This checks whether an object implements the pandas extension | ||
This checks whether an array object implements the pandas extension | ||
array interface. In pandas, this includes: | ||
|
||
* Categorical | ||
* Interval | ||
|
||
Third-party libraries may implement arrays or types satisfying | ||
Third-party libraries may implement arrays satisfying | ||
this interface as well. | ||
""" | ||
from pandas.core.arrays import ExtensionArray | ||
|
||
if isinstance(arr_or_dtype, (ABCIndexClass, ABCSeries)): | ||
arr_or_dtype = arr_or_dtype._values | ||
See Also | ||
-------- | ||
is_extension_dtype : Similar method for dtypes. | ||
""" | ||
from pandas.core.dtypes.base import ExtensionDtype | ||
|
||
try: | ||
arr_or_dtype = pandas_dtype(arr_or_dtype) | ||
except TypeError: | ||
pass | ||
dtype = getattr(arr, 'dtype') | ||
except AttributeError: | ||
return False | ||
|
||
return isinstance(dtype, ExtensionDtype) | ||
|
||
return isinstance(arr_or_dtype, (ExtensionDtype, ExtensionArray)) | ||
|
||
def is_extension_dtype(dtype): | ||
"""Check if a dtype object is a pandas extension dtype. | ||
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. One question is whether we want to accept strings here as well? 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. That's a good point, I suspect we should accept strings that have been registered. I'll see what I can do. 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. meant for the comment here |
||
|
||
Parameters | ||
---------- | ||
arr : object | ||
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. array -> dtype |
||
|
||
Returns | ||
------- | ||
bool | ||
|
||
Notes | ||
----- | ||
This checks whether a dtype object implements the pandas extension | ||
array interface. In pandas, this includes: | ||
|
||
* CategoricalDtype | ||
* IntervalDtype | ||
|
||
Third-party libraries may implement dtypes satisfying | ||
this interface as well. | ||
|
||
See Also | ||
-------- | ||
is_extension_array_dtype : Similar method for arrays. | ||
""" | ||
return isinstance(dtype, ExtensionDtype) | ||
|
||
|
||
def is_complex_dtype(arr_or_dtype): | ||
|
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.
Wouldn't it be more logical to call this
is_extension_array
instead ofis_extension_array_dtype
? (I suppose the reason is backwards compatibility for at least one of the two use cases?)Because I find the 'dtype' part of the name quite confusing.