-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Select numeric ExtensionDtypes with DataFrame.select_dtypes #35341
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 5 commits
503f9fc
ea1796f
3932391
7cf6e3e
0cb170a
83c84ba
1087ff7
7fe272f
77243fd
eb475a8
9b5db9a
9cbf294
4c1d67a
14fabfc
da6cf68
3801310
9e23d20
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import pytest | ||
import numpy as np | ||
|
||
import pandas as pd | ||
from pandas.core.arrays import ExtensionArray | ||
from pandas.core.dtypes.dtypes import ExtensionDtype | ||
|
||
|
||
class DummyDtype(ExtensionDtype): | ||
type = int | ||
_numeric = False | ||
|
||
@property | ||
def name(self): | ||
return "Dummy" | ||
|
||
@property | ||
def _is_numeric(self): | ||
return self._numeric | ||
|
||
|
||
class DummyArray(ExtensionArray): | ||
_dtype = DummyDtype() | ||
|
||
def __init__(self, data): | ||
self.data = data | ||
|
||
def __array__(self, dtype): | ||
return self.data | ||
|
||
@property | ||
def dtype(self): | ||
return self._dtype | ||
|
||
def __len__(self) -> int: | ||
return len(self.data) | ||
|
||
def __getitem__(self, item): | ||
pass | ||
|
||
|
||
@pytest.mark.parametrize("numeric", [True, False]) | ||
def test_select_dtypes_numeric(numeric): | ||
da = DummyArray([1, 2]) | ||
da._dtype._numeric = numeric | ||
df = pd.DataFrame(da) | ||
if numeric: | ||
assert df.select_dtypes(np.number).shape == df.shape | ||
else: | ||
assert df.select_dtypes(np.number).shape != df.shape | ||
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. the extension tests are predominantly testing specific EAs. could you add a test to pandas\tests\extension\base\dtype.py instead and override as necessary for the different EAs. 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. My intention isn't to test the different EAs, but to test a dummy EA that emulates an external EA, like PintArray. The most similar tests I could find were in pandas\tests\extension\test_common.py. Perhaps I should add the tests there? |
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.
not looked at this in detail, but from the name of the function, i'm assuming that this is not the correct location for this special casing.
There is another issue regarding extension arrays and dtypes, #9581. maybe a compat function for np.issubdtype could help, where we could add the special cases for EAs.
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.
OK I've added function to pandas\compat\numpy_init_.py