-
-
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
Closed
Closed
Changes from 13 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
503f9fc
select dtypes
andrewgsavage ea1796f
flake8
andrewgsavage 3932391
flake8
andrewgsavage 7cf6e3e
black
andrewgsavage 0cb170a
checks
andrewgsavage 83c84ba
create compat func
andrewgsavage 1087ff7
create compat func
andrewgsavage 7fe272f
create compat func
andrewgsavage 77243fd
test
andrewgsavage eb475a8
lint
andrewgsavage 9b5db9a
lint
andrewgsavage 9cbf294
remove import from pandas.core
andrewgsavage 4c1d67a
Merge branch 'master' into select_dtypes
andrewgsavage 14fabfc
move files, add docstring and examples
andrewgsavage da6cf68
isort, remove pint_pandas example
andrewgsavage 3801310
isort, remove pint_pandas example
andrewgsavage 9e23d20
remove unused type comment@
andrewgsavage File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
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 | ||
|
||
|
||
def test_select_dtypes_numeric(): | ||
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. all of this should go in tests/frame/methods/test_select_dtypes.py |
||
da = DummyArray([1, 2]) | ||
da._dtype._numeric = True | ||
df = pd.DataFrame(da) | ||
assert df.select_dtypes(np.number).shape == df.shape | ||
|
||
da = DummyArray([1, 2]) | ||
da._dtype._numeric = False | ||
df = pd.DataFrame(da) | ||
assert df.select_dtypes(np.number).shape != df.shape |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
this wouldn't g in here, better in pandas/core/dtypes/common.py
with a full doc-string & examples
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.
this is almost is_numeric, but with the twist about np.number
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.
I've moved it and added doc string and examples. I'm not aware of any pandas dtypes that have
_is_numeric = True
and don't inherit fromnp.number
.Should
is_numeric
return True for ExtensionDtypes that have_is_numeric = True
and don't inherit fromnp.number
?