0

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)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
John
  • 279
  • 1
  • 3
  • 16

1 Answers1

1

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