Skip to content

Commit

Permalink
WARN: add stacklevel to to_dict() UserWarning (#16927) (#16936)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
alanbato authored and TomAugspurger committed Jul 15, 2017
1 parent 794fd78 commit daf07a6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,8 @@ def to_dict(self, orient='dict', into=dict):
"""
if not self.columns.is_unique:
warnings.warn("DataFrame columns are not unique, some "
"columns will be omitted.", UserWarning)
"columns will be omitted.", UserWarning,
stacklevel=2)
# GH16122
into_c = standardize_mapping(into)
if orient.lower().startswith('d'):
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/frame/test_convert_to.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@ def test_to_dict_errors(self, mapping):
with pytest.raises(TypeError):
df.to_dict(into=mapping)

def test_to_dict_not_unique_warning(self):
# GH16927: When converting to a dict, if a column has a non-unique name
# it will be dropped, throwing a warning.
df = DataFrame([[1, 2, 3]], columns=['a', 'a', 'b'])
with tm.assert_produces_warning(UserWarning):
df.to_dict()

@pytest.mark.parametrize('tz', ['UTC', 'GMT', 'US/Eastern'])
def test_to_records_datetimeindex_with_tz(self, tz):
# GH13937
Expand Down

0 comments on commit daf07a6

Please sign in to comment.