Repository: pandas-dev/pandas
Pull request: BUG GH23224 Allow integer_array to be initialized with all None
- [X] closes #23224
- [X] tests added / passed
- [X] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [ ] whatsnew entry

Allows for initialization of `integer_array` with `[None]`, lists of `None`, and lists made of only `None` and other na types

Problem statement:
Support None in integer_array
It mostly works

```python
In [1]: import pandas as pd

In [2]: pd.core.arrays.integer_array([0, 1, None])
Out[2]: IntegerArray([0, 1, nan], dtype='Int64')
```

aside from 

```pytb
In [3]: pd.core.arrays.integer_array([None])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-1010251caa97> in <module>
----> 1 pd.core.arrays.integer_array([None])

~/sandbox/pandas/pandas/core/arrays/integer.py in integer_array(values, dtype, copy)
    104     TypeError if incompatible types
    105     """
--> 106     values, mask = coerce_to_array(values, dtype=dtype, copy=copy)
    107     return IntegerArray(values, mask)
    108

~/sandbox/pandas/pandas/core/arrays/integer.py in coerce_to_array(values, dtype, mask, copy)
    177                                  'mixed-integer', 'mixed-integer-float']:
    178             raise TypeError("{} cannot be converted to an IntegerDtype".format(
--> 179                 values.dtype))
    180
    181     elif not (is_integer_dtype(values) or is_float_dtype(values)):

TypeError: object cannot be converted to an IntegerDtype
```

Hints from issue discussion:

Commit message: Fix existing tests

Patch to review:
diff --git a/pandas/core/arrays/integer.py b/pandas/core/arrays/integer.py
index 52762514d00c2..c178830dc444a 100644
--- a/pandas/core/arrays/integer.py
+++ b/pandas/core/arrays/integer.py
@@ -173,8 +173,10 @@ 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():
+            values = np.array([np.nan] * len(values))  # GH 23224
+        elif inferred_type not in ['floating', 'integer',
+                                   'mixed-integer', 'mixed-integer-float']:
             raise TypeError("{} cannot be converted to an IntegerDtype".format(
                 values.dtype))
 
diff --git a/pandas/tests/arrays/test_integer.py b/pandas/tests/arrays/test_integer.py
index 66d2baac8c91c..e6dae0ffaec28 100644
--- a/pandas/tests/arrays/test_integer.py
+++ b/pandas/tests/arrays/test_integer.py
@@ -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)
@@ -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):


Code review comments:
diff_hunk: @@ -173,8 +173,10 @@ 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():
+            values = np.array([np.nan] * len(values))  # GH 23224
path: pandas/core/arrays/integer.py
text: @user1:
use
```
values = np.empty(len(values))
values.fill(np.nan)
```