-
-
Notifications
You must be signed in to change notification settings - Fork 145
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?
Conversation
94f0d80
to
d041ff2
Compare
1b5bb8c
to
7da086e
Compare
pandas-stubs/core/frame.pyi
Outdated
*, | ||
into: _T_MUTABLE_MAPPING | type[_T_MUTABLE_MAPPING], | ||
index: Literal[True] = ..., | ||
) -> MutableMapping[Hashable, _T_MUTABLE_MAPPING]: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do now know how to get rid of the MutableMapping
here...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is into
used for the outer mapping, the inner mapping, or both at runtime?
I assume we would need somthing like _T_MUTABLE_MAPPING[Hashable, dict[Hashable, Any]]
, if TypeVar would support [] (it doesn't) and if the inner mapping is always a dict (I don't know).
I'm not sure if we can annotate both the correct sub-class and also the keys/values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
into
used for the outer mapping, the inner mapping, or both at runtime?
Good point!
pd.DataFrame({("a", "b"): [[1], [2, 3]]}).to_dict("index", into=defaultdict(list))
the result is actually
defaultdict(<class 'list'>, {0: {('a', 'b'): [1]}, 1: {('a', 'b'): [2, 3]}})
Only the outer mapping is the type specified by into
. The inner mapping are dict
s.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With afb5729, now if into
is defaultdict
or OrderedDict
, the outer mapping is of the corresponding type, and the inner mapping is correctly dict
.
If into
is a more generic MutableMapping
, however, I do not see a way to make the outer mapping covariant and fix the inner mapping to dict
at the same time. I choose to make the outer mapping just the generic MutableMapping
, and the inner mapping dict
.
8bf75d2
to
4f1fb49
Compare
4f1fb49
to
afb5729
Compare
@overload | ||
def to_dict( | ||
self, | ||
orient: Literal["dict", "list", "series", "index"], | ||
orient: Literal["index"], | ||
*, | ||
into: MutableMapping | type[MutableMapping], | ||
into: defaultdict, | ||
index: Literal[True] = ..., | ||
) -> MutableMapping[Hashable, Any]: ... | ||
) -> defaultdict[Hashable, dict[Hashable, Any]]: ... | ||
@overload | ||
def to_dict( | ||
self, | ||
orient: Literal["split", "tight"], | ||
orient: Literal["index"], | ||
*, | ||
into: MutableMapping | type[MutableMapping], | ||
index: bool = ..., | ||
) -> MutableMapping[Hashable, Any]: ... | ||
into: OrderedDict | type[OrderedDict], | ||
index: Literal[True] = ..., | ||
) -> OrderedDict[Hashable, dict[Hashable, Any]]: ... | ||
@overload | ||
def to_dict( | ||
self, | ||
orient: Literal["dict", "list", "series", "index"] = ..., | ||
orient: Literal["index"], | ||
*, | ||
into: MutableMapping | type[MutableMapping], | ||
index: Literal[True] = ..., | ||
) -> MutableMapping[Hashable, Any]: ... | ||
) -> MutableMapping[Hashable, dict[Hashable, Any]]: ... | ||
@overload | ||
def to_dict( | ||
self, | ||
orient: Literal["index"], | ||
*, | ||
into: type[dict] = ..., | ||
index: Literal[True] = ..., | ||
) -> dict[Hashable, dict[Hashable, Any]]: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Hard-coded
defaultdict
,OrderedDict
anddict
- Otherwise,
to_dict
now givesMutableMapping
def to_dict( | ||
self, | ||
orient: str = ..., | ||
*, | ||
into: type[defaultdict], | ||
index: Literal[True] = ..., | ||
) -> Never: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@overload | ||
def to_dict( | ||
self, | ||
orient: Literal["records"], | ||
*, | ||
into: MutableMapping | type[MutableMapping], | ||
into: _T_MUTABLE_MAPPING | type[_T_MUTABLE_MAPPING], | ||
index: Literal[True] = ..., | ||
) -> list[MutableMapping[Hashable, Any]]: ... | ||
) -> list[_T_MUTABLE_MAPPING]: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made covariant
@overload | ||
def to_dict( | ||
self, | ||
orient: Literal["split", "tight"] = ..., | ||
orient: Literal["dict", "list", "series"] = ..., | ||
*, | ||
into: MutableMapping | type[MutableMapping], | ||
index: bool = ..., | ||
) -> MutableMapping[Hashable, Any]: ... | ||
into: _T_MUTABLE_MAPPING | type[_T_MUTABLE_MAPPING], | ||
index: Literal[True] = ..., | ||
) -> _T_MUTABLE_MAPPING: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Covariant
@overload | ||
def to_dict( | ||
self, | ||
orient: Literal["split", "tight"] = ..., | ||
orient: Literal["split", "tight"], | ||
*, | ||
into: MutableMapping | type[MutableMapping], | ||
index: bool = ..., | ||
) -> MutableMapping[str, list]: ... | ||
@overload | ||
def to_dict( | ||
self, | ||
orient: Literal["split", "tight"], | ||
*, | ||
into: type[dict] = ..., | ||
index: bool = ..., | ||
) -> dict[Hashable, Any]: ... | ||
) -> dict[str, list]: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The old typing was wrong, now fixed.
assert_type()
to assert the type of any return value