From 159427a4eec4f8dfe40e9c76c4ca1b5ae3694c2a Mon Sep 17 00:00:00 2001 From: Pietro Battiston Date: Thu, 14 Dec 2017 14:36:30 +0100 Subject: [PATCH] TST: test_cast_1d_array --- pandas/tests/dtypes/test_cast.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/pandas/tests/dtypes/test_cast.py b/pandas/tests/dtypes/test_cast.py index 82a35fa711e8c..daa27bcfd2b3f 100644 --- a/pandas/tests/dtypes/test_cast.py +++ b/pandas/tests/dtypes/test_cast.py @@ -21,7 +21,8 @@ infer_dtype_from_array, maybe_convert_string_to_object, maybe_convert_scalar, - find_common_type) + find_common_type, + construct_1d_array_from_listlike) from pandas.core.dtypes.dtypes import ( CategoricalDtype, DatetimeTZDtype, @@ -407,3 +408,24 @@ def test_period_dtype(self): np.dtype('datetime64[ns]'), np.object, np.int64]: assert find_common_type([dtype, dtype2]) == np.object assert find_common_type([dtype2, dtype]) == np.object + + @pytest.mark.parametrize('dtype', [int, float, str, object, None]) + @pytest.mark.parametrize('datum1', [1, 2., "3", (4, 5), [6, 7], None]) + @pytest.mark.parametrize('datum2', [8, 9., "10", (11, 12), [13, 14], None]) + def test_cast_1d_array(self, dtype, datum1, datum2): + data = [datum1, datum2] + try: + # Conversion to 1d array is possible if requested dtype is object + possible = dtype is object + # ... or the following succeeds _and_ the result has dimension 1: + possible = possible or np.array(data, dtype=dtype).ndim == 1 + if not possible: + exc = ValueError + except (ValueError, TypeError) as exception: + exc = type(exception) + + if possible: + assert list(construct_1d_array_from_listlike(data)) == data + else: + pytest.raises(exc, construct_1d_array_from_listlike, + data, dtype=dtype)