Home > front end >  Creating Datetime index in python
Creating Datetime index in python

Time:12-06

I am trying to create datetime index in python. I have an existing dataframe with date column (CrimeDate), here is a snapshot of it:

enter image description here

The date is not in datetime format though.

I intent to have an output similar to the below format, but with my existing dataframe's date column-

enter image description here

The Crimedate column has approx. 334192 rows and start date from 2021-04-24 to 1963-10-30 (all are in sequence of months and year)

CodePudding user response:

First you'll need to convert the date column to datetime:

df['CrimeDate'] = pd.to_datetime(df['CrimeDate'])

And after that set that column as the index:

df.set_index(['CrimeDate'], inplace=True)

Once set, you can access the datetime index directly:

df.index
  • Related