Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Ignore warning for duplicate columns in to_dict when orient='tight' #58335

Merged
merged 10 commits into from
Apr 24, 2024
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 @@ -456,6 +456,7 @@ Other
- Bug in :func:`unique` on :class:`Index` not always returning :class:`Index` (:issue:`57043`)
- Bug in :meth:`DataFrame.eval` and :meth:`DataFrame.query` which caused an exception when using NumPy attributes via ``@`` notation, e.g., ``df.eval("@np.floor(a)")``. (:issue:`58041`)
- Bug in :meth:`DataFrame.sort_index` when passing ``axis="columns"`` and ``ignore_index=True`` and ``ascending=False`` not returning a :class:`RangeIndex` columns (:issue:`57293`)
- Bug in :meth:`DataFrame.to_dict` when ``orieent='tight'`` raise unnecessary ``UserWarning``. (:issue:`58281`)
luke396 marked this conversation as resolved.
Show resolved Hide resolved
- Bug in :meth:`DataFrame.transform` that was returning the wrong order unless the index was monotonically increasing. (:issue:`57069`)
- Bug in :meth:`DataFrame.where` where using a non-bool type array in the function would return a ``ValueError`` instead of a ``TypeError`` (:issue:`56330`)
- Bug in :meth:`Index.sort_values` when passing a key function that turns values into tuples, e.g. ``key=natsort.natsort_key``, would raise ``TypeError`` (:issue:`56081`)
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
12 changes: 12 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,18 @@ 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(
{
"A": [1, 2, 3],
"B": [4, 5, 6],
}
)
df.columns = ["A", "A"]
luke396 marked this conversation as resolved.
Show resolved Hide resolved
with tm.assert_produces_warning(None):
df.to_dict(orient="tight")
luke396 marked this conversation as resolved.
Show resolved Hide resolved


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