From f0985d202514c7d908b0383e5d735d047d15295e Mon Sep 17 00:00:00 2001 From: Alan Velasco Date: Fri, 14 Jul 2017 23:05:56 -0500 Subject: [PATCH 1/4] ERR: add stacklevel to to_dict() UserWarning (#16927) --- pandas/core/frame.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 6559fc4c24ce2..44e43138e6911 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -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'): From f6a717956fadce3a5b4a480c25262f872b547536 Mon Sep 17 00:00:00 2001 From: Alan Velasco Date: Fri, 14 Jul 2017 23:52:27 -0500 Subject: [PATCH 2/4] TST: Add warning testing to to_dict() --- pandas/tests/frame/test_convert_to.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pandas/tests/frame/test_convert_to.py b/pandas/tests/frame/test_convert_to.py index 34dd138ee1c80..30d877a133542 100644 --- a/pandas/tests/frame/test_convert_to.py +++ b/pandas/tests/frame/test_convert_to.py @@ -216,6 +216,12 @@ def test_to_dict_errors(self, mapping): with pytest.raises(TypeError): df.to_dict(into=mapping) + def test_to_dict_not_unique_warning(self): + # 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']) + tm.assert_produces_warning(df.to_dict()) + @pytest.mark.parametrize('tz', ['UTC', 'GMT', 'US/Eastern']) def test_to_records_datetimeindex_with_tz(self, tz): # GH13937 From d00e6fd0ee5ab6272275d58ff373d31a497f0a1e Mon Sep 17 00:00:00 2001 From: Alan Velasco Date: Sat, 15 Jul 2017 00:02:56 -0500 Subject: [PATCH 3/4] Fix warning assertion on to_dict() test --- pandas/tests/frame/test_convert_to.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/tests/frame/test_convert_to.py b/pandas/tests/frame/test_convert_to.py index 30d877a133542..ada758ccbac44 100644 --- a/pandas/tests/frame/test_convert_to.py +++ b/pandas/tests/frame/test_convert_to.py @@ -220,7 +220,8 @@ def test_to_dict_not_unique_warning(self): # 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']) - tm.assert_produces_warning(df.to_dict()) + 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): From 96cc9baabbcbcfd3efae09f3a01425c9f0856bde Mon Sep 17 00:00:00 2001 From: Alan Velasco Date: Sat, 15 Jul 2017 09:59:39 -0500 Subject: [PATCH 4/4] Add github issue to documentation on to_dict() warning test --- pandas/tests/frame/test_convert_to.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/frame/test_convert_to.py b/pandas/tests/frame/test_convert_to.py index ada758ccbac44..629c695b702fe 100644 --- a/pandas/tests/frame/test_convert_to.py +++ b/pandas/tests/frame/test_convert_to.py @@ -217,8 +217,8 @@ def test_to_dict_errors(self, mapping): df.to_dict(into=mapping) def test_to_dict_not_unique_warning(self): - # When converting to a dict, if a column has a non-unique name it will - # be dropped, throwing a warning. + # 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()