Skip to content

BUG: Partial support only for extension DTypes in pandas.core.dtypes.common #45417

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

Closed
3 tasks done
erezinman opened this issue Jan 17, 2022 · 4 comments
Closed
3 tasks done
Labels
Bug ExtensionArray Extending pandas with custom dtypes or arrays.

Comments

@erezinman
Copy link

Pandas version checks

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

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

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
import pandas.core.dtypes.common as pdc

pdc.is_bool_dtype(pd.BooleanDtype)

Issue Description

The above code returns a property and not bool.
The bug here is actually because of pandas.core.dtypes.common.get_dtype that doesn't deal with extension DTypes (I guess it should return the extension DType, but I'm unsure). On the other hand, if the get_dtype method should not work with extension DTypes (for some reason), then it should check if this in an extension DType prior to calling get_dtype.

Anyway, is_bool_dtype should return a boolean and it doesn't necessarily do so (and I'm sure that it's a problem with many other functions in this module).

This is also the issue with not inferring strings that represent extension DTypes. Calling

pdc.is_bool_dtype('boolean')

yields

AttributeError: 'str' object has no attribute '_is_boolean'

Thanks

p.s. Regarding checking the bug's existence, I encountered it in my python v.3.6.9 code (so it can't be updated to the latest), but I checked the code in the repo, and I'm quite sure that it should reproduce.

Expected Behavior

  1. get_dtype(ExtensionDtype) should return that DType.
  2. is_bool_dtype and similar should always return boolean values.
  3. is_bool_dtype('boolean') should not raise an exception.
  4. is_bool_dtype(pd.BooleanDtype) should return True (like pdc.is_bool_dtype(pd.Series([], dtype='boolean')) does)

Installed Versions

INSTALLED VERSIONS

commit : 2a7d332
python : 3.6.9.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-77-generic
Version : #86~18.04.1-Ubuntu SMP Fri Jun 18 01:23:22 UTC 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.1.2
numpy : 1.19.5
pytz : 2018.3
dateutil : 2.8.1
pip : 21.1.3
setuptools : 45.2.0
Cython : 0.29.21
pytest : 5.3.5
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.5.0
html5lib : 0.999999999
pymysql : None
psycopg2 : None
jinja2 : 2.11.1
IPython : 7.16.1
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : 0.8.7
fastparquet : 0.4.1
gcsfs : None
matplotlib : 3.3.4
numexpr : 2.7.2
odfpy : None
openpyxl : 3.0.7
pandas_gbq : None
pyarrow : 4.0.1
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : 1.4.17
tables : 3.6.1
tabulate : 0.8.7
xarray : 0.16.0
xlrd : 1.2.0
xlwt : None
numba : 0.48.0

@erezinman erezinman added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 17, 2022
@erezinman erezinman changed the title BUG: Missing support for extension DTypes in pandas.core.dtypes.common BUG: Partial support only for extension DTypes in pandas.core.dtypes.common Jan 17, 2022
@jbrockmendel
Copy link
Member

Best guess is that in pdc.pandas_dtype we return the class instead of giving an instance. I think changing that would solve this, not sure if the current behavior is intentional. I think changing this would also fix #31356

@mroeschke mroeschke added ExtensionArray Extending pandas with custom dtypes or arrays. and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 17, 2022
@erezinman
Copy link
Author

@jbrockmendel Yes, passing pd.BooleanDtype() and not pd.BooleanDtype does solve this issue, but this is not all that there is to it - there's also the exception raised by pdc.is_bool_dtype('boolean'). Also, all the internal dtypes in pandas are immutable, so this behavior of not recognizing pd.BooleanDtype as a valid DType is unexpected.

@erezinman
Copy link
Author

Moreover, is_bool_dtype should always return a boolean value or raise a TypeError.

patrickmckenna added a commit to patrickmckenna/pandas that referenced this issue May 24, 2022
And parameterize while we're at it.
@jreback
Copy link
Contributor

jreback commented May 25, 2022

the is_ only take instances and not classes themselves

so this is intentional and works correctly for non EA types

linked PR to address a better error message

@jreback jreback added this to the No action milestone May 25, 2022
@jreback jreback closed this as completed May 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug ExtensionArray Extending pandas with custom dtypes or arrays.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants