Skip to content

Commit daf07a6

Browse files
alanbatoTomAugspurger
authored andcommitted
WARN: add stacklevel to to_dict() UserWarning (#16927) (#16936)
* ERR: add stacklevel to to_dict() UserWarning (#16927) * TST: Add warning testing to to_dict() * Fix warning assertion on to_dict() test * Add github issue to documentation on to_dict() warning test
1 parent 794fd78 commit daf07a6

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

pandas/core/frame.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,8 @@ def to_dict(self, orient='dict', into=dict):
972972
"""
973973
if not self.columns.is_unique:
974974
warnings.warn("DataFrame columns are not unique, some "
975-
"columns will be omitted.", UserWarning)
975+
"columns will be omitted.", UserWarning,
976+
stacklevel=2)
976977
# GH16122
977978
into_c = standardize_mapping(into)
978979
if orient.lower().startswith('d'):

pandas/tests/frame/test_convert_to.py

+7
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,13 @@ def test_to_dict_errors(self, mapping):
216216
with pytest.raises(TypeError):
217217
df.to_dict(into=mapping)
218218

219+
def test_to_dict_not_unique_warning(self):
220+
# GH16927: When converting to a dict, if a column has a non-unique name
221+
# it will be dropped, throwing a warning.
222+
df = DataFrame([[1, 2, 3]], columns=['a', 'a', 'b'])
223+
with tm.assert_produces_warning(UserWarning):
224+
df.to_dict()
225+
219226
@pytest.mark.parametrize('tz', ['UTC', 'GMT', 'US/Eastern'])
220227
def test_to_records_datetimeindex_with_tz(self, tz):
221228
# GH13937

0 commit comments

Comments
 (0)