You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Users often know the data types they want to convert their columns to at creation.
Can the pd.Series constructor be given an additional argument errors (default raise) to optionally convert all values to a specified type (pd.NA or np.nan if cannot cast) at creation?
e.g. Currently, the following raises a ValueError:
Extending to the pd.DataFrame constructor, can the dtype argument be altered to something like Union[pd._typing.Dtype, Dict[str, pd._typing.Dtype] | None such that the user could pass in a dictionaries of columns as strings with dtype values they want to convert them to?
(Again, the an argument errors (default raise) should be added so ValueErrors are still raised unless the user explicitly sets errors=coerce)
Currently, it is common that users must get each DataFrame column to convert manually after it is created, convert its dtype with a to_..() method using errors=coerce, and reassign back to the DataFrame column (since the to_...() methods have no inplace argument).
This feature would combine the functionality common to the other to_...() methods in one place. The method read_excel, for example, has an argument converters, which has the desired behavior sought after in this feature request when creating pd.DataFrame objects.
(ASIDE: It should be noted DataFrame.convert_dtypes doesn't have coerce functionality, so the Seriesa in the above example would simply be converted to type object)
The text was updated successfully, but these errors were encountered:
adamgranthendry
changed the title
Feature Request: DataFrame constructor dtypes dict and coerce bool args
Feature Request: Update Series/DataFrame Constructor args to Enable Column dtype Forced Conversion on Creation
Oct 20, 2021
adamgranthendry
changed the title
Feature Request: Update Series/DataFrame Constructor args to Enable Column dtype Forced Conversion on Creation
ENH: Update Series/DataFrame Constructor args to Enable Column dtype Forced Conversion on Creation
Oct 20, 2021
adamgranthendry
changed the title
ENH: Update Series/DataFrame Constructor args to Enable Column dtype Forced Conversion on Creation
ENH: Update Series/DataFrame Constructor args to Enable dtype Forced Conversion on Creation
Oct 20, 2021
Extra keywords are somewhat out of scope for the discussion in #22384, I would say (the discussion is already broad, so whatever we can leave out, the better :)). So reopening this.
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: