Skip to content
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

Series.astype(object) not supported anymore #565

Closed
tcrasset opened this issue Mar 8, 2023 · 3 comments · Fixed by #566
Closed

Series.astype(object) not supported anymore #565

tcrasset opened this issue Mar 8, 2023 · 3 comments · Fixed by #566

Comments

@tcrasset
Copy link

tcrasset commented Mar 8, 2023

Describe the bug

Series.astype(object) runs fine, and used to typecheck properly before, but since updating mypy and/or pandas-stubs, it doesn't anymore. Didn't take the time to find a working version that did not throw errors.

To Reproduce

import pandas as pd

s = pd.Series([1, 1])
s = s.astype(object)

Getting the following error message:

error: Argument 1 to "astype" of "Series" has incompatible type "Type[object]"; expected "Union[Type[bool], BooleanDtype, Type[bool_], Literal['bool', 'boolean']]"  [arg-type]
    s = s.astype(object)

Please complete the following information:

  • OS: Ubuntu 20.04.5, uname -a yields: Linux REDACTED 5.14.0-1058-oem #66-Ubuntu SMP Fri Feb 10 09:46:18 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

  • python version = 3.9.5

  • version of type checker: mypy=1.0.1

  • version of installed pandas-stubs: pandas_stubs=1.5.3.230304

@tcrasset
Copy link
Author

tcrasset commented Mar 8, 2023

Seems to be causing the issue with DataFrame too:

import pandas as pd

df = pd.DataFrame([1, 1])
df = df.astype(object)

yields

error: Argument 1 to "astype" of "DataFrame" has incompatible type "Type[object]"; expected "Union[Union[Union[Type[bool], BooleanDtype, Type[bool_], Literal['bool', 'boolean']], Union[Type[int], Int8Dtype, Int16Dtype, Int32Dtype, Int64Dtype, Type[signedinteger[Any]], Type[signedinteger[_8Bit]], Type[signedinteger[_16Bit]], Type[signedinteger[_32Bit]], Type[signedinteger[_64Bit]], Type[signedinteger[Any]], Type[unsignedinteger[Any]], Type[unsignedinteger[_8Bit]], Type[unsignedinteger[_16Bit]], Type[unsignedinteger[_32Bit]], Type[unsignedinteger[_64Bit]], Type[unsignedinteger[Any]], Literal['int', 'Int8', 'Int16', 'Int32', 'Int64', 'byte', 'int8', 'int16', 'int32', 'int64', 'intp', 'ubyte', 'uint8', 'uint16', 'uint32', 'uint64', 'uintp']], Union[Type[str], StringDtype, Literal['str', 'string']], Type[bytes], Union[Type[float], Float32Dtype, Float64Dtype, Type[floating[_16Bit]], Type[floating[_32Bit]], Type[floating[_64Bit]], Literal['float', 'Float32', 'Float64', 'float16', 'float32', 'float64']], Union[Type[complex], Type[complexfloating[_32Bit, _32Bit]], Type[complexfloating[_64Bit, _64Bit]], Literal['complex', 'complex64', 'complex128']], Literal['timedelta64[Y]', 'timedelta64[M]', 'timedelta64[W]', 'timedelta64[D]', 'timedelta64[h]', 'timedelta64[m]', 'timedelta64[s]', 'timedelta64[ms]', 'timedelta64[us]', 'timedelta64[μs]', 'timedelta64[ns]', 'timedelta64[ps]', 'timedelta64[fs]', 'timedelta64[as]'], Literal['datetime64[Y]', 'datetime64[M]', 'datetime64[W]', 'datetime64[D]', 'datetime64[h]', 'datetime64[m]', 'datetime64[s]', 'datetime64[ms]', 'datetime64[us]', 'datetime64[μs]', 'datetime64[ns]', 'datetime64[ps]', 'datetime64[fs]', 'datetime64[as]'], Union[CategoricalDtype, Literal['category']], ExtensionDtype], Mapping[Any, Union[ExtensionDtype, Union[str, dtype[generic], Type[str], Type[complex], Type[bool], Type[object]]]], Series[Any]]" [arg-type] df = df.astype(object)

but I see that Type[object] is present in the error message: Mapping[Any, Union[ExtensionDtype, Union[str, dtype[generic], Type[str], Type[complex], Type[bool], Type[object]]]], which would indicate that

df.astype({col: object}) should work. EDIT: It does.

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Mar 8, 2023

Thanks for the report. We just introduced making astype() accept specific types and mapping that to different generic Series types.

We need to add object as a valid type here:

dtype: ExtensionDtype,

and here:
| ExtensionDtype

Tagging @ramvikrams to do this and add appropriate test.

@ramvikrams
Copy link
Contributor

ramvikrams commented Mar 8, 2023

Tagging @ramvikrams to do this and add appropriate test.

Thanks I'll do the change

Dr-Irv pushed a commit that referenced this issue Mar 9, 2023
* added object support to astype

* Update test.py

* Delete test.py
twoertwein pushed a commit to twoertwein/pandas-stubs that referenced this issue Apr 1, 2023
* added object support to astype

* Update test.py

* Delete test.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants