Description
Pandas version checks
- I have checked that the issue still exists on the latest versions of the docs on
main
here
Location of the documentation
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.astype.html
Documentation problem
Currently, the parameter specification
dtype: data type, or dict of column name
does not mention that Series are natively supported.
Suggested fix for documentation
Something along the lines of
dtype: string, data type, Series or Mapping of column name
Use a data type like object (string, numpy.dtype, pandas.ExtensionDtype or Python type) to cast entire pandas object to the same type. Alternatively, use a Mapping such as a dictionary of the form {col: dtype, …}, where col is a column label and dtype is the scalar type to cast one or more of the DataFrame’s columns to column-specific types.
One might also want to add: (#43837)
The Mapping is not allowed to contain column names that are present in the DataFrame.
I also noticed that the relevant code
pandas/pandas/core/dtypes/inference.py
Lines 266 to 295 in 91111fd
essentially is a weaker version of isinstance(obj, collections.abc.Mapping)
that I guess was introduced to also catch Series
.
I would propose to think about replacing this with isinstance(obj, Mapping | Series)
when appropriate.
Related: pandas-dev/pandas-stubs#410