Skip to content

Commit

Permalink
BUG: Ignore warning for duplicate columns in to_dict when orient='t…
Browse files Browse the repository at this point in the history
…ight' (pandas-dev#58335)

* Ignore warning for duplicate columns in to_dict when orient='tight'

* Add whatsnew

* Update doc/source/whatsnew/v3.0.0.rst

Co-authored-by: Abdulaziz Aloqeely <52792999+Aloqeely@users.noreply.github.com>

* Update whatsnew and redefine duplicate columns

* Use assert instead

* assert not raise and equal

---------

Co-authored-by: Abdulaziz Aloqeely <52792999+Aloqeely@users.noreply.github.com>
  • Loading branch information
2 people authored and pmhatre1 committed May 7, 2024
1 parent b568226 commit e19a8ea
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
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
14 changes: 14 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,20 @@ 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"])
with tm.assert_produces_warning(None):
result = df.to_dict(orient="tight")
expected = {
"index": [0, 1, 2],
"columns": ["A", "A"],
"data": [[1, 2], [3, 4], [5, 6]],
"index_names": [None],
"column_names": [None],
}
assert result == expected


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

0 comments on commit e19a8ea

Please sign in to comment.