From 13f8a48ef84486370886acf6eb6e342437dd251d Mon Sep 17 00:00:00 2001 From: phofl Date: Thu, 7 Jan 2021 20:24:04 +0100 Subject: [PATCH 1/3] Fix regression in setitem when expanding DataFrame with specific column name format --- doc/source/whatsnew/v1.2.1.rst | 1 + pandas/core/indexes/numeric.py | 4 +++- pandas/tests/frame/indexing/test_setitem.py | 7 +++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.2.1.rst b/doc/source/whatsnew/v1.2.1.rst index 5695c817b5a3a..39e5b67fbbc37 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/indexes/numeric.py b/pandas/core/indexes/numeric.py index 59793d1a63813..916c62b64c6d8 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -124,7 +124,9 @@ def _shallow_copy(self, values=None, name: Label = lib.no_default): @doc(Index._validate_fill_value) def _validate_fill_value(self, value): - if is_bool(value) or is_bool_dtype(value): + if isinstance(value, str): + raise TypeError + elif is_bool(value) or is_bool_dtype(value): # force conversion to object # so we don't lose the bools raise TypeError diff --git a/pandas/tests/frame/indexing/test_setitem.py b/pandas/tests/frame/indexing/test_setitem.py index 28b1f02ff020c..a838b09b39be6 100644 --- a/pandas/tests/frame/indexing/test_setitem.py +++ b/pandas/tests/frame/indexing/test_setitem.py @@ -366,6 +366,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): From 8efeec204fb130b311d2956d1048c61dc1875893 Mon Sep 17 00:00:00 2001 From: phofl Date: Thu, 7 Jan 2021 21:12:57 +0100 Subject: [PATCH 2/3] Change fix --- pandas/core/dtypes/common.py | 2 +- pandas/core/indexes/numeric.py | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/pandas/core/dtypes/common.py b/pandas/core/dtypes/common.py index 1993c41db03f8..9861a466b2d2f 100644 --- a/pandas/core/dtypes/common.py +++ b/pandas/core/dtypes/common.py @@ -1395,7 +1395,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/core/indexes/numeric.py b/pandas/core/indexes/numeric.py index 916c62b64c6d8..59793d1a63813 100644 --- a/pandas/core/indexes/numeric.py +++ b/pandas/core/indexes/numeric.py @@ -124,9 +124,7 @@ def _shallow_copy(self, values=None, name: Label = lib.no_default): @doc(Index._validate_fill_value) def _validate_fill_value(self, value): - if isinstance(value, str): - raise TypeError - elif is_bool(value) or is_bool_dtype(value): + if is_bool(value) or is_bool_dtype(value): # force conversion to object # so we don't lose the bools raise TypeError From 7d3f7186e6565275fd68fb7886f4204c566eb2e6 Mon Sep 17 00:00:00 2001 From: phofl Date: Thu, 7 Jan 2021 22:49:16 +0100 Subject: [PATCH 3/3] Add is bool dtype test --- pandas/tests/dtypes/test_common.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pandas/tests/dtypes/test_common.py b/pandas/tests/dtypes/test_common.py index 8df61394e8e7e..a5522e503c7f4 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)]