Skip to content

Commit b4b96fb

Browse files
authored
DEPR: make arguments keyword only in to_dict (#54630)
* make arguments keyword only * added argument keywords in tests * modified overloaded methods * fixed docs * added whatsnew * made orient as positional * added test and deprecation
1 parent cbe88f3 commit b4b96fb

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

doc/source/whatsnew/v2.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ Deprecations
9494
~~~~~~~~~~~~
9595
- Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_clipboard`. (:issue:`54229`)
9696
- Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_csv` except ``path_or_buf``. (:issue:`54229`)
97+
- Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_dict`. (:issue:`54229`)
9798
- Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_hdf` except ``path_or_buf``. (:issue:`54229`)
9899
- Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_html` except ``buf``. (:issue:`54229`)
99100
- Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_json` except ``path_or_buf``. (:issue:`54229`)

pandas/core/frame.py

+3
Original file line numberDiff line numberDiff line change
@@ -1933,6 +1933,9 @@ def to_dict(
19331933
def to_dict(self, orient: Literal["records"], into: type[dict] = ...) -> list[dict]:
19341934
...
19351935

1936+
@deprecate_nonkeyword_arguments(
1937+
version="3.0", allowed_args=["self", "orient"], name="to_dict"
1938+
)
19361939
def to_dict(
19371940
self,
19381941
orient: Literal[

pandas/tests/frame/methods/test_to_dict.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -99,27 +99,27 @@ def test_to_dict(self, mapping):
9999
for k2, v2 in v.items():
100100
assert v2 == recons_data[k][k2]
101101

102-
recons_data = DataFrame(test_data).to_dict("list", mapping)
102+
recons_data = DataFrame(test_data).to_dict("list", into=mapping)
103103

104104
for k, v in test_data.items():
105105
for k2, v2 in v.items():
106106
assert v2 == recons_data[k][int(k2) - 1]
107107

108-
recons_data = DataFrame(test_data).to_dict("series", mapping)
108+
recons_data = DataFrame(test_data).to_dict("series", into=mapping)
109109

110110
for k, v in test_data.items():
111111
for k2, v2 in v.items():
112112
assert v2 == recons_data[k][k2]
113113

114-
recons_data = DataFrame(test_data).to_dict("split", mapping)
114+
recons_data = DataFrame(test_data).to_dict("split", into=mapping)
115115
expected_split = {
116116
"columns": ["A", "B"],
117117
"index": ["1", "2", "3"],
118118
"data": [[1.0, "1"], [2.0, "2"], [np.nan, "3"]],
119119
}
120120
tm.assert_dict_equal(recons_data, expected_split)
121121

122-
recons_data = DataFrame(test_data).to_dict("records", mapping)
122+
recons_data = DataFrame(test_data).to_dict("records", into=mapping)
123123
expected_records = [
124124
{"A": 1.0, "B": "1"},
125125
{"A": 2.0, "B": "2"},
@@ -494,3 +494,13 @@ def test_to_dict_masked_native_python(self):
494494
df = DataFrame({"a": Series([1, NA], dtype="Int64"), "B": 1})
495495
result = df.to_dict(orient="records")
496496
assert isinstance(result[0]["a"], int)
497+
498+
def test_to_dict_pos_args_deprecation(self):
499+
# GH-54229
500+
df = DataFrame({"a": [1, 2, 3]})
501+
msg = (
502+
r"Starting with pandas version 3.0 all arguments of to_dict except for the "
503+
r"argument 'orient' will be keyword-only."
504+
)
505+
with tm.assert_produces_warning(FutureWarning, match=msg):
506+
df.to_dict("records", {})

0 commit comments

Comments
 (0)