-
-
Notifications
You must be signed in to change notification settings - Fork 146
fix(DataFrame): #799 to_dict #1283
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
from __future__ import annotations | ||
|
||
from collections import defaultdict | ||
from collections import ( | ||
OrderedDict, | ||
defaultdict, | ||
) | ||
from collections.abc import ( | ||
Callable, | ||
Hashable, | ||
Iterable, | ||
Iterator, | ||
|
@@ -20,7 +24,6 @@ | |
from typing import ( | ||
TYPE_CHECKING, | ||
Any, | ||
Callable, | ||
Generic, | ||
TypedDict, | ||
TypeVar, | ||
|
@@ -39,6 +42,7 @@ | |
) | ||
import pytest | ||
from typing_extensions import ( | ||
Never, | ||
TypeAlias, | ||
assert_never, | ||
assert_type, | ||
|
@@ -3638,33 +3642,86 @@ def test_to_records() -> None: | |
) | ||
|
||
|
||
def test_to_dict() -> None: | ||
def test_to_dict_simple() -> None: | ||
check(assert_type(DF.to_dict(), dict[Hashable, Any]), dict) | ||
check(assert_type(DF.to_dict("split"), dict[Hashable, Any]), dict) | ||
check(assert_type(DF.to_dict("records"), list[dict[Hashable, Any]]), list) | ||
check(assert_type(DF.to_dict("index"), dict[Hashable, dict[Hashable, Any]]), dict) | ||
check(assert_type(DF.to_dict("dict"), dict[Hashable, Any]), dict) | ||
check(assert_type(DF.to_dict("list"), dict[Hashable, Any]), dict) | ||
check(assert_type(DF.to_dict("series"), dict[Hashable, Any]), dict) | ||
check(assert_type(DF.to_dict("split"), dict[str, list]), dict, str) | ||
check(assert_type(DF.to_dict("tight"), dict[str, list]), dict, str) | ||
|
||
if TYPE_CHECKING_INVALID_USAGE: | ||
|
||
def test(mapping: Mapping) -> None: # pyright: ignore[reportUnusedFunction] | ||
DF.to_dict( # type: ignore[call-overload] | ||
into=mapping # pyright: ignore[reportArgumentType,reportCallIssue] | ||
) | ||
|
||
assert_type(DF.to_dict(into=defaultdict), Never) | ||
assert_type(DF.to_dict("records", into=defaultdict), Never) | ||
assert_type(DF.to_dict("index", into=defaultdict), Never) | ||
assert_type(DF.to_dict("dict", into=defaultdict), Never) | ||
assert_type(DF.to_dict("list", into=defaultdict), Never) | ||
assert_type(DF.to_dict("series", into=defaultdict), Never) | ||
assert_type(DF.to_dict("split", into=defaultdict), Never) | ||
assert_type(DF.to_dict("tight", into=defaultdict), Never) | ||
|
||
|
||
def test_to_dict_into_defaultdict() -> None: | ||
"""Test DataFrame.to_dict with `into` is an instance of defaultdict[Any, list]""" | ||
|
||
data = pd.DataFrame({("str", "rts"): [[1, 2, 4], [2, 3], [3]]}) | ||
target: defaultdict[Any, list] = defaultdict(list) | ||
|
||
target: MutableMapping = defaultdict(list) | ||
check( | ||
assert_type(DF.to_dict(into=target), MutableMapping[Hashable, Any]), defaultdict | ||
assert_type(data.to_dict(into=target), defaultdict[Any, list]), | ||
defaultdict, | ||
tuple, | ||
) | ||
target = defaultdict(list) | ||
check( | ||
assert_type(DF.to_dict("tight", into=target), MutableMapping[Hashable, Any]), | ||
assert_type( # type: ignore[assert-type] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't want to see There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The overload def to_dict(
self,
orient: Literal["index"],
*,
into: defaultdict,
index: Literal[True] = ...,
) -> defaultdict[Hashable, dict[Hashable, Any]]: ... should give the correct typing. However, with the following testing script import pandas as pd
from collections import defaultdict
from typing import Any, reveal_type
data = pd.DataFrame({("str", "rts"): [[1, 2, 4], [2, 3], [3]]})
target: defaultdict[Any, list] = defaultdict(list)
reveal_type(data.to_dict("index", into=target))
I think it is a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, was able to recreate at python/mypy#19525 So in this case, can you put a comment in the test with a link to that issue to indicate why the |
||
data.to_dict("index", into=target), | ||
defaultdict[Hashable, dict[Hashable, Any]], | ||
), | ||
defaultdict, | ||
) | ||
target = defaultdict(list) | ||
check(assert_type(DF.to_dict("records"), list[dict[Hashable, Any]]), list) | ||
check( | ||
assert_type(data.to_dict("tight", into=target), MutableMapping[str, list]), | ||
defaultdict, | ||
str, | ||
) | ||
check( | ||
assert_type(data.to_dict("records", into=target), list[defaultdict[Any, list]]), | ||
list, | ||
defaultdict, | ||
) | ||
|
||
|
||
def test_to_dict_into_ordered_dict() -> None: | ||
"""Test DataFrame.to_dict with `into=OrderedDict`""" | ||
|
||
data = pd.DataFrame({("str", "rts"): [[1, 2, 4], [2, 3], [3]]}) | ||
|
||
check(assert_type(data.to_dict(into=OrderedDict), OrderedDict), OrderedDict, tuple) | ||
check( | ||
assert_type( | ||
DF.to_dict("records", into=target), list[MutableMapping[Hashable, Any]] | ||
data.to_dict("index", into=OrderedDict), | ||
OrderedDict[Hashable, dict[Hashable, Any]], | ||
), | ||
OrderedDict, | ||
) | ||
check( | ||
assert_type(data.to_dict("tight", into=OrderedDict), MutableMapping[str, list]), | ||
OrderedDict, | ||
str, | ||
) | ||
check( | ||
assert_type(data.to_dict("records", into=OrderedDict), list[OrderedDict]), | ||
list, | ||
OrderedDict, | ||
) | ||
if TYPE_CHECKING_INVALID_USAGE: | ||
|
||
def test(mapping: Mapping) -> None: # pyright: ignore[reportUnusedFunction] | ||
DF.to_dict( # type: ignore[call-overload] | ||
into=mapping # pyright: ignore[reportArgumentType,reportCallIssue] | ||
) | ||
|
||
|
||
def test_neg() -> None: | ||
|
@@ -4111,19 +4168,22 @@ def test_to_dict_index() -> None: | |
assert_type(df.to_dict(orient="series", index=True), dict[Hashable, Any]), dict | ||
) | ||
check( | ||
assert_type(df.to_dict(orient="index", index=True), dict[Hashable, Any]), dict | ||
assert_type( | ||
df.to_dict(orient="index", index=True), dict[Hashable, dict[Hashable, Any]] | ||
), | ||
dict, | ||
) | ||
check( | ||
assert_type(df.to_dict(orient="split", index=True), dict[Hashable, Any]), dict | ||
assert_type(df.to_dict(orient="split", index=True), dict[str, list]), dict, str | ||
) | ||
check( | ||
assert_type(df.to_dict(orient="tight", index=True), dict[Hashable, Any]), dict | ||
assert_type(df.to_dict(orient="tight", index=True), dict[str, list]), dict, str | ||
) | ||
check( | ||
assert_type(df.to_dict(orient="tight", index=False), dict[Hashable, Any]), dict | ||
assert_type(df.to_dict(orient="tight", index=False), dict[str, list]), dict, str | ||
) | ||
check( | ||
assert_type(df.to_dict(orient="split", index=False), dict[Hashable, Any]), dict | ||
assert_type(df.to_dict(orient="split", index=False), dict[str, list]), dict, str | ||
) | ||
if TYPE_CHECKING_INVALID_USAGE: | ||
check(assert_type(df.to_dict(orient="records", index=False), list[dict[Hashable, Any]]), list) # type: ignore[assert-type, call-overload] # pyright: ignore[reportArgumentType,reportAssertTypeFailure,reportCallIssue] | ||
|
Uh oh!
There was an error while loading. Please reload this page.