From ce6e153739faa7d6a9ac56dc335ddfb090548b9c Mon Sep 17 00:00:00 2001 From: phofl Date: Sat, 14 Aug 2021 21:29:50 +0200 Subject: [PATCH 1/2] Fix len one list dataframe constructor bug for datetime --- doc/source/whatsnew/v1.3.2.rst | 1 + pandas/core/construction.py | 1 - pandas/tests/frame/test_constructors.py | 15 +++++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.3.2.rst b/doc/source/whatsnew/v1.3.2.rst index a94eab960418b..fdb932bcc4052 100644 --- a/doc/source/whatsnew/v1.3.2.rst +++ b/doc/source/whatsnew/v1.3.2.rst @@ -26,6 +26,7 @@ Fixed regressions - Fixed regression in :func:`concat` where ``copy=False`` was not honored in ``axis=1`` Series concatenation (:issue:`42501`) - Regression in :meth:`Series.nlargest` and :meth:`Series.nsmallest` with nullable integer or float dtype (:issue:`42816`) - Fixed regression in :meth:`Series.quantile` with :class:`Int64Dtype` (:issue:`42626`) +- Fixed regression in :class:`DataFrame` constructor failing to broadcast for defined :class:`Index` and len one list of :class:`Timestamp` (:issue:`42810`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/construction.py b/pandas/core/construction.py index f84aaa907f3fc..7215cd17409cb 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -550,7 +550,6 @@ def sanitize_array( subarr = subarr.astype(dtype, copy=copy) elif copy: subarr = subarr.copy() - return subarr else: if isinstance(data, (set, frozenset)): diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index 71f9544df42a3..6bf12a843eb9a 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -1300,6 +1300,21 @@ def test_constructor_unequal_length_nested_list_column(self): with pytest.raises(ValueError, match=msg): DataFrame([[1, 2, 3, 4], [4, 5, 6, 7]], columns=arrays) + @pytest.mark.parametrize( + "data", + [ + [[Timestamp("2021-01-01")]], + [{"x": Timestamp("2021-01-01")}], + {"x": [Timestamp("2021-01-01")]}, + {"x": Timestamp("2021-01-01")}, + ], + ) + def test_constructor_one_element_data_list(self, data): + # GH#42810 + result = DataFrame(data, index=[0, 1, 2], columns=["x"]) + expected = DataFrame({"x": [Timestamp("2021-01-01")] * 3}) + tm.assert_frame_equal(result, expected) + def test_constructor_sequence_like(self): # GH 3783 # collections.Sequence like From c04d742cd032b5f4bb88f7532b2aa12fe88f2579 Mon Sep 17 00:00:00 2001 From: phofl Date: Mon, 16 Aug 2021 08:48:22 +0200 Subject: [PATCH 2/2] Move whatsnew --- doc/source/whatsnew/v1.3.2.rst | 1 - doc/source/whatsnew/v1.3.3.rst | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.3.2.rst b/doc/source/whatsnew/v1.3.2.rst index 6a40186ac4e81..7a9549affef00 100644 --- a/doc/source/whatsnew/v1.3.2.rst +++ b/doc/source/whatsnew/v1.3.2.rst @@ -26,7 +26,6 @@ Fixed regressions - Fixed regression in :func:`concat` where ``copy=False`` was not honored in ``axis=1`` Series concatenation (:issue:`42501`) - Regression in :meth:`Series.nlargest` and :meth:`Series.nsmallest` with nullable integer or float dtype (:issue:`42816`) - Fixed regression in :meth:`Series.quantile` with :class:`Int64Dtype` (:issue:`42626`) -- Fixed regression in :class:`DataFrame` constructor failing to broadcast for defined :class:`Index` and len one list of :class:`Timestamp` (:issue:`42810`) .. --------------------------------------------------------------------------- diff --git a/doc/source/whatsnew/v1.3.3.rst b/doc/source/whatsnew/v1.3.3.rst index 78f1883695c47..9c76610124846 100644 --- a/doc/source/whatsnew/v1.3.3.rst +++ b/doc/source/whatsnew/v1.3.3.rst @@ -14,7 +14,7 @@ including other versions of pandas. Fixed regressions ~~~~~~~~~~~~~~~~~ -- +- Fixed regression in :class:`DataFrame` constructor failing to broadcast for defined :class:`Index` and len one list of :class:`Timestamp` (:issue:`42810`) - .. ---------------------------------------------------------------------------