Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 the data type datetime64[ns] to DatetimeIndex with no success. Here are my attempts:

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'])
print(test.dtypes.value_counts())

# Attempt using pd.DateTimeIndex
test['datetime'] = pd.DatetimeIndex(test['datetime'])
print(test.dtypes.value_counts())

if isinstance(test['datetime'], pd.DatetimeIndex):
    print('Success')

# Attempt using pd.to_datetime without format string
test['datetime'] = pd.to_datetime(test['datetime'])
print(test.dtypes.value_counts())

if isinstance(test['datetime'], pd.DatetimeIndex):
    print('Success')

# Attempt using pd.to_datetime with format string
test['datetime'] = pd.to_datetime(test['datetime'], format='%Y-%m-%d %h:%m:%s')
print(test.dtypes.value_counts())

if isinstance(test['datetime'], pd.DatetimeIndex):
    print('Success')

I am using the latest version of pandas - 0.25.3 and am on python 3.7. Any constructive advice is well appreciated.

like image 680
UGuntupalli Avatar asked Sep 12 '25 10:09

UGuntupalli


1 Answers

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)
like image 123
Nick Becker Avatar answered Sep 14 '25 01:09

Nick Becker