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)

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.