Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set DateTimeIndex in existing DataFrame

I have a existing dataframe, df, which consists of the following structure:

   tick_id    stock_ticker        tick_datetime  price  volume
0   160939   A2M AU Equity  2016-10-19 09:00:00  450.0   79700
1   160940   A2M AU Equity  2016-10-19 09:00:01  450.0     100
2   160941   A2M AU Equity  2016-10-19 09:00:01  450.0    2500
3   160942   A2M AU Equity  2016-10-19 09:00:01  451.0     200

What I am looking to do is to set the "tick_datetime" as the index of the dataframe, so that it should become DateTimeIndex for easier data manipulation later.

However, executing the following command yields unexpected result.

df.set_index('tick_datetime')

What is the correct way to achieve my desired outcome?

like image 552
Roy Avatar asked Oct 20 '25 15:10

Roy


1 Answers

Try:

df['tick_datetime'] = pd.to_datetime(df['tick_datetime'])   
df.set_index('tick_datetime',inplace=True)

or:

df['tick_datetime'] = pd.to_datetime(df['tick_datetime'])
df = df.set_index('tick_datetime')
like image 169
brunormoreira Avatar answered Oct 25 '25 11:10

brunormoreira



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!