Один из пакетов, с которыми я работаю, имеет предварительное условие, что индекс фрейма данных должен быть pandas DatetimeIndex. Итак, я пытался преобразовать столбец типа данных datetime64 [ns] в DatetimeIndex безуспешно. Вот мои попытки:
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')
Я использую последнюю версию pandas - 0.25.3 и на python 3.7. Любые конструктивные советы приветствуются.