What I am trying to do is convert my date to datetime64[D]. At source - Some of the dates are object type and some of the dates are datetime64[ns]. I am not asking how to do the conversion - I know it. But something's happening while I create a new column, and following code seems to have no impact and datetime64[ns] doesnt change.
df2['date'].values.astype('datetime64[D]')
This is the sample dataframe:
d = {'date' :['2015-10-05 15:08:43', '2015-10-05 19:17:12', '2015-10-06 15:51:22', '2015-10-06 19:39:18', '2015-10-06 19:58:06', '2015-12-18 11:09:01'], 'name': ['john', 'tom', 'phill', 'nero', 'bob', 'rob']}
df2 = pd.DataFrame(data = d)
date in df2 is object type. When we do the following
df2['date'] = pd.to_datetime(df2['date'])
date becomes dtype: datetime64[ns].
Now following code works and produces datetime64[D] output
df2['date'].values.astype('datetime64[D]')
But when I create a new column, it goes back to
df2['date'] = df2['date'].values.astype('datetime64[D]')
See the output here -
Name: date, dtype: datetime64[ns]
So, my question is that why is it not working when I am creating a new column ?
Note: I know that last line produces warning. So I also tried below method but its not producing datetime64[D]
newcol = df2['date'].values.astype('datetime64[D]')
df2.assign(date = newcol)