John
John

Reputation: 349

Creating Datetime index in python

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)

Upvotes: 0

Views: 1166

Answers (1)

tvanvalkenburg
tvanvalkenburg

Reputation: 128

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

Upvotes: 1

Related Questions