Skip to content

Commit d53ff7a

Browse files
Backport PR pandas-dev#39023: Fix regression in setitem when expanding DataFrame with specific column name format (pandas-dev#39032)
Co-authored-by: patrick <[email protected]>
1 parent 8267ca9 commit d53ff7a

File tree

4 files changed

+14
-1
lines changed

4 files changed

+14
-1
lines changed

doc/source/whatsnew/v1.2.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Fixed regressions
2020
- Fixed regression in repr of float-like strings of an ``object`` dtype having trailing 0's truncated after the decimal (:issue:`38708`)
2121
- Fixed regression in :meth:`DataFrame.groupby()` with :class:`Categorical` grouping column not showing unused categories for ``grouped.indices`` (:issue:`38642`)
2222
- Fixed regression in :meth:`DataFrame.any` and :meth:`DataFrame.all` not returning a result for tz-aware ``datetime64`` columns (:issue:`38723`)
23+
- Fixed regression in :meth:`DataFrame.__setitem__` raising ``ValueError`` when expanding :class:`DataFrame` and new column is from type ``"0 - name"`` (:issue:`39010`)
2324
- Fixed regression in :meth:`.GroupBy.sem` where the presence of non-numeric columns would cause an error instead of being dropped (:issue:`38774`)
2425
- Fixed regression in :func:`read_excel` with non-rawbyte file handles (:issue:`38788`)
2526
- 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`)

pandas/core/dtypes/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1382,7 +1382,7 @@ def is_bool_dtype(arr_or_dtype) -> bool:
13821382
return False
13831383
try:
13841384
dtype = get_dtype(arr_or_dtype)
1385-
except TypeError:
1385+
except (TypeError, ValueError):
13861386
return False
13871387

13881388
if isinstance(arr_or_dtype, CategoricalDtype):

pandas/tests/dtypes/test_common.py

+5
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,11 @@ def test_is_bool_dtype():
557557
assert com.is_bool_dtype("boolean")
558558

559559

560+
def test_is_bool_dtype_numpy_error():
561+
# GH39010
562+
assert not com.is_bool_dtype("0 - Name")
563+
564+
560565
@pytest.mark.filterwarnings("ignore:'is_extension_type' is deprecated:FutureWarning")
561566
@pytest.mark.parametrize(
562567
"check_scipy", [False, pytest.param(True, marks=td.skip_if_no_scipy)]

pandas/tests/frame/indexing/test_setitem.py

+7
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,13 @@ def test_setitem_listlike_views(self):
356356
expected = Series([100, 2, 3], name="a")
357357
tm.assert_series_equal(ser, expected)
358358

359+
def test_setitem_string_column_numpy_dtype_raising(self):
360+
# GH#39010
361+
df = DataFrame([[1, 2], [3, 4]])
362+
df["0 - Name"] = [5, 6]
363+
expected = DataFrame([[1, 2, 5], [3, 4, 6]], columns=[0, 1, "0 - Name"])
364+
tm.assert_frame_equal(df, expected)
365+
359366

360367
class TestDataFrameSetItemSlicing:
361368
def test_setitem_slice_position(self):

0 commit comments

Comments
 (0)