Skip to content

pandas.DataFrame.from_dict : issue with dtype and columns arguments types #896

Closed
@mutricyl

Description

@mutricyl

Describe the bug
issue with types for pandas.DataFrame.from_dict method:

  • dtype is restricted to str in the stub file when pands allows for str, type and None
  • columns is restricted to list of strings when pandas allows for much more (at least: str, int, float, datetime)

To Reproduce

  1. minimal exemple:
import pandas as pd
import datetime

data = {'l1': [1, 2, 3],
        'l2': [4, 5, 6]}

# all of the following are accepted by pandas
# however mypy complains with the last 3
pd.DataFrame.from_dict(data, orient='index')  # OK
pd.DataFrame.from_dict(data, orient='index', dtype='float')  # OK
pd.DataFrame.from_dict(data, orient='index', columns=['a', 'b', 'c'])  # OK
pd.DataFrame.from_dict(data, orient='index', dtype=float)  # NOK
pd.DataFrame.from_dict(data, orient='index', dtype=None)  # NOK
pd.DataFrame.from_dict(data, orient='index',
                       columns=[1.0, 2, datetime.datetime.now()])  # NOK
  1. type checker : mypy
  2. typing errors:
from_dict_test.py:12: error: Argument "dtype" to "from_dict" of "DataFrame" has incompatible type "type[float]"; expected "str"  [arg-type]
from_dict_test.py:13: error: Argument "dtype" to "from_dict" of "DataFrame" has incompatible type "None"; expected "str"  [arg-type]
from_dict_test.py:15: error: List item 0 has incompatible type "float"; expected "str"  [list-item]
from_dict_test.py:15: error: List item 1 has incompatible type "int"; expected "str"  [list-item]
from_dict_test.py:15: error: List item 2 has incompatible type "datetime"; expected "str"  [list-item]

Please complete the following information:

  • OS: Windows
  • Windows 10 22H2 build: 19045.4170
  • Python 3.12.2 | packaged by Anaconda, Inc. | (main, Feb 27 2024, 17:28:07) [MSC v.1916 64 bit (AMD64)] on win32
  • mypy 1.9.0
  • pandas-stubs 2.2.0.240218

Metadata

Metadata

Assignees

No one assigned

    Labels

    ConstructorsSeries/DataFrame/Index/pd.array ConstructorsDataFrameDataFrame data structure

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions