Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,7 @@ Indexing
^^^^^^^^
- Bug in :meth:`DataFrame.__getitem__` returning modified columns when called with ``slice`` in Python 3.12 (:issue:`57500`)
- Bug in :meth:`DataFrame.__getitem__` when slicing a :class:`DataFrame` with many rows raised an ``OverflowError`` (:issue:`59531`)
- Bug in :meth:`DataFrame.__setitem__` on an empty :class:`DataFrame` with a tuple corrupting the frame (:issue:`54385`)
- Bug in :meth:`DataFrame.from_records` throwing a ``ValueError`` when passed an empty list in ``index`` (:issue:`58594`)
- Bug in :meth:`DataFrame.loc` and :meth:`DataFrame.iloc` returning incorrect dtype when selecting from a :class:`DataFrame` with mixed data types. (:issue:`60600`)
- Bug in :meth:`DataFrame.loc` with inconsistent behavior of loc-set with 2 given indexes to Series (:issue:`59933`)
Expand Down
8 changes: 7 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5193,7 +5193,9 @@ def _validate_fill_value(self, value):
"""
dtype = self.dtype
if isinstance(dtype, np.dtype) and dtype.kind not in "mM":
# return np_can_hold_element(dtype, value)
if isinstance(value, tuple) and dtype != object:
# GH#54385
raise TypeError
try:
return np_can_hold_element(dtype, value)
except LossySetitemError as err:
Expand Down Expand Up @@ -6357,6 +6359,10 @@ def _find_common_type_compat(self, target) -> DtypeObj:
"""
target_dtype, _ = infer_dtype_from(target)

if isinstance(target, tuple):
# GH#54385
return np.dtype(object)

if using_string_dtype():
# special case: if left or right is a zero-length RangeIndex or
# Index[object], those can be created by the default empty constructors
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,15 @@ def test_loc_expansion_with_timedelta_type(self):
)
tm.assert_frame_equal(result, expected)

def test_setitem_tuple_key_in_empty_frame(self):
# GH#54385
df = DataFrame()
df[(0, 0)] = [1, 2, 3]

cols = Index([(0, 0)], tupleize_cols=False)
expected = DataFrame({(0, 0): [1, 2, 3]}, columns=cols)
tm.assert_frame_equal(df, expected)


class TestDataFrameSetItemSlicing:
def test_setitem_slice_position(self):
Expand Down
Loading