Skip to content

DataFrame.to_dict(orient="index") should return a nested dictionary #799

Open
@gandhis1

Description

@gandhis1

Describe the bug
When you do DataFrame.to_dict(orient="index"), and you are using default integer index and string columns, you will get a Dict[int, Dict[str, Any]] at runtime. The current annotations do not reflect this nested dictionary, they instead return a Dict[Hashable, Any]. I understand that the exact type this returns has changed slightly after recent commits by @twoertwein, but the issue still stands.

Expectation: returns MutableMapping[Hashable, MutableMapping[Hashable, Any]]

This issue could be broadened to more precisely annotate all possible values of orient if desired. For instance, orient="split" probably should return a TypedDict that has index and columns as keys.

To Reproduce
Any usage of to_dict(orient="index") should reproduce this. Below is a screenshot of an example:

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions