-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Allow numeric ExtensionDtypes in DataFrame.select_dtypes #38246
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
Merged
Merged
Changes from 33 commits
Commits
Show all changes
36 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 13b1531
merge master
arw2019 41fd75e
pd namespace usage
arw2019 85d0ae8
remove tests from old location
arw2019 4445207
unused import
arw2019 31ef92f
consolidate return
arw2019 5d9d2c2
whatsnew
arw2019 5d60908
minimize diff
arw2019 b8495c3
Merge branch 'master' of https://github.com/pandas-dev/pandas into HEAD
arw2019 75ea3a2
don't make separate method + ignore mypy
arw2019 e84ecb0
Merge branch 'master' of https://github.com/pandas-dev/pandas into GH…
arw2019 1df34f5
review comment
arw2019 0b5a9ef
Merge branch 'master' of https://github.com/pandas-dev/pandas into GH…
arw2019 ce63fa7
move whatsnew to 1.3
arw2019 93c7403
parametrize test
arw2019 ef095b5
add
arw2019 7c25c80
Merge branch 'master' of https://github.com/pandas-dev/pandas into GH…
arw2019 9bbd980
review comment
arw2019 57b6126
remove commented code
arw2019 f0902be
Merge branch 'master' of https://github.com/pandas-dev/pandas into GH…
arw2019 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 |
---|---|---|
@@ -1,9 +1,46 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
from pandas.core.dtypes.dtypes import ExtensionDtype | ||
|
||
import pandas as pd | ||
from pandas import DataFrame, Timestamp | ||
import pandas._testing as tm | ||
from pandas.core.arrays import ExtensionArray | ||
|
||
|
||
class DummyDtype(ExtensionDtype): | ||
type = int | ||
|
||
def __init__(self, numeric): | ||
self._numeric = numeric | ||
|
||
@property | ||
def name(self): | ||
return "Dummy" | ||
|
||
@property | ||
def _is_numeric(self): | ||
return self._numeric | ||
|
||
|
||
class DummyArray(ExtensionArray): | ||
def __init__(self, data, dtype): | ||
self.data = data | ||
self._dtype = dtype | ||
|
||
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 | ||
|
||
|
||
class TestSelectDtypes: | ||
|
@@ -322,3 +359,24 @@ def test_select_dtypes_typecodes(self): | |
expected = df | ||
FLOAT_TYPES = list(np.typecodes["AllFloat"]) | ||
tm.assert_frame_equal(df.select_dtypes(FLOAT_TYPES), expected) | ||
|
||
@pytest.mark.parametrize( | ||
"arr,expected", | ||
( | ||
(np.array([1, 2], dtype=np.int32), True), | ||
(pd.array([1, 2], dtype="Int32"), True), | ||
(pd.array(["a", "b"], dtype="string"), False), | ||
(DummyArray([1, 2], dtype=DummyDtype(numeric=True)), True), | ||
(DummyArray([1, 2], dtype=DummyDtype(numeric=False)), False), | ||
), | ||
) | ||
def test_select_dtypes_numeric(self, arr, expected): | ||
# GH 35340 | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
df = DataFrame(arr) | ||
is_selected = df.select_dtypes(np.number).shape == df.shape | ||
assert is_selected == expected | ||
|
||
# da = DummyArray([1, 2], dtype=DummyDtype(numeric=False)) | ||
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. remove commented code 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. Done |
||
# df = 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.
use
getattr(unique_dtype, '_is_numeric', False)
as we don't actually require this on an EA type (maybe we should though)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.
Done