From 8c3a680dc76690d41fc1b09060d329a1c18d50b4 Mon Sep 17 00:00:00 2001 From: patrick <61934744+phofl@users.noreply.github.com> Date: Fri, 8 Jan 2021 01:19:43 +0100 Subject: [PATCH] Backport PR #39023: Fix regression in setitem when expanding DataFrame with specific column name format --- doc/source/whatsnew/v1.2.1.rst | 1 + pandas/core/dtypes/common.py | 2 +- pandas/tests/dtypes/test_common.py | 5 +++++ pandas/tests/frame/indexing/test_setitem.py | 7 +++++++ 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.2.1.rst b/doc/source/whatsnew/v1.2.1.rst index baeca87b8c4f8..4b7a4180ee9f9 100644 --- a/doc/source/whatsnew/v1.2.1.rst +++ b/doc/source/whatsnew/v1.2.1.rst @@ -20,6 +20,7 @@ Fixed regressions - Fixed regression in repr of float-like strings of an ``object`` dtype having trailing 0's truncated after the decimal (:issue:`38708`) - Fixed regression in :meth:`DataFrame.groupby()` with :class:`Categorical` grouping column not showing unused categories for ``grouped.indices`` (:issue:`38642`) - Fixed regression in :meth:`DataFrame.any` and :meth:`DataFrame.all` not returning a result for tz-aware ``datetime64`` columns (:issue:`38723`) +- Fixed regression in :meth:`DataFrame.__setitem__` raising ``ValueError`` when expanding :class:`DataFrame` and new column is from type ``"0 - name"`` (:issue:`39010`) - Fixed regression in :meth:`.GroupBy.sem` where the presence of non-numeric columns would cause an error instead of being dropped (:issue:`38774`) - Fixed regression in :func:`read_excel` with non-rawbyte file handles (:issue:`38788`) - Bug in :meth:`read_csv` with ``float_precision="high"`` caused segfault or wrong parsing of long exponent strings. This resulted in a regression in some cases as the default for ``float_precision`` was changed in pandas 1.2.0 (:issue:`38753`) diff --git a/pandas/core/dtypes/common.py b/pandas/core/dtypes/common.py index d8b0ad739b056..73cf20979a8ad 100644 --- a/pandas/core/dtypes/common.py +++ b/pandas/core/dtypes/common.py @@ -1382,7 +1382,7 @@ def is_bool_dtype(arr_or_dtype) -> bool: return False try: dtype = get_dtype(arr_or_dtype) - except TypeError: + except (TypeError, ValueError): return False if isinstance(arr_or_dtype, CategoricalDtype): diff --git a/pandas/tests/dtypes/test_common.py b/pandas/tests/dtypes/test_common.py index 19d80b714a674..128f505402eff 100644 --- a/pandas/tests/dtypes/test_common.py +++ b/pandas/tests/dtypes/test_common.py @@ -557,6 +557,11 @@ def test_is_bool_dtype(): assert com.is_bool_dtype("boolean") +def test_is_bool_dtype_numpy_error(): + # GH39010 + assert not com.is_bool_dtype("0 - Name") + + @pytest.mark.filterwarnings("ignore:'is_extension_type' is deprecated:FutureWarning") @pytest.mark.parametrize( "check_scipy", [False, pytest.param(True, marks=td.skip_if_no_scipy)] diff --git a/pandas/tests/frame/indexing/test_setitem.py b/pandas/tests/frame/indexing/test_setitem.py index 19d2f8301037a..cedef4784e4a1 100644 --- a/pandas/tests/frame/indexing/test_setitem.py +++ b/pandas/tests/frame/indexing/test_setitem.py @@ -356,6 +356,13 @@ def test_setitem_listlike_views(self): expected = Series([100, 2, 3], name="a") tm.assert_series_equal(ser, expected) + def test_setitem_string_column_numpy_dtype_raising(self): + # GH#39010 + df = DataFrame([[1, 2], [3, 4]]) + df["0 - Name"] = [5, 6] + expected = DataFrame([[1, 2, 5], [3, 4, 6]], columns=[0, 1, "0 - Name"]) + tm.assert_frame_equal(df, expected) + class TestDataFrameSetItemSlicing: def test_setitem_slice_position(self):