Closed
Description
Describe the bug
issue with types for pandas.DataFrame.from_dict
method:
dtype
is restricted to str in the stub file when pands allows forstr
,type
andNone
columns
is restricted to list of strings when pandas allows for much more (at least: str, int, float, datetime)
To Reproduce
- 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
- type checker :
mypy
- 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