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: np.issubdtype(ea_dtype, ...) raises TypeError: Cannot interpret '<EADtype>' as a data type #48237

Closed
2 of 3 tasks
ssche opened this issue Aug 24, 2022 · 3 comments
Closed
2 of 3 tasks
Labels
Bug Closing Candidate May be closeable, needs more eyeballs ExtensionArray Extending pandas with custom dtypes or arrays. Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@ssche
Copy link
Contributor

ssche commented Aug 24, 2022

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

from cyberpandas import IPArray
import pandas as pd
import numpy as np

# Create a dataframe to extract the `IPType` (I'm sure this can be shortened)
df = pd.DataFrame({
    'address': IPArray(['192.168.1.1', '192.168.1.10'])
})

ipdtype = df['address'].dtype

np.issubdtype(ipdtype, np.int64)

raises

Traceback (most recent call last):
  File "foo.py", line 13, in <module>
    np.issubdtype(ipdtype, np.int64)
  File "./venv/lib64/python3.8/site-packages/numpy/core/numerictypes.py", line 416, in issubdtype
    arg1 = dtype(arg1).type
TypeError: Cannot interpret '<cyberpandas.ip_array.IPType object at 0x7fd1dddb1640>' as a data type

Issue Description

np.issubdtype(...) raises an error for EADtype. I found this #27953 closed ticket which suggests that np.issubdtype should work, but I haven't had a chance to check how this was solved for CategoricalDtype (or if it's still working).

Expected Behavior

No exception, normal behaviour as other dtypes

Installed Versions

INSTALLED VERSIONS

commit : e8093ba
python : 3.8.13.final.0
python-bits : 64
OS : Linux
OS-release : 5.18.17-200.fc36.x86_64
Version : #1 SMP PREEMPT_DYNAMIC Thu Aug 11 14:36:06 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_AU.UTF-8
LOCALE : en_AU.UTF-8

pandas : 1.4.3
numpy : 1.23.2
pytz : 2022.2.1
dateutil : 2.8.2
setuptools : 59.6.0
pip : 21.3.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
markupsafe : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

@ssche ssche added Bug Needs Triage Issue that has not been reviewed by a pandas team member ExtensionArray Extending pandas with custom dtypes or arrays. labels Aug 24, 2022
@Runningwater2357
Copy link

Modify the issubtype method of numerictypes.py as follows:

@set_module('numpy')
def issubdtype(arg1, arg2):
    if not issubclass_(arg1, generic):
        if hasattr(arg1, 'type'):
            arg1 = arg1.type
        else:
            return False
    if not issubclass_(arg2, generic):
        arg2 = dtype(arg2).type

    return issubclass(arg1, arg2)

If arg1 is of the numpy data type and contains the type attribute, the subtype can be identified normally. (The NumPy data type has been tested and the function is normal.)
If arg1 is not a numpy data type and does not have the type attribute, it must not be a numpy subtype. In this case, false is returned.
If the type attribute exists, the system directly determines whether the type of arg1 is a subclass of the arg2 type. This does not cause abnormal running.

@jbrockmendel
Copy link
Member

[...] #27953 closed ticket which suggests that np.issubdtype should work

That isn't about np.issubdtype, but about a pandas constructor. We wouldn't except np.issubdtype to work on anything other than numpy dtypes.

@jbrockmendel jbrockmendel added the Closing Candidate May be closeable, needs more eyeballs label Nov 29, 2022
@ssche
Copy link
Contributor Author

ssche commented Nov 29, 2022

Fair enough - I think I got confused with #27953.

I was just looking for a function that is checking the dtype in a Numpy dtype/EA dtype agnostic manner. With your explanation, I assume this snippet works because the dtype happens to be a Numpy dtype.

>>> import numpy as np
>>> import pandas as pd
>>> 
>>> df = pd.DataFrame({'a': [1, 2, 3]})
>>> 
>>> np.issubdtype(df['a'].dtype, np.int64)
True

@ssche ssche closed this as completed Nov 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Closing Candidate May be closeable, needs more eyeballs ExtensionArray Extending pandas with custom dtypes or arrays. Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

3 participants