-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG GH23224 Allow integer_array to be initialized with all None #23237
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
Conversation
Hello @JustinZhengBC! Thanks for submitting the PR.
|
pandas/core/arrays/integer.py
Outdated
@@ -174,8 +174,12 @@ def coerce_to_array(values, dtype, mask=None, copy=False): | |||
inferred_type = infer_dtype(values) | |||
if inferred_type not in ['floating', 'integer', | |||
'mixed-integer', 'mixed-integer-float']: | |||
raise TypeError("{} cannot be converted to an IntegerDtype".format( | |||
values.dtype)) |
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.
put this before the
if inferred_type not in
and make that an elif
pandas/tests/arrays/test_integer.py
Outdated
[ | ||
(integer_array([None]), integer_array([np.nan])), | ||
(integer_array([None, np.nan]), integer_array([np.nan, np.nan]))]) | ||
def test_to_integer_array_none(result, expected): |
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.
can u also try an array of all np.nan
pandas/core/arrays/integer.py
Outdated
raise TypeError("{} cannot be converted to an IntegerDtype".format( | ||
values.dtype)) | ||
if inferred_type is 'mixed' and all(isna(x) for x in values): | ||
values = np.array([np.nan] * len(values)) # GH 23224 |
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.
you can use
isna(values).any() as we always return an. array
pandas/core/arrays/integer.py
Outdated
@@ -174,8 +174,12 @@ def coerce_to_array(values, dtype, mask=None, copy=False): | |||
inferred_type = infer_dtype(values) | |||
if inferred_type not in ['floating', 'integer', | |||
'mixed-integer', 'mixed-integer-float']: |
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 don’t recall if we are checking for exactly ndim == 1 right after we convert to ndarray
can u add this as well ( and some tests)
Codecov Report
@@ Coverage Diff @@
## master #23237 +/- ##
=========================================
Coverage ? 92.19%
=========================================
Files ? 169
Lines ? 50974
Branches ? 0
=========================================
Hits ? 46997
Misses ? 3977
Partials ? 0
Continue to review full report at Codecov.
|
lgtm. i think you need to merge master and push again. ping on green. |
@jreback green now. I also moved my test cases to another more appropriate test that someone added since I last pulled. |
pandas/core/arrays/integer.py
Outdated
if inferred_type not in ['floating', 'integer', | ||
'mixed-integer', 'mixed-integer-float']: | ||
if inferred_type is 'mixed' and isna(values).any(): | ||
values = np.array([np.nan] * len(values)) # GH 23224 |
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
values = np.empty(len(values))
values.fill(np.nan)
thanks @JustinZhengBC |
@@ -173,8 +173,11 @@ def coerce_to_array(values, dtype, mask=None, copy=False): | |||
values = np.array(values, copy=copy) | |||
if is_object_dtype(values): | |||
inferred_type = lib.infer_dtype(values) | |||
if inferred_type not in ['floating', 'integer', | |||
'mixed-integer', 'mixed-integer-float']: | |||
if inferred_type is 'mixed' and isna(values).any(): |
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.
Shoudn't this any
be all
?
Because now this allows something like this:
In [14]: pd.core.arrays.integer_array([np.nan, {'a':1}])
Out[14]: IntegerArray([nan, nan], dtype='Int64')
A cornercase, but basically any python object that is not a datetime or string (something that numpy will convert to an object array).
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.
good catch, yep need to have all here
@JustinZhengBC feel free to open a follow-up PR |
git diff upstream/master -u -- "*.py" | flake8 --diff
Allows for initialization of
integer_array
with[None]
, lists ofNone
, and lists made of onlyNone
and other na types