Skip to content
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 @@ -410,6 +410,7 @@ MultiIndex
I/O
^^^
- Bug in :class:`DataFrame` and :class:`Series` ``repr`` of :py:class:`collections.abc.Mapping`` elements. (:issue:`57915`)
- Bug in :meth:`DataFrame.to_dict` raises unnecessary ``UserWarning`` when columns are not unique and ``orient='tight'``. (:issue:`58281`)
- Bug in :meth:`DataFrame.to_excel` when writing empty :class:`DataFrame` with :class:`MultiIndex` on both axes (:issue:`57696`)
- Bug in :meth:`DataFrame.to_string` that raised ``StopIteration`` with nested DataFrames. (:issue:`16098`)
- Bug in :meth:`read_csv` raising ``TypeError`` when ``index_col`` is specified and ``na_values`` is a dict containing the key ``None``. (:issue:`57547`)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/methods/to_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def to_dict(
Return a collections.abc.MutableMapping object representing the
DataFrame. The resulting transformation depends on the `orient` parameter.
"""
if not df.columns.is_unique:
if orient != "tight" and not df.columns.is_unique:
warnings.warn(
"DataFrame columns are not unique, some columns will be omitted.",
UserWarning,
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/frame/methods/test_to_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,17 @@ def test_to_dict_masked_native_python(self):
result = df.to_dict(orient="records")
assert isinstance(result[0]["a"], int)

def test_to_dict_tight_no_warning_with_duplicate_column(self):
# GH#58281
df = DataFrame([[1, 2], [3, 4], [5, 6]], columns=["A", "A"])
assert df.to_dict(orient="tight") == {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you still assert that df.to_dict(orient="tight") doesn't raise a warning?

Also can you structure like

result = expected = assert result == expected 
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this time? Am I understanding correctly?

"index": [0, 1, 2],
"columns": ["A", "A"],
"data": [[1, 2], [3, 4], [5, 6]],
"index_names": [None],
"column_names": [None],
}


@pytest.mark.parametrize(
"val", [Timestamp(2020, 1, 1), Timedelta(1), Period("2020"), Interval(1, 2)]
Expand Down