From 3cf3ed9c4165970c2be5f66ba3a777cd3fa5e7fa Mon Sep 17 00:00:00 2001 From: elmonsomiat Date: Fri, 6 Mar 2020 19:35:20 +0000 Subject: [PATCH 01/28] updated to dict function and tests involved --- .pre-commit-config.yaml | 8 ++++---- pandas/core/frame.py | 15 +++++++++------ pandas/tests/frame/methods/test_to_dict.py | 19 ++++++++++--------- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 896765722bf32..907291f24e19d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,18 +8,18 @@ repos: rev: 3.7.7 hooks: - id: flake8 - language: python_venv + language: python additional_dependencies: [flake8-comprehensions>=3.1.0] - id: flake8 name: flake8-pyx - language: python_venv + language: python files: \.(pyx|pxd)$ types: - file args: [--append-config=flake8/cython.cfg] - id: flake8 name: flake8-pxd - language: python_venv + language: python files: \.pxi\.in$ types: - file @@ -28,7 +28,7 @@ repos: rev: v4.3.21 hooks: - id: isort - language: python_venv + language: python exclude: ^pandas/__init__\.py$|^pandas/core/api\.py$ - repo: https://github.com/pre-commit/mirrors-mypy rev: v0.730 diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 60d89b2076e92..7babbc0499598 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1399,11 +1399,13 @@ def to_dict(self, orient="dict", into=dict): ) # GH16122 into_c = com.standardize_mapping(into) - if orient.lower().startswith("d"): + if orient == "dict": return into_c((k, v.to_dict(into)) for k, v in self.items()) - elif orient.lower().startswith("l"): + + elif orient == "list": return into_c((k, v.tolist()) for k, v in self.items()) - elif orient.lower().startswith("sp"): + + elif orient == "split": return into_c( ( ("index", self.index.tolist()), @@ -1417,9 +1419,10 @@ def to_dict(self, orient="dict", into=dict): ), ) ) - elif orient.lower().startswith("s"): + elif orient == "series": return into_c((k, com.maybe_box_datetimelike(v)) for k, v in self.items()) - elif orient.lower().startswith("r"): + + elif orient == "records": columns = self.columns.tolist() rows = ( dict(zip(columns, row)) @@ -1429,7 +1432,7 @@ def to_dict(self, orient="dict", into=dict): into_c((k, com.maybe_box_datetimelike(v)) for k, v in row.items()) for row in rows ] - elif orient.lower().startswith("i"): + elif orient == "index": if not self.index.is_unique: raise ValueError("DataFrame index must be unique for orient='index'.") return into_c( diff --git a/pandas/tests/frame/methods/test_to_dict.py b/pandas/tests/frame/methods/test_to_dict.py index cd9bd169322fd..f75a07d8aca5d 100644 --- a/pandas/tests/frame/methods/test_to_dict.py +++ b/pandas/tests/frame/methods/test_to_dict.py @@ -64,11 +64,12 @@ def test_to_dict_index_not_unique_with_index_orient(self): with pytest.raises(ValueError, match=msg): df.to_dict(orient="index") - def test_to_dict_invalid_orient(self): + @pytest.mark.parametrize("orient", ["d", "l", "r", "sp", "s", "i", "xinvalid"]) + def test_to_dict_invalid_orient(self, orient): df = DataFrame({"A": [0, 1]}) - msg = "orient 'xinvalid' not understood" + msg = f"orient '{orient}' not understood" with pytest.raises(ValueError, match=msg): - df.to_dict(orient="xinvalid") + df.to_dict(orient=orient) @pytest.mark.parametrize("mapping", [dict, defaultdict(list), OrderedDict]) def test_to_dict(self, mapping): @@ -81,19 +82,19 @@ def test_to_dict(self, mapping): for k2, v2 in v.items(): assert v2 == recons_data[k][k2] - recons_data = DataFrame(test_data).to_dict("l", mapping) + recons_data = DataFrame(test_data).to_dict("list", mapping) for k, v in test_data.items(): for k2, v2 in v.items(): assert v2 == recons_data[k][int(k2) - 1] - recons_data = DataFrame(test_data).to_dict("s", mapping) + recons_data = DataFrame(test_data).to_dict("series", mapping) for k, v in test_data.items(): for k2, v2 in v.items(): assert v2 == recons_data[k][k2] - recons_data = DataFrame(test_data).to_dict("sp", mapping) + recons_data = DataFrame(test_data).to_dict("split", mapping) expected_split = { "columns": ["A", "B"], "index": ["1", "2", "3"], @@ -101,7 +102,7 @@ def test_to_dict(self, mapping): } tm.assert_dict_equal(recons_data, expected_split) - recons_data = DataFrame(test_data).to_dict("r", mapping) + recons_data = DataFrame(test_data).to_dict("records", mapping) expected_records = [ {"A": 1.0, "B": "1"}, {"A": 2.0, "B": "2"}, @@ -113,7 +114,7 @@ def test_to_dict(self, mapping): tm.assert_dict_equal(l, r) # GH#10844 - recons_data = DataFrame(test_data).to_dict("i") + recons_data = DataFrame(test_data).to_dict("index") for k, v in test_data.items(): for k2, v2 in v.items(): @@ -121,7 +122,7 @@ def test_to_dict(self, mapping): df = DataFrame(test_data) df["duped"] = df[df.columns[0]] - recons_data = df.to_dict("i") + recons_data = df.to_dict("index") comp_data = test_data.copy() comp_data["duped"] = comp_data[df.columns[0]] for k, v in comp_data.items(): From 2e783e019afbdb2b097ef5a37f184b39ab9ed54a Mon Sep 17 00:00:00 2001 From: elmonsomiat Date: Fri, 6 Mar 2020 19:44:47 +0000 Subject: [PATCH 02/28] rolled back to python_env in yaml file --- .pre-commit-config.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 907291f24e19d..896765722bf32 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,18 +8,18 @@ repos: rev: 3.7.7 hooks: - id: flake8 - language: python + language: python_venv additional_dependencies: [flake8-comprehensions>=3.1.0] - id: flake8 name: flake8-pyx - language: python + language: python_venv files: \.(pyx|pxd)$ types: - file args: [--append-config=flake8/cython.cfg] - id: flake8 name: flake8-pxd - language: python + language: python_venv files: \.pxi\.in$ types: - file @@ -28,7 +28,7 @@ repos: rev: v4.3.21 hooks: - id: isort - language: python + language: python_venv exclude: ^pandas/__init__\.py$|^pandas/core/api\.py$ - repo: https://github.com/pre-commit/mirrors-mypy rev: v0.730 From dcc4c2ea9b287a4e69b91b3e1c2244aaefc249c9 Mon Sep 17 00:00:00 2001 From: elmonsomiat Date: Fri, 6 Mar 2020 19:46:09 +0000 Subject: [PATCH 03/28] added space between lines --- pandas/core/frame.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 7babbc0499598..9a98994ef8525 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1419,6 +1419,7 @@ def to_dict(self, orient="dict", into=dict): ), ) ) + elif orient == "series": return into_c((k, com.maybe_box_datetimelike(v)) for k, v in self.items()) From b755897cd4823337b4f52b63f09463128144637a Mon Sep 17 00:00:00 2001 From: elmonsomiat Date: Fri, 6 Mar 2020 19:50:34 +0000 Subject: [PATCH 04/28] added space between lines --- pandas/core/frame.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 9a98994ef8525..d20f35381ec3b 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1419,7 +1419,7 @@ def to_dict(self, orient="dict", into=dict): ), ) ) - + elif orient == "series": return into_c((k, com.maybe_box_datetimelike(v)) for k, v in self.items()) @@ -1433,6 +1433,7 @@ def to_dict(self, orient="dict", into=dict): into_c((k, com.maybe_box_datetimelike(v)) for k, v in row.items()) for row in rows ] + elif orient == "index": if not self.index.is_unique: raise ValueError("DataFrame index must be unique for orient='index'.") @@ -1440,6 +1441,7 @@ def to_dict(self, orient="dict", into=dict): (t[0], dict(zip(self.columns, t[1:]))) for t in self.itertuples(name=None) ) + else: raise ValueError(f"orient '{orient}' not understood") From dc1207bfce860153cb6baf7a6212825e31c2de60 Mon Sep 17 00:00:00 2001 From: elmonsomiat Date: Sat, 7 Mar 2020 12:18:25 +0000 Subject: [PATCH 05/28] documented tests0 --- pandas/tests/frame/methods/test_to_dict.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pandas/tests/frame/methods/test_to_dict.py b/pandas/tests/frame/methods/test_to_dict.py index f75a07d8aca5d..e206cb0813080 100644 --- a/pandas/tests/frame/methods/test_to_dict.py +++ b/pandas/tests/frame/methods/test_to_dict.py @@ -66,6 +66,9 @@ def test_to_dict_index_not_unique_with_index_orient(self): @pytest.mark.parametrize("orient", ["d", "l", "r", "sp", "s", "i", "xinvalid"]) def test_to_dict_invalid_orient(self, orient): + # to_dict(orient='d') should fail, as should only take the listed options + # see GH#32515 + df = DataFrame({"A": [0, 1]}) msg = f"orient '{orient}' not understood" with pytest.raises(ValueError, match=msg): @@ -73,6 +76,8 @@ def test_to_dict_invalid_orient(self, orient): @pytest.mark.parametrize("mapping", [dict, defaultdict(list), OrderedDict]) def test_to_dict(self, mapping): + # orient= should only take the listed options + # see GH#32515 test_data = {"A": {"1": 1, "2": 2}, "B": {"1": "1", "2": "2", "3": "3"}} # GH#16122 From 1a78f3c5739e63250db8ada4e14492075c13e216 Mon Sep 17 00:00:00 2001 From: elmonsomiat Date: Sat, 7 Mar 2020 12:24:28 +0000 Subject: [PATCH 06/28] formated to black --- pandas/core/frame.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index d20f35381ec3b..0264cc83d9885 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1441,7 +1441,7 @@ def to_dict(self, orient="dict", into=dict): (t[0], dict(zip(self.columns, t[1:]))) for t in self.itertuples(name=None) ) - + else: raise ValueError(f"orient '{orient}' not understood") From 43d702cb59c44b44ed3adbdbf2f432d6d3836808 Mon Sep 17 00:00:00 2001 From: elmonsomiat Date: Sat, 7 Mar 2020 12:52:14 +0000 Subject: [PATCH 07/28] added whats new --- doc/source/whatsnew/v1.1.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index ff9655ab7f177..0967e94a2fbf5 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -352,6 +352,7 @@ Other instead of ``TypeError: Can only append a Series if ignore_index=True or if the Series has a name`` (:issue:`30871`) - Set operations on an object-dtype :class:`Index` now always return object-dtype results (:issue:`31401`) - Bug in :meth:`AbstractHolidayCalendar.holidays` when no rules were defined (:issue:`31415`) +- Using an `orient` argument not listed inside :meth:`DataFrame.to_dict` will raise a ``ValueError`` (:issue:`32515`) .. --------------------------------------------------------------------------- From e52a3b2e87937964efd38b0b8e6eecfe59562c03 Mon Sep 17 00:00:00 2001 From: elmonsomiat Date: Sat, 7 Mar 2020 14:18:54 +0000 Subject: [PATCH 08/28] removed line from doc whats new --- doc/source/whatsnew/v1.1.0.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 0967e94a2fbf5..ff9655ab7f177 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -352,7 +352,6 @@ Other instead of ``TypeError: Can only append a Series if ignore_index=True or if the Series has a name`` (:issue:`30871`) - Set operations on an object-dtype :class:`Index` now always return object-dtype results (:issue:`31401`) - Bug in :meth:`AbstractHolidayCalendar.holidays` when no rules were defined (:issue:`31415`) -- Using an `orient` argument not listed inside :meth:`DataFrame.to_dict` will raise a ``ValueError`` (:issue:`32515`) .. --------------------------------------------------------------------------- From ad5afa5cd79056fab9905d7b1469f0987b7a88a0 Mon Sep 17 00:00:00 2001 From: elmonsomiat Date: Sat, 7 Mar 2020 15:32:23 +0000 Subject: [PATCH 09/28] added comment on other enhancements to test the ci docs --- doc/source/whatsnew/v1.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index ff9655ab7f177..8bf2762bb7f93 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -67,7 +67,7 @@ Other enhancements - When writing directly to a sqlite connection :func:`to_sql` now supports the ``multi`` method (:issue:`29921`) - `OptionError` is now exposed in `pandas.errors` (:issue:`27553`) - :func:`timedelta_range` will now infer a frequency when passed ``start``, ``stop``, and ``periods`` (:issue:`32377`) -- +- Using an `orient` argument not listed inside :meth:`DataFrame.to_dict` will raise a ``ValueError`` (:issue:32515) .. --------------------------------------------------------------------------- From f161803c1d97d944828e03617232bd7b679c8a96 Mon Sep 17 00:00:00 2001 From: elmonsomiat Date: Sat, 7 Mar 2020 16:02:24 +0000 Subject: [PATCH 10/28] removed changes from docs --- doc/source/whatsnew/v1.1.0.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 8bf2762bb7f93..2440c12048e20 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -67,7 +67,6 @@ Other enhancements - When writing directly to a sqlite connection :func:`to_sql` now supports the ``multi`` method (:issue:`29921`) - `OptionError` is now exposed in `pandas.errors` (:issue:`27553`) - :func:`timedelta_range` will now infer a frequency when passed ``start``, ``stop``, and ``periods`` (:issue:`32377`) -- Using an `orient` argument not listed inside :meth:`DataFrame.to_dict` will raise a ``ValueError`` (:issue:32515) .. --------------------------------------------------------------------------- From a2edef11f7a148cfa2fc570003124b2307b2038b Mon Sep 17 00:00:00 2001 From: elmonsomiat Date: Sat, 7 Mar 2020 16:05:42 +0000 Subject: [PATCH 11/28] reset whatsnew file --- doc/source/whatsnew/v1.1.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 2440c12048e20..ff9655ab7f177 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -67,6 +67,7 @@ Other enhancements - When writing directly to a sqlite connection :func:`to_sql` now supports the ``multi`` method (:issue:`29921`) - `OptionError` is now exposed in `pandas.errors` (:issue:`27553`) - :func:`timedelta_range` will now infer a frequency when passed ``start``, ``stop``, and ``periods`` (:issue:`32377`) +- .. --------------------------------------------------------------------------- From e069a60a3d2e2a52233cb6ef4eb8e82a3345baab Mon Sep 17 00:00:00 2001 From: elmonsomiat Date: Sat, 7 Mar 2020 22:25:57 +0000 Subject: [PATCH 12/28] added orient lower back --- pandas/core/frame.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 0264cc83d9885..b1631578e9e86 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1399,6 +1399,7 @@ def to_dict(self, orient="dict", into=dict): ) # GH16122 into_c = com.standardize_mapping(into) + orient = orient.lower() if orient == "dict": return into_c((k, v.to_dict(into)) for k, v in self.items()) From 707546740401cc0c17978a66991f4009e167a08e Mon Sep 17 00:00:00 2001 From: elmonsomiat Date: Sun, 8 Mar 2020 20:59:37 +0000 Subject: [PATCH 13/28] added deprecation warning and rolled back to accepting short versions of orient --- pandas/core/frame.py | 23 ++++++++++++++++++++++ pandas/tests/frame/methods/test_to_dict.py | 14 +++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index b1631578e9e86..b60f8c8429220 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1400,6 +1400,29 @@ def to_dict(self, orient="dict", into=dict): # GH16122 into_c = com.standardize_mapping(into) orient = orient.lower() + # GH32515 + if orient not in {"dict", "list", "series", "split", "records", "index"}: + warnings.warn( + "Using short name for 'orient' is deprecated. Only the " + "options: ('dict', list, 'series', 'split', 'records', 'index') " + "will be used in a future version. Use one of the above " + "to silence this warning.", + DeprecationWarning, + stacklevel=2) + + if orient.startswith("d"): + orient = "dict" + elif orient.startswith("l"): + orient = "list" + elif orient.startswith("sp"): + orient = "split" + elif orient.startswith("s"): + orient = "series" + elif orient.startswith("r"): + orient = "records" + elif orient.startswith("i"): + orient = "index" + if orient == "dict": return into_c((k, v.to_dict(into)) for k, v in self.items()) diff --git a/pandas/tests/frame/methods/test_to_dict.py b/pandas/tests/frame/methods/test_to_dict.py index e206cb0813080..637c89d286c9f 100644 --- a/pandas/tests/frame/methods/test_to_dict.py +++ b/pandas/tests/frame/methods/test_to_dict.py @@ -64,16 +64,22 @@ def test_to_dict_index_not_unique_with_index_orient(self): with pytest.raises(ValueError, match=msg): df.to_dict(orient="index") - @pytest.mark.parametrize("orient", ["d", "l", "r", "sp", "s", "i", "xinvalid"]) + @pytest.mark.parametrize("orient", ["xinvalid"]) def test_to_dict_invalid_orient(self, orient): - # to_dict(orient='d') should fail, as should only take the listed options - # see GH#32515 - df = DataFrame({"A": [0, 1]}) msg = f"orient '{orient}' not understood" with pytest.raises(ValueError, match=msg): df.to_dict(orient=orient) + @pytest.mark.parametrize("orient", ["d", "l", "r", "sp", "s", "i"]) + def test_to_dict_short_orient(self, orient): + # to_dict(orient='d') or any other short option should raise DeprecationWarning + # see GH#32515 + df = DataFrame({"A": [0, 1]}) + with pytest.warns(DeprecationWarning): + df.to_dict(orient=orient) + + @pytest.mark.parametrize("mapping", [dict, defaultdict(list), OrderedDict]) def test_to_dict(self, mapping): # orient= should only take the listed options From fb7de337af625ba98a89577a7c09145512e5e65f Mon Sep 17 00:00:00 2001 From: elmonsomiat Date: Sun, 8 Mar 2020 21:00:28 +0000 Subject: [PATCH 14/28] updated black formatting --- pandas/core/frame.py | 5 +++-- pandas/tests/frame/methods/test_to_dict.py | 1 - 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index b60f8c8429220..3160d345e4e48 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1407,8 +1407,9 @@ def to_dict(self, orient="dict", into=dict): "options: ('dict', list, 'series', 'split', 'records', 'index') " "will be used in a future version. Use one of the above " "to silence this warning.", - DeprecationWarning, - stacklevel=2) + DeprecationWarning, + stacklevel=2, + ) if orient.startswith("d"): orient = "dict" diff --git a/pandas/tests/frame/methods/test_to_dict.py b/pandas/tests/frame/methods/test_to_dict.py index 637c89d286c9f..eac6c09aa2c5b 100644 --- a/pandas/tests/frame/methods/test_to_dict.py +++ b/pandas/tests/frame/methods/test_to_dict.py @@ -79,7 +79,6 @@ def test_to_dict_short_orient(self, orient): with pytest.warns(DeprecationWarning): df.to_dict(orient=orient) - @pytest.mark.parametrize("mapping", [dict, defaultdict(list), OrderedDict]) def test_to_dict(self, mapping): # orient= should only take the listed options From 4164a873809e2ce43ada66b02a5f800abe0b0851 Mon Sep 17 00:00:00 2001 From: elmonsomiat Date: Sun, 8 Mar 2020 21:10:20 +0000 Subject: [PATCH 15/28] removed full orient strings to return warning" --- pandas/core/frame.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 3160d345e4e48..1edbb8a9a206d 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1401,7 +1401,14 @@ def to_dict(self, orient="dict", into=dict): into_c = com.standardize_mapping(into) orient = orient.lower() # GH32515 - if orient not in {"dict", "list", "series", "split", "records", "index"}: + if orient.startswith(("d", "l", "s", "r", "i")) and orient not in { + "dict", + "list", + "series", + "split", + "records", + "index", + }: warnings.warn( "Using short name for 'orient' is deprecated. Only the " "options: ('dict', list, 'series', 'split', 'records', 'index') " From c598b322ed2467ca45c8f87a41d4f762ef142ed3 Mon Sep 17 00:00:00 2001 From: elmonsomiat Date: Sun, 8 Mar 2020 21:12:49 +0000 Subject: [PATCH 16/28] removed deprecation warning test --- pandas/tests/frame/methods/test_to_dict.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/pandas/tests/frame/methods/test_to_dict.py b/pandas/tests/frame/methods/test_to_dict.py index eac6c09aa2c5b..34fac98e8a421 100644 --- a/pandas/tests/frame/methods/test_to_dict.py +++ b/pandas/tests/frame/methods/test_to_dict.py @@ -71,14 +71,6 @@ def test_to_dict_invalid_orient(self, orient): with pytest.raises(ValueError, match=msg): df.to_dict(orient=orient) - @pytest.mark.parametrize("orient", ["d", "l", "r", "sp", "s", "i"]) - def test_to_dict_short_orient(self, orient): - # to_dict(orient='d') or any other short option should raise DeprecationWarning - # see GH#32515 - df = DataFrame({"A": [0, 1]}) - with pytest.warns(DeprecationWarning): - df.to_dict(orient=orient) - @pytest.mark.parametrize("mapping", [dict, defaultdict(list), OrderedDict]) def test_to_dict(self, mapping): # orient= should only take the listed options From 7b9e90f0290fe1933e26b4f92ebbdf1f0d22311b Mon Sep 17 00:00:00 2001 From: elmonsomiat Date: Sun, 8 Mar 2020 21:43:11 +0000 Subject: [PATCH 17/28] added whatsnew for deprecation --- doc/source/whatsnew/v1.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index ff9655ab7f177..71cdd0db886ef 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -172,7 +172,7 @@ Deprecations ~~~~~~~~~~~~ - Lookups on a :class:`Series` with a single-item list containing a slice (e.g. ``ser[[slice(0, 4)]]``) are deprecated, will raise in a future version. Either convert the list to tuple, or pass the slice directly instead (:issue:`31333`) - :meth:`DataFrame.mean` and :meth:`DataFrame.median` with ``numeric_only=None`` will include datetime64 and datetime64tz columns in a future version (:issue:`29941`) -- +- :meth:`DataFrame.to_dict` will not accept short names for ``orient`` in future versions (:issue:`32515`) - .. --------------------------------------------------------------------------- From 6e117ee4cd1c371c7bd01e2d9e7e71ca72617e5b Mon Sep 17 00:00:00 2001 From: elmonsomiat Date: Sun, 8 Mar 2020 21:53:19 +0000 Subject: [PATCH 18/28] added deprecation warning test --- pandas/tests/frame/methods/test_to_dict.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pandas/tests/frame/methods/test_to_dict.py b/pandas/tests/frame/methods/test_to_dict.py index 34fac98e8a421..e46afc1456d1e 100644 --- a/pandas/tests/frame/methods/test_to_dict.py +++ b/pandas/tests/frame/methods/test_to_dict.py @@ -71,6 +71,14 @@ def test_to_dict_invalid_orient(self, orient): with pytest.raises(ValueError, match=msg): df.to_dict(orient=orient) + @pytest.mark.parametrize("orient", ["d", "l", "r", "sp", "s", "i"]) + def test_to_dict_short_orient(self, orient): + # to_dict(orient='d') or any other short option should raise DeprecationWarning + # see GH#32515 + df = DataFrame({"A": [0, 1]}) + with pytest.deprecated_call(): + df.to_dict(orient=orient) + @pytest.mark.parametrize("mapping", [dict, defaultdict(list), OrderedDict]) def test_to_dict(self, mapping): # orient= should only take the listed options From 36f956123da9c1c290acee62d86750e4eace4928 Mon Sep 17 00:00:00 2001 From: elmonsomiat Date: Sun, 8 Mar 2020 22:33:54 +0000 Subject: [PATCH 19/28] added deprecation warning test --- doc/source/whatsnew/v1.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 721dd93dbc1f6..ab6d4818667f4 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -174,7 +174,7 @@ Deprecations - Lookups on a :class:`Series` with a single-item list containing a slice (e.g. ``ser[[slice(0, 4)]]``) are deprecated, will raise in a future version. Either convert the list to tuple, or pass the slice directly instead (:issue:`31333`) - :meth:`DataFrame.mean` and :meth:`DataFrame.median` with ``numeric_only=None`` will include datetime64 and datetime64tz columns in a future version (:issue:`29941`) - Setting values with ``.loc`` using a positional slice is deprecated and will raise in a future version. Use ``.loc`` with labels or ``.iloc`` with positions instead (:issue:`31840`) -- :meth:`DataFrame.to_dict` will not accept short names for ``orient`` in future versions (:issue:`32515`) +- :meth:`DataFrame.to_dict` will not accept short names for ``orient`` in future versions .. --------------------------------------------------------------------------- From a17765efeeb3af4ac86844ea5dcf26e24f9afdd0 Mon Sep 17 00:00:00 2001 From: elmonsomiat Date: Sun, 8 Mar 2020 22:34:50 +0000 Subject: [PATCH 20/28] removed whatsnew --- doc/source/whatsnew/v1.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index ab6d4818667f4..d644a995a4876 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -174,7 +174,7 @@ Deprecations - Lookups on a :class:`Series` with a single-item list containing a slice (e.g. ``ser[[slice(0, 4)]]``) are deprecated, will raise in a future version. Either convert the list to tuple, or pass the slice directly instead (:issue:`31333`) - :meth:`DataFrame.mean` and :meth:`DataFrame.median` with ``numeric_only=None`` will include datetime64 and datetime64tz columns in a future version (:issue:`29941`) - Setting values with ``.loc`` using a positional slice is deprecated and will raise in a future version. Use ``.loc`` with labels or ``.iloc`` with positions instead (:issue:`31840`) -- :meth:`DataFrame.to_dict` will not accept short names for ``orient`` in future versions +- .. --------------------------------------------------------------------------- From 96194038073e1222e154577e9618c211ed8fb280 Mon Sep 17 00:00:00 2001 From: elmonsomiat Date: Sun, 8 Mar 2020 22:46:39 +0000 Subject: [PATCH 21/28] added --again-- whatsnew --- doc/source/whatsnew/v1.1.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index d644a995a4876..60f050a04894c 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -174,6 +174,7 @@ Deprecations - Lookups on a :class:`Series` with a single-item list containing a slice (e.g. ``ser[[slice(0, 4)]]``) are deprecated, will raise in a future version. Either convert the list to tuple, or pass the slice directly instead (:issue:`31333`) - :meth:`DataFrame.mean` and :meth:`DataFrame.median` with ``numeric_only=None`` will include datetime64 and datetime64tz columns in a future version (:issue:`29941`) - Setting values with ``.loc`` using a positional slice is deprecated and will raise in a future version. Use ``.loc`` with labels or ``.iloc`` with positions instead (:issue:`31840`) +- :meth:`DataFrame.to_dict` will not accept short names for ``orient`` in future versions (:issue:`32515`) - .. --------------------------------------------------------------------------- From fa43f067af0760fc53fef2c937332f7b37ce1d75 Mon Sep 17 00:00:00 2001 From: elmonsomiat Date: Mon, 9 Mar 2020 09:23:23 +0000 Subject: [PATCH 22/28] added back whatsnew --- doc/source/whatsnew/v1.1.0.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 60f050a04894c..721dd93dbc1f6 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -175,7 +175,6 @@ Deprecations - :meth:`DataFrame.mean` and :meth:`DataFrame.median` with ``numeric_only=None`` will include datetime64 and datetime64tz columns in a future version (:issue:`29941`) - Setting values with ``.loc`` using a positional slice is deprecated and will raise in a future version. Use ``.loc`` with labels or ``.iloc`` with positions instead (:issue:`31840`) - :meth:`DataFrame.to_dict` will not accept short names for ``orient`` in future versions (:issue:`32515`) -- .. --------------------------------------------------------------------------- From d0bc7dc7c1ad8d76684e041465125979108cace3 Mon Sep 17 00:00:00 2001 From: elmonsomiat Date: Mon, 9 Mar 2020 09:23:39 +0000 Subject: [PATCH 23/28] changed deprecation warning for future warning --- pandas/core/frame.py | 3 +-- pandas/tests/frame/methods/test_to_dict.py | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 0b49da5a1bd60..12a26a4435b32 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1414,8 +1414,7 @@ def to_dict(self, orient="dict", into=dict): "options: ('dict', list, 'series', 'split', 'records', 'index') " "will be used in a future version. Use one of the above " "to silence this warning.", - DeprecationWarning, - stacklevel=2, + FutureWarning, ) if orient.startswith("d"): diff --git a/pandas/tests/frame/methods/test_to_dict.py b/pandas/tests/frame/methods/test_to_dict.py index e46afc1456d1e..dd31ad544c6fd 100644 --- a/pandas/tests/frame/methods/test_to_dict.py +++ b/pandas/tests/frame/methods/test_to_dict.py @@ -76,7 +76,7 @@ def test_to_dict_short_orient(self, orient): # to_dict(orient='d') or any other short option should raise DeprecationWarning # see GH#32515 df = DataFrame({"A": [0, 1]}) - with pytest.deprecated_call(): + with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): df.to_dict(orient=orient) @pytest.mark.parametrize("mapping", [dict, defaultdict(list), OrderedDict]) From 9e3dabaacb1d035ee76c0813007b1665f731a41e Mon Sep 17 00:00:00 2001 From: elmonsomiat Date: Mon, 9 Mar 2020 09:39:02 +0000 Subject: [PATCH 24/28] updated test name --- pandas/tests/frame/methods/test_to_dict.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/tests/frame/methods/test_to_dict.py b/pandas/tests/frame/methods/test_to_dict.py index dd31ad544c6fd..386d695b2255d 100644 --- a/pandas/tests/frame/methods/test_to_dict.py +++ b/pandas/tests/frame/methods/test_to_dict.py @@ -72,9 +72,8 @@ def test_to_dict_invalid_orient(self, orient): df.to_dict(orient=orient) @pytest.mark.parametrize("orient", ["d", "l", "r", "sp", "s", "i"]) - def test_to_dict_short_orient(self, orient): - # to_dict(orient='d') or any other short option should raise DeprecationWarning - # see GH#32515 + def test_to_dict_short_orient_warns(self, orient): + # GH#32515 df = DataFrame({"A": [0, 1]}) with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): df.to_dict(orient=orient) From d1b9c505c2b85b8e50ca7c112fe54e724ac434a6 Mon Sep 17 00:00:00 2001 From: elmonsomiat Date: Mon, 9 Mar 2020 12:28:05 +0000 Subject: [PATCH 25/28] rolled back to original ValueError test for .to_dict --- pandas/tests/frame/methods/test_to_dict.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pandas/tests/frame/methods/test_to_dict.py b/pandas/tests/frame/methods/test_to_dict.py index 386d695b2255d..f1656b46cf356 100644 --- a/pandas/tests/frame/methods/test_to_dict.py +++ b/pandas/tests/frame/methods/test_to_dict.py @@ -64,12 +64,11 @@ def test_to_dict_index_not_unique_with_index_orient(self): with pytest.raises(ValueError, match=msg): df.to_dict(orient="index") - @pytest.mark.parametrize("orient", ["xinvalid"]) - def test_to_dict_invalid_orient(self, orient): + def test_to_dict_invalid_orient(self): df = DataFrame({"A": [0, 1]}) - msg = f"orient '{orient}' not understood" + msg = "orient 'xinvalid' not understood" with pytest.raises(ValueError, match=msg): - df.to_dict(orient=orient) + df.to_dict(orient="xinvalid") @pytest.mark.parametrize("orient", ["d", "l", "r", "sp", "s", "i"]) def test_to_dict_short_orient_warns(self, orient): From 95573494ab183c2ba45b1887911ac28d9361999f Mon Sep 17 00:00:00 2001 From: elmonsomiat Date: Wed, 11 Mar 2020 14:27:45 +0000 Subject: [PATCH 26/28] updated whatsnew --- doc/source/whatsnew/v1.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 721dd93dbc1f6..0e13672e66446 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -174,7 +174,7 @@ Deprecations - Lookups on a :class:`Series` with a single-item list containing a slice (e.g. ``ser[[slice(0, 4)]]``) are deprecated, will raise in a future version. Either convert the list to tuple, or pass the slice directly instead (:issue:`31333`) - :meth:`DataFrame.mean` and :meth:`DataFrame.median` with ``numeric_only=None`` will include datetime64 and datetime64tz columns in a future version (:issue:`29941`) - Setting values with ``.loc`` using a positional slice is deprecated and will raise in a future version. Use ``.loc`` with labels or ``.iloc`` with positions instead (:issue:`31840`) -- :meth:`DataFrame.to_dict` will not accept short names for ``orient`` in future versions (:issue:`32515`) +- :meth:`DataFrame.to_dict` has deprecated accepting short names for ``orient`` in future versions (:issue:`32515`) .. --------------------------------------------------------------------------- From dcc9d43c79a5b9bb95db4488293a153454d161db Mon Sep 17 00:00:00 2001 From: elmonsomiat Date: Sat, 14 Mar 2020 17:06:10 +0000 Subject: [PATCH 27/28] moved mapping of strings inside if statements --- pandas/core/frame.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 12a26a4435b32..69a5a8fbc1829 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1399,6 +1399,8 @@ def to_dict(self, orient="dict", into=dict): ) # GH16122 into_c = com.standardize_mapping(into) + + contains_upper = any(x.isupper() for x in orient) orient = orient.lower() # GH32515 if orient.startswith(("d", "l", "s", "r", "i")) and orient not in { @@ -1417,18 +1419,18 @@ def to_dict(self, orient="dict", into=dict): FutureWarning, ) - if orient.startswith("d"): - orient = "dict" - elif orient.startswith("l"): - orient = "list" - elif orient.startswith("sp"): - orient = "split" - elif orient.startswith("s"): - orient = "series" - elif orient.startswith("r"): - orient = "records" - elif orient.startswith("i"): - orient = "index" + if orient.startswith("d"): + orient = "dict" + elif orient.startswith("l"): + orient = "list" + elif orient.startswith("sp"): + orient = "split" + elif orient.startswith("s"): + orient = "series" + elif orient.startswith("r"): + orient = "records" + elif orient.startswith("i"): + orient = "index" if orient == "dict": return into_c((k, v.to_dict(into)) for k, v in self.items()) From 4173ce6f5fa60ea9e4b435c697ab5a7805ff1908 Mon Sep 17 00:00:00 2001 From: elmonsomiat Date: Sat, 14 Mar 2020 17:53:42 +0000 Subject: [PATCH 28/28] remvoed unused contains upper --- pandas/core/frame.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index e3c0fc63cdc12..204c916c88fa0 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -1402,7 +1402,6 @@ def to_dict(self, orient="dict", into=dict): # GH16122 into_c = com.standardize_mapping(into) - contains_upper = any(x.isupper() for x in orient) orient = orient.lower() # GH32515 if orient.startswith(("d", "l", "s", "r", "i")) and orient not in {