Skip to content

DEPR: make arguments keyword only in to_dict #54630

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

Merged
merged 15 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
added test and deprecation
  • Loading branch information
rsm-23 committed Aug 21, 2023
commit 0940b3a204e17a47ff1c1d80b69d37ae502938ce
9 changes: 4 additions & 5 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1925,23 +1925,22 @@ def _create_data_for_split_and_tight_to_dict(
def to_dict(
self,
orient: Literal["dict", "list", "series", "split", "tight", "index"] = ...,
*,
into: type[dict] = ...,
) -> dict:
...

@overload
def to_dict(
self, orient: Literal["records"], *, into: type[dict] = ...
) -> list[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[
"dict", "list", "series", "split", "tight", "records", "index"
] = "dict",
*,
into: type[dict] = dict,
index: bool = True,
) -> dict | list[dict]:
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/frame/methods/test_to_dict.py
Original file line number Diff line number Diff line change
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", {})