Pandas version checks
-
[X] I have checked that this issue has not already been reported.
-
[X] 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 https://github.com/pandas-dev/pandas/issues/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
Comment From: Runningwater2357
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.
Comment From: jbrockmendel
[...] https://github.com/pandas-dev/pandas/issues/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.
Comment From: ssche
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