Skip to content
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

BUG: pd.core.dtypes.common.is_extension_array_dtype wrong output for some dtypes #40478

Closed
3 tasks done
siboehm opened this issue Mar 17, 2021 · 3 comments
Closed
3 tasks done
Labels
Dtype Conversions Unexpected or buggy dtype conversions ExtensionArray Extending pandas with custom dtypes or arrays.

Comments

@siboehm
Copy link
Contributor

siboehm commented Mar 17, 2021

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

from pandas.core.dtypes.common import is_extension_array_dtype
from pandas.core.base import ExtensionArray

dt = DatetimeArray(np.array([], dtype='datetime64[ns]'))
assert isinstance(dt, ExtensionArray) # passes
assert is_extension_array_dtype(dt) # fails
assert is_extension_array_dtype(dt.dtype) # fails

inter = pd.Interval(0, 5)
assert com.is_extension_array_dtype(inter) # fails

Similar problems for Period and timedelta64[ns].

Problem description

Some of it (example: DatetimeArray) is due to this commit: be68850#diff-e279f0dcc8af317412f2a0db83d50c2ffedcd0743af7da8545ff272ee6ebe2f3R1529

Where DatetimeArray implements ExtensionArray, but datetime64[ns] is a np dtype and doesn't (yet) implement ExtensionDtype.

Expected Output

Function should return true for all dtypes whose Array implements ExtensionArray.

I need this for #40450.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : a75152784c17a73627a414319f6c89324cfa2840
python : 3.8.6.final.0
python-bits : 64
OS : Linux
OS-release : 5.8.0-44-generic
Version : #50-Ubuntu SMP Tue Feb 9 06:29:41 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : C.UTF-8
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.3.0.dev0+1084.ga75152784c.dirty
numpy : 1.19.5
pytz : 2021.1
dateutil : 2.8.1
pip : 20.3.3
setuptools : 49.6.0.post20201009
Cython : 0.29.22
pytest : 6.2.2
hypothesis : 6.7.0
sphinx : 3.5.2
blosc : None
feather : None
xlsxwriter : 1.3.7
lxml.etree : 4.6.2
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.3
IPython : 7.21.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : 1.3.2
fsspec : 0.8.7
fastparquet : 0.5.0
gcsfs : 0.7.2
matplotlib : 3.3.4
numexpr : 2.7.3
odfpy : None
openpyxl : 3.0.6
pandas_gbq : None
pyarrow : 3.0.0
pyxlsb : None
s3fs : 0.4.2
scipy : 1.6.0
sqlalchemy : 1.3.23
tables : 3.6.1
tabulate : 0.8.9
xarray : 0.17.0
xlrd : 2.0.1
xlwt : 1.3.0
numba : 0.52.0

@siboehm siboehm added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 17, 2021
@jbrockmendel
Copy link
Member

In all the listed cases, the behavior is correct.

DatetimeArray[dt64ns] is an ExtensionArray, but its dtype is a numpy dtype, not an ExtensionDtype. Same for TimedeltaArray.

IntervalDtype and IntervalArray would both give True for is_extension_array_dtype, but pd.Interval is not a dtype, nor does it have a dtype.

@jbrockmendel
Copy link
Member

in general I advocate just checking for isinstance(arr, ExtensionArray) or isinstance(dtype, ExtensionDtype) where relevant.

@jreback jreback added this to the No action milestone Mar 21, 2021
@jreback jreback added ExtensionArray Extending pandas with custom dtypes or arrays. Dtype Conversions Unexpected or buggy dtype conversions and removed Needs Triage Issue that has not been reviewed by a pandas team member Bug labels Mar 21, 2021
@jreback jreback closed this as completed Mar 21, 2021
@siboehm
Copy link
Contributor Author

siboehm commented Mar 22, 2021

Agree about pd.Interval, but for DatetimeArray and Timedelta, the docstring of is_extension_array_dtype suggests otherwise:

Notes
-----
This checks whether an object implements the pandas extension
array interface. In pandas, this includes:
* Categorical
* Sparse
* Interval
* Period
* DatetimeArray
* TimedeltaArray

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dtype Conversions Unexpected or buggy dtype conversions ExtensionArray Extending pandas with custom dtypes or arrays.
Projects
None yet
Development

No branches or pull requests

3 participants