0

I have an excel file with data. I defined this file as a DataFrame (5000,12) using python/pandas. As an index, I set the date based on the below:

Data_Final=Data.set_index(['Date Time']) # Data_Final is Dataframe

For example, the first index is 01/01/2016 00:00. Now I want this index in datetime. How is this conversion done?

FObersteiner
  • 22,500
  • 8
  • 42
  • 72

1 Answers1

0

use the .to_datetime() method

Data_Final = Data
Data_Final['Date Time'] = pd.to_datetime(Data['Date Time'])
Data_Final.set_index('Date Time', inplace=True)

How to convert string to datetime format in pandas python?

  • Not working. Maybe because the Date Time column is an index and it can not read it as a datetime. – Stelios Loizi Oct 13 '22 at 16:22
  • this answer suggests reassigning initial `Data` to `Data_Final` to change the `Date Time` column dtype and then set the index again. You may avoid such conflicts changing the index directly `Data_Final.index = pd.to_datetime(Data_Final.index)` – Nikita Shabankin Oct 17 '22 at 11:21