Skip to content

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

Merged
merged 6 commits into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions pandas/core/arrays/integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Copy link
Member

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).

Copy link
Contributor

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

values = np.empty(len(values))
values.fill(np.nan)
elif inferred_type not in ['floating', 'integer',
'mixed-integer', 'mixed-integer-float']:
raise TypeError("{} cannot be converted to an IntegerDtype".format(
values.dtype))

Expand Down
9 changes: 5 additions & 4 deletions pandas/tests/arrays/test_integer.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,9 @@ def test_integer_array_constructor():

@pytest.mark.parametrize('a, b', [
([1, None], [1, np.nan]),
pytest.param([None], [np.nan],
marks=pytest.mark.xfail(reason='GH-23224',
strict=True)),
([None], [np.nan]),
([None, np.nan], [np.nan, np.nan]),
([np.nan, np.nan], [np.nan, np.nan]),
])
def test_integer_array_constructor_none_is_nan(a, b):
result = integer_array(a)
Expand Down Expand Up @@ -559,7 +559,8 @@ def test_integer_array_constructor_copy():
1,
1.0,
pd.date_range('20130101', periods=2),
np.array(['foo'])])
np.array(['foo']),
[[1, 2], [3, 4]]])
def test_to_integer_array_error(values):
# error in converting existing arrays to IntegerArrays
with pytest.raises(TypeError):
Expand Down