Skip to content

BUG: DataFrame.astype(dict) causes mypy error if dict includes 'datetime64[ns]' among mixed types #59411

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

Closed
2 of 3 tasks
bjfar opened this issue Aug 5, 2024 · 3 comments
Closed
2 of 3 tasks
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@bjfar
Copy link

bjfar commented Aug 5, 2024

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
df = pd.DataFrame({'ID':[], 'col_2':[]})
dtypes = dict(ID=int, col_2='datetime64[ns]')
df2 = df.astype(dtypes)
print(df2.dtypes)

Issue Description

Output is:

ID                int64
col_1    datetime64[ns]
dtype: object

Seems fine. But running mypy we get:

error: Argument 1 to "astype" of "DataFrame" has incompatible type "dict[str, object]"; expected "Literal['bool', 'boolean', '?', 'b1', 'bool8', 'bool_', 'bool[pyarrow]', 'boolean[pyarrow]'] | type[builtins.bool] | BooleanDtype | type[numpy.bool] | Literal['int', 'Int8', 'Int16', 'Int32', 'Int64', 'b', 'i1', 'int8', 'byte', 'h', 'i2', 'int16', 'short', 'i', 'i4', 'int32', 'intc', 'l', 'i8', 'int64', 'int_', 'long', 'q', 'longlong', 'p', 'intp', 'int0', 'int8[pyarrow]', 'int16[pyarrow]', 'int32[pyarrow]', 'int64[pyarrow]'] | type[int] | Int8Dtype | Int16Dtype | Int32Dtype | Int64Dtype | <6 more items> | Literal['UInt8', 'UInt16', 'UInt32', 'UInt64', 'B', 'u1', 'uint8', 'ubyte', 'H', 'u2', 'uint16', 'ushort', 'I', 'u4', 'uint32', 'uintc', 'L', 'u8', 'uint', 'ulong', 'uint64', 'Q', 'ulonglong', 'P', 'uintp', 'uint0', 'uint8[pyarrow]', 'uint16[pyarrow]', 'uint32[pyarrow]', 'uint64[pyarrow]'] | UInt8Dtype | UInt16Dtype | UInt32Dtype | UInt64Dtype | type[unsignedinteger[Any]] | type[unsignedinteger[Any]] | type[unsignedinteger[Any]] | type[unsignedinteger[Any]] | type[unsignedinteger[Any]] | type[unsignedinteger[Any]] | Literal['str', 'string', 'U', 'str_', 'str0', 'unicode', 'unicode_', 'string[pyarrow]'] | type[str] | StringDtype | type[str_] | Literal['bytes', 'S', 'a', 'bytes_', 'bytes0', 'string_', 'binary[pyarrow]'] | type[bytes] | type[bytes_] | <8 more items> | Mapping[Any, ExtensionDtype | str | dtype[generic] | type[str] | type[complex] | type[builtins.bool] | type[object]] | Series[Any]"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

Note that if both types are datetime64[ns] then mypy is fine with it.

Expected Behavior

I don't see anything in the docs for astype that would forbid this, and indeed the code seems to work fine. So I would expect mypy not to report an error for this.

Installed Versions

pd.show_versions()

INSTALLED VERSIONS

commit : d9cdd2e
python : 3.12.4.final.0
python-bits : 64
OS : Linux
OS-release : 6.5.0-44-generic
Version : #44~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue Jun 18 14:36:16 UTC 2
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_AU.UTF-8
LOCALE : en_AU.UTF-8

pandas : 2.2.2
numpy : 2.0.1
pytz : 2024.1
dateutil : 2.9.0
setuptools : 72.1.0
pip : 24.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

@bjfar bjfar added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 5, 2024
@Patsnoop
Copy link

Patsnoop commented Aug 5, 2024

I would like to work on this

@loicdiridollou
Copy link
Contributor

I am not sure this is really a pandas issue but more a pandas-stubs issue.
The error raised by mypy is that the type you are passing is of type <class 'type'> which is not explicitly allowed in the pandas-stubs for the astype method, see https://github.com/pandas-dev/pandas-stubs/blob/8a41508683212d6e861f07d053d86df796460124/pandas-stubs/core/frame.pyi#L1631.
I believe you should be better reporting it on the pandas-stubs https://github.com/pandas-dev/pandas-stubs/issues.

@rhshadrach
Copy link
Member

Agreed @loicdiridollou. @bjfar - can you open an issue there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

4 participants