Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEPR: make arguments keyword only in to_dict #54630

Merged
merged 15 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Other API changes
Deprecations
~~~~~~~~~~~~
- Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_csv` except ``path_or_buf``. (:issue:`54229`)
- Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_dict`. (:issue:`54229`)
- Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_hdf` except ``path_or_buf``. (:issue:`54229`)
- Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_html` except ``buf``. (:issue:`54229`)
- Deprecated allowing non-keyword arguments in :meth:`DataFrame.to_json` except ``path_or_buf``. (:issue:`54229`)
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1933,6 +1933,9 @@ def to_dict(
def to_dict(self, orient: Literal["records"], into: type[dict] = ...) -> list[dict]:
...

@deprecate_nonkeyword_arguments(
version="3.0", allowed_args=["self", "orient"], name="to_dict"
)
def to_dict(
self,
orient: Literal[
Expand Down
18 changes: 14 additions & 4 deletions pandas/tests/frame/methods/test_to_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,27 +99,27 @@ 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("list", mapping)
recons_data = DataFrame(test_data).to_dict("list", into=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("series", mapping)
recons_data = DataFrame(test_data).to_dict("series", into=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("split", mapping)
recons_data = DataFrame(test_data).to_dict("split", into=mapping)
expected_split = {
"columns": ["A", "B"],
"index": ["1", "2", "3"],
"data": [[1.0, "1"], [2.0, "2"], [np.nan, "3"]],
}
tm.assert_dict_equal(recons_data, expected_split)

recons_data = DataFrame(test_data).to_dict("records", mapping)
recons_data = DataFrame(test_data).to_dict("records", into=mapping)
expected_records = [
{"A": 1.0, "B": "1"},
{"A": 2.0, "B": "2"},
Expand Down Expand Up @@ -494,3 +494,13 @@ def test_to_dict_masked_native_python(self):
df = DataFrame({"a": Series([1, NA], dtype="Int64"), "B": 1})
result = df.to_dict(orient="records")
assert isinstance(result[0]["a"], int)

def test_to_dict_pos_args_deprecation(self):
# GH-54229
df = DataFrame({"a": [1, 2, 3]})
msg = (
r"Starting with pandas version 3.0 all arguments of to_dict except for the "
r"argument 'orient' will be keyword-only."
)
with tm.assert_produces_warning(FutureWarning, match=msg):
df.to_dict("records", {})