Convert Datetime64[ns] Column To Datetimeindex In Pandas
One of the packages that I am working with has a pre-requisite that the index of the data frame needs to be a pandas DatetimeIndex. So, I have been trying to convert a column of th
Solution 1:
You can cast an index as a datetime
. Use set_index
on your column, and then typecast.
import pandas as pd
my_data = [[1,'2019-05-01 04:00:00'], [2, '2019-05-01 04:01:00'], [3, '2019-05-01 04:02:00']]
test = pd.DataFrame(my_data, columns=['count', 'datetime'])
test.set_index('datetime').index.astype('datetime64[ns]')
DatetimeIndex(['2019-05-01 04:00:00', '2019-05-01 04:01:00',
'2019-05-01 04:02:00'],
dtype='datetime64[ns]', name='datetime', freq=None)
Post a Comment for "Convert Datetime64[ns] Column To Datetimeindex In Pandas"