Skip to content

Commit b58e2b8

Browse files
authored
BUG: Series(list_of_tuples, dtype=PandasDtype(object)) (#39357)
1 parent 799143d commit b58e2b8

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

doc/source/whatsnew/v1.3.0.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ I/O
333333

334334
Period
335335
^^^^^^
336-
- 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:`??`)
336+
- 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`)
337337
-
338338
-
339339

@@ -383,7 +383,7 @@ ExtensionArray
383383
Other
384384
^^^^^
385385
- Bug in :class:`Index` constructor sometimes silently ignorning a specified ``dtype`` (:issue:`38879`)
386-
-
386+
- Bug in constructing a :class:`Series` from a list and a :class:`PandasDtype` (:issue:`39357`)
387387
-
388388

389389
.. ---------------------------------------------------------------------------

pandas/core/construction.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,11 @@ def _sanitize_ndim(
531531
elif result.ndim > 1:
532532
if isinstance(data, np.ndarray):
533533
raise ValueError("Data must be 1-dimensional")
534+
if is_object_dtype(dtype) and isinstance(dtype, ExtensionDtype):
535+
# i.e. PandasDtype("O")
536+
result = com.asarray_tuplesafe(data, dtype=object)
537+
cls = dtype.construct_array_type()
538+
result = cls._from_sequence(result, dtype=dtype)
534539
else:
535540
result = com.asarray_tuplesafe(data, dtype=dtype)
536541
return result

pandas/tests/extension/test_numpy.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -192,16 +192,6 @@ def test_take_series(self, data):
192192
# ValueError: PandasArray must be 1-dimensional.
193193
super().test_take_series(data)
194194

195-
def test_loc_iloc_frame_single_dtype(self, data, request):
196-
npdtype = data.dtype.numpy_dtype
197-
if npdtype == object:
198-
# GH#33125
199-
mark = pytest.mark.xfail(
200-
reason="GH#33125 astype doesn't recognize data.dtype"
201-
)
202-
request.node.add_marker(mark)
203-
super().test_loc_iloc_frame_single_dtype(data)
204-
205195

206196
class TestGroupby(BaseNumPyTests, base.BaseGroupbyTests):
207197
def test_groupby_extension_apply(

pandas/tests/series/test_constructors.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,6 +1622,14 @@ def test_constructor_infer_index_tz(self):
16221622
# it works! GH#2443
16231623
repr(series.index[0])
16241624

1625+
def test_constructor_with_pandas_dtype(self):
1626+
# going through 2D->1D path
1627+
vals = [(1,), (2,), (3,)]
1628+
ser = Series(vals)
1629+
dtype = ser.array.dtype # PandasDtype
1630+
ser2 = Series(vals, dtype=dtype)
1631+
tm.assert_series_equal(ser, ser2)
1632+
16251633

16261634
class TestSeriesConstructorIndexCoercion:
16271635
def test_series_constructor_datetimelike_index_coercion(self):

0 commit comments

Comments
 (0)