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`) - .. --------------------------------------------------------------------------- diff --git a/pandas/core/construction.py b/pandas/core/construction.py index 2980547e23f24..640c8d66807ad 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 isinstance(dtype, ExtensionDtype): + # 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 4e054a07d8ef1..6cd87836afa39 100644 --- a/pandas/tests/extension/test_numpy.py +++ b/pandas/tests/extension/test_numpy.py @@ -192,16 +192,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): def test_groupby_extension_apply( @@ -313,6 +303,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 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):