From 79c51223d9e92bb2efed1b248056b730a71b12e7 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Tue, 17 Aug 2021 08:26:16 +0200 Subject: [PATCH] Backport PR #43041: Fix len one list dataframe constructor bug for datetime --- doc/source/whatsnew/v1.3.3.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.3.rst b/doc/source/whatsnew/v1.3.3.rst index b3cd6d00d4322..549d0dcb593fd 100644 --- a/doc/source/whatsnew/v1.3.3.rst +++ b/doc/source/whatsnew/v1.3.3.rst @@ -14,6 +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`) - Performance regression in :meth:`core.window.ewm.ExponentialMovingWindow.mean` (:issue:`42333`) - - diff --git a/pandas/core/construction.py b/pandas/core/construction.py index 68d7f6c6f8a22..9d48bf612eb11 100644 --- a/pandas/core/construction.py +++ b/pandas/core/construction.py @@ -552,7 +552,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 1d286e379da86..94fba0cbb90d5 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -1286,6 +1286,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