Closed
Description
It would be useful to be able to change a column dtype inline.
For example with a DataFrame-level astype() overload:
Assuming df
has a column called str_col
that is a string (object),
df.astype(col='str_col', dtype='int').head()
The use case that brought me here is to numerically sort a string column without permanently changing it. What I was hoping to do is
df.astype(col='str_col', dtype='int').sort_values(by='str_col')
Instead, I had to do this:
df = df.assign(int_col = lambda x: x['str_col'].astype('int')).sort_values(by='int_col').drop('int_col', axis = 'columns')
The method would also support dictionaries for multiple column type changes:
df.astype({'col1':'type1', 'col2':'type2'})