From 736f669f9ae979b070eefafef999fded984379bd Mon Sep 17 00:00:00 2001 From: Brock Date: Sat, 23 Jan 2021 13:46:51 -0800 Subject: [PATCH 1/4] BUG: Series(list_of_tuples, dtype=PandasDtype(object)) --- pandas/core/construction.py | 5 +++++ pandas/tests/extension/test_numpy.py | 10 ---------- pandas/tests/series/test_constructors.py | 8 ++++++++ 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/pandas/core/construction.py b/pandas/core/construction.py index 2980547e23f24..275e86577c8c5 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -531,6 +531,11 @@ def _sanitize_ndim( elif result.ndim > 1: if isinstance(data, np.ndarray): raise ValueError("Data must be 1-dimensional") + if is_object_dtype(dtype) and not isinstance(dtype, np.dtype): + # i.e. PandasDtype("O") + result = com.asarray_tuplesafe(data, dtype=object) + cls = dtype.construct_array_type() + result = cls._from_sequence(result, dtype=dtype) else: result = com.asarray_tuplesafe(data, dtype=dtype) return result diff --git a/pandas/tests/extension/test_numpy.py b/pandas/tests/extension/test_numpy.py index 1f0181eec8830..f1ebedb616600 100644 --- a/pandas/tests/extension/test_numpy.py +++ b/pandas/tests/extension/test_numpy.py @@ -190,16 +190,6 @@ def test_take_series(self, data): # ValueError: PandasArray must be 1-dimensional. super().test_take_series(data) - def test_loc_iloc_frame_single_dtype(self, data, request): - npdtype = data.dtype.numpy_dtype - if npdtype == object: - # GH#33125 - mark = pytest.mark.xfail( - reason="GH#33125 astype doesn't recognize data.dtype" - ) - request.node.add_marker(mark) - super().test_loc_iloc_frame_single_dtype(data) - class TestGroupby(BaseNumPyTests, base.BaseGroupbyTests): @skip_nested diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index 0565ca7a3476a..4605e0961189b 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -1622,6 +1622,14 @@ def test_constructor_infer_index_tz(self): # it works! GH#2443 repr(series.index[0]) + def test_constructor_with_pandas_dtype(self): + # going through 2D->1D path + vals = [(1,), (2,), (3,)] + ser = Series(vals) + dtype = ser.array.dtype # PandasDtype + ser2 = Series(vals, dtype=dtype) + tm.assert_series_equal(ser, ser2) + class TestSeriesConstructorIndexCoercion: def test_series_constructor_datetimelike_index_coercion(self): From d52e1a91f972bcb3bfc809b4ff65b9da8d3de461 Mon Sep 17 00:00:00 2001 From: Brock Date: Sat, 23 Jan 2021 15:55:39 -0800 Subject: [PATCH 2/4] mypy fixup --- pandas/core/construction.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/construction.py b/pandas/core/construction.py index 275e86577c8c5..640c8d66807ad 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -531,7 +531,7 @@ def _sanitize_ndim( elif result.ndim > 1: if isinstance(data, np.ndarray): raise ValueError("Data must be 1-dimensional") - if is_object_dtype(dtype) and not isinstance(dtype, np.dtype): + if is_object_dtype(dtype) and isinstance(dtype, ExtensionDtype): # i.e. PandasDtype("O") result = com.asarray_tuplesafe(data, dtype=object) cls = dtype.construct_array_type() From 1a3b757650e49ab1d3495dc70d3604ea9e47abf7 Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 24 Jan 2021 16:38:28 -0800 Subject: [PATCH 3/4] CI: fix PandasArray test --- pandas/tests/extension/test_numpy.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pandas/tests/extension/test_numpy.py b/pandas/tests/extension/test_numpy.py index 4e054a07d8ef1..753ad7bb3728d 100644 --- a/pandas/tests/extension/test_numpy.py +++ b/pandas/tests/extension/test_numpy.py @@ -313,6 +313,10 @@ def test_arith_series_with_scalar(self, data, all_arithmetic_operators): def test_arith_series_with_array(self, data, all_arithmetic_operators): super().test_arith_series_with_array(data, all_arithmetic_operators) + @skip_nested + def test_arith_frame_with_scalar(self, data, all_arithmetic_operators): + super().test_arith_frame_with_scalar(data, all_arithmetic_operators) + class TestPrinting(BaseNumPyTests, base.BasePrintingTests): pass From 3b606ace5736acfc3a213a4f901558bf450397cd Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 24 Jan 2021 18:15:10 -0800 Subject: [PATCH 4/4] whatsnew --- doc/source/whatsnew/v1.3.0.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 67f27a75c7071..ea6cc17567d4a 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -331,7 +331,7 @@ I/O Period ^^^^^^ -- Comparisons of :class:`Period` objects or :class:`Index`, :class:`Series`, or :class:`DataFrame` with mismatched ``PeriodDtype`` now behave like other mismatched-type comparisons, returning ``False`` for equals, ``True`` for not-equal, and raising ``TypeError`` for inequality checks (:issue:`??`) +- Comparisons of :class:`Period` objects or :class:`Index`, :class:`Series`, or :class:`DataFrame` with mismatched ``PeriodDtype`` now behave like other mismatched-type comparisons, returning ``False`` for equals, ``True`` for not-equal, and raising ``TypeError`` for inequality checks (:issue:`39274`) - - @@ -381,7 +381,7 @@ ExtensionArray Other ^^^^^ - Bug in :class:`Index` constructor sometimes silently ignorning a specified ``dtype`` (:issue:`38879`) -- +- Bug in constructing a :class:`Series` from a list and a :class:`PandasDtype` (:issue:`39357`) - .. ---------------------------------------------------------------------------