I’ve transformed OrderDate column from its existing object data type into a ‘datetime64[ns]’ with astype method.
But after dtypes() I saw it didn’t work.
Why?
Thanks.
`df_raw.OrderDate.astype(‘datetime64[ns]’),
`astype done
df_raw.dtypes
dtypes not changed
Fwooper Fwooper is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
I think (assuming) you didn’t assign is back to the column is causing the issue
Code working on sample data as below
Duration Pulse Maxpulse Calories
0 60 110 130 409.1
1 60 117 145 479.0
2 60 103 135 340.0
3 45 109 175 282.4
4 45 117 148 406.0
Code sample
print(df.dtypes)
print("----------------")
df.Duration = df.Duration.astype('int32')
print(df.dtypes)
Got below response
Duration int64
Pulse int64
Maxpulse int64
Calories float64
dtype: object
----------------
Duration int32
Pulse int64
Maxpulse int64
Calories float64
dtype: object
Now for the answer to this question, if you did only
df_raw.OrderDate.astype('datetime64[ns]')
Try doing
df_raw.OrderDate = df_raw.OrderDate.astype('datetime64[ns]')
Update: for multiple columns as @user19077881 mentioned in comment, you can use dictionary as below
df = df.astype({'col1': 'object', 'col2': 'int'})
8