ENH: Update Series/DataFrame Constructor args to Enable dtype
Forced Conversion on Creation
#44117
Labels
dtype
Forced Conversion on Creation
#44117
Users often know the data types they want to convert their columns to at creation.
pd.Series
constructor be given an additional argumenterrors
(defaultraise
) to optionally convert all values to a specified type (pd.NA
ornp.nan
if cannot cast) at creation?e.g. Currently, the following raises a
ValueError
:However, it would be nice to have the following capability:
pd.DataFrame
constructor, can thedtype
argument be altered to something likeUnion[pd._typing.Dtype, Dict[str, pd._typing.Dtype] | None
such that the user could pass in a dictionaries of columns as strings withdtype
values they want to convert them to?(Again, the an argument
errors
(defaultraise
) should be added soValueError
s are still raised unless the user explicitly setserrors=coerce
)Currently, it is common that users must get each
DataFrame
column to convert manually after it is created, convert itsdtype
with ato_..()
method usingerrors=coerce
, and reassign back to theDataFrame
column (since theto_...()
methods have noinplace
argument).This feature would combine the functionality common to the other
to_...()
methods in one place. The methodread_excel
, for example, has an argumentconverters
, which has the desired behavior sought after in this feature request when creatingpd.DataFrame
objects.(ASIDE: It should be noted
DataFrame.convert_dtypes
doesn't havecoerce
functionality, so theSeries
a
in the above example would simply be converted to typeobject
)The text was updated successfully, but these errors were encountered: