From f2b4e9df9635cc5c8fdc471f037229120baaf199 Mon Sep 17 00:00:00 2001 From: "H. Vetinari" Date: Wed, 20 Feb 2019 09:02:25 +0100 Subject: [PATCH 1/2] CLN: (re-)enable infer_dtype to catch complex --- pandas/_libs/lib.pyx | 4 ++++ pandas/tests/dtypes/test_inference.py | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 1f0f0a408aee8..34ceeb20e260e 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -939,6 +939,7 @@ _TYPE_MAP = { 'float32': 'floating', 'float64': 'floating', 'f': 'floating', + 'complex64': 'complex', 'complex128': 'complex', 'c': 'complex', 'string': 'string' if PY2 else 'bytes', @@ -1305,6 +1306,9 @@ def infer_dtype(value: object, skipna: object=None) -> str: elif is_decimal(val): return 'decimal' + elif is_complex(val): + return 'complex' + elif util.is_float_object(val): if is_float_array(values): return 'floating' diff --git a/pandas/tests/dtypes/test_inference.py b/pandas/tests/dtypes/test_inference.py index 49a66efaffc11..aced7c81df7e7 100644 --- a/pandas/tests/dtypes/test_inference.py +++ b/pandas/tests/dtypes/test_inference.py @@ -618,6 +618,30 @@ def test_decimals(self): result = lib.infer_dtype(arr, skipna=True) assert result == 'decimal' + def test_complex(self): + # gets cast to complex on array construction + arr = np.array([1.0, 2.0, 1 + 1j]) + result = lib.infer_dtype(arr, skipna=True) + assert result == 'complex' + + arr = np.array([1.0, 2.0, 1 + 1j], dtype='O') + result = lib.infer_dtype(arr, skipna=True) + assert result == 'mixed' + + # gets cast to complex on array construction + arr = np.array([1, np.nan, 1 + 1j]) + result = lib.infer_dtype(arr, skipna=True) + assert result == 'complex' + + arr = np.array([1.0, np.nan, 1 + 1j], dtype='O') + result = lib.infer_dtype(arr, skipna=True) + assert result == 'mixed' + + # complex with nans stays complex + arr = np.array([1 + 1j, np.nan, 3 + 3j], dtype='O') + result = lib.infer_dtype(arr, skipna=True) + assert result == 'complex' + def test_string(self): pass From 12740f2f878c6efc3b9831ca9e94dbbc50bd62d2 Mon Sep 17 00:00:00 2001 From: "H. Vetinari" Date: Wed, 20 Feb 2019 22:29:18 +0100 Subject: [PATCH 2/2] Review (jreback) --- pandas/tests/dtypes/test_inference.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/pandas/tests/dtypes/test_inference.py b/pandas/tests/dtypes/test_inference.py index aced7c81df7e7..187b37d4f788e 100644 --- a/pandas/tests/dtypes/test_inference.py +++ b/pandas/tests/dtypes/test_inference.py @@ -618,28 +618,35 @@ def test_decimals(self): result = lib.infer_dtype(arr, skipna=True) assert result == 'decimal' - def test_complex(self): + # complex is compatible with nan, so skipna has no effect + @pytest.mark.parametrize('skipna', [True, False]) + def test_complex(self, skipna): # gets cast to complex on array construction arr = np.array([1.0, 2.0, 1 + 1j]) - result = lib.infer_dtype(arr, skipna=True) + result = lib.infer_dtype(arr, skipna=skipna) assert result == 'complex' arr = np.array([1.0, 2.0, 1 + 1j], dtype='O') - result = lib.infer_dtype(arr, skipna=True) + result = lib.infer_dtype(arr, skipna=skipna) assert result == 'mixed' # gets cast to complex on array construction arr = np.array([1, np.nan, 1 + 1j]) - result = lib.infer_dtype(arr, skipna=True) + result = lib.infer_dtype(arr, skipna=skipna) assert result == 'complex' arr = np.array([1.0, np.nan, 1 + 1j], dtype='O') - result = lib.infer_dtype(arr, skipna=True) + result = lib.infer_dtype(arr, skipna=skipna) assert result == 'mixed' # complex with nans stays complex arr = np.array([1 + 1j, np.nan, 3 + 3j], dtype='O') - result = lib.infer_dtype(arr, skipna=True) + result = lib.infer_dtype(arr, skipna=skipna) + assert result == 'complex' + + # test smaller complex dtype; will pass through _try_infer_map fastpath + arr = np.array([1 + 1j, np.nan, 3 + 3j], dtype=np.complex64) + result = lib.infer_dtype(arr, skipna=skipna) assert result == 'complex' def test_string(self):