Skip to content

Commit 9b16b1e

Browse files
authored
BUG: DataFrame.__setitem__ raising ValueError with string indexer and empty df and df to set (#38931)
1 parent cd0224d commit 9b16b1e

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ Indexing
244244
- Bug in :meth:`CategoricalIndex.get_indexer` failing to raise ``InvalidIndexError`` when non-unique (:issue:`38372`)
245245
- Bug in inserting many new columns into a :class:`DataFrame` causing incorrect subsequent indexing behavior (:issue:`38380`)
246246
- Bug in :meth:`DataFrame.loc`, :meth:`Series.loc`, :meth:`DataFrame.__getitem__` and :meth:`Series.__getitem__` returning incorrect elements for non-monotonic :class:`DatetimeIndex` for string slices (:issue:`33146`)
247+
- Bug in :meth:`DataFrame.__setitem__` raising ``ValueError`` with empty :class:`DataFrame` and specified columns for string indexer and non empty :class:`DataFrame` to set (:issue:`38831`)
247248
- Bug in :meth:`DataFrame.iloc.__setitem__` and :meth:`DataFrame.loc.__setitem__` with mixed dtypes when setting with a dictionary value (:issue:`38335`)
248249
- Bug in :meth:`DataFrame.loc` dropping levels of :class:`MultiIndex` when :class:`DataFrame` used as input has only one row (:issue:`10521`)
249250
-

pandas/core/frame.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -3334,13 +3334,14 @@ def _ensure_valid_index(self, value):
33343334
"""
33353335
# GH5632, make sure that we are a Series convertible
33363336
if not len(self.index) and is_list_like(value) and len(value):
3337-
try:
3338-
value = Series(value)
3339-
except (ValueError, NotImplementedError, TypeError) as err:
3340-
raise ValueError(
3341-
"Cannot set a frame with no defined index "
3342-
"and a value that cannot be converted to a Series"
3343-
) from err
3337+
if not isinstance(value, DataFrame):
3338+
try:
3339+
value = Series(value)
3340+
except (ValueError, NotImplementedError, TypeError) as err:
3341+
raise ValueError(
3342+
"Cannot set a frame with no defined index "
3343+
"and a value that cannot be converted to a Series"
3344+
) from err
33443345

33453346
# GH31368 preserve name of index
33463347
index_copy = value.index.copy()

pandas/tests/frame/indexing/test_setitem.py

+10
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,16 @@ def test_setitem_bool_with_numeric_index(self, dtype):
338338

339339
tm.assert_index_equal(df.columns, expected_cols)
340340

341+
@pytest.mark.parametrize("indexer", ["B", ["B"]])
342+
def test_setitem_frame_length_0_str_key(self, indexer):
343+
# GH#38831
344+
df = DataFrame(columns=["A", "B"])
345+
other = DataFrame({"B": [1, 2]})
346+
df[indexer] = other
347+
expected = DataFrame({"A": [np.nan] * 2, "B": [1, 2]})
348+
expected["A"] = expected["A"].astype("object")
349+
tm.assert_frame_equal(df, expected)
350+
341351

342352
class TestDataFrameSetItemWithExpansion:
343353
def test_setitem_listlike_views(self):

0 commit comments

Comments
 (0)