Skip to content

BUG: Series(list_of_tuples, dtype=PandasDtype(object)) #39357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
-
-

Expand Down Expand Up @@ -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`)
-

.. ---------------------------------------------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 4 additions & 10 deletions pandas/tests/extension/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down