def change_date_column_type(df,change_column_name):
df=df.assign(change_column_name=df[change_column_name].astype('datetime64'))
# return the result
return df
result = change_date_column_type(test_df,'release_date') print(result)
output:
movie_title release_date genre MPAA_rating change_column_name
0 titanic Dec 21, 1937 Drama R 1937-12-21
1 frozen Feb 9, 1940 Musical PG 1940-02-09
2 rythm Nov 13, 1940 Family G 1940-11-13
3 godfather Nov 12, 1946 Drama R 1946-11-12
4 red Feb 15, 1950 Musical PG-13 1950-02-15
I want to change the release_Date column type to datetime64, but why the df.assign(change_column_name not taking the argument value and taking the literal value and created new column ? any help would be appreciated
expecting the release_date column to change type as datetime64 using a function call
CodePudding user response:
creating a new column is expected behavior --> see docs for usage of df.assign()
if you don't want to create a new column then just use astype
def change_date_column_type(df,change_column_name):
df[change_column_name] = df[change_column_name].astype('datetime64')
return df