1

I imported a csv file in python. Then, I changed the first column to datetime format.

datetime                  Bid32    Ask32

2019-01-01 22:06:11.699  1.14587  1.14727  
2019-01-01 22:06:12.634  1.14567  1.14707  
2019-01-01 22:06:13.091  1.14507  1.14647  

I saw three ways for indexing first column.

df.index = df.datetime
del datetime

or

df.set_index('datetime', inplace=True)

and

df.set_index(pd.DatetimeIndex('datetime'), inplace=True)

My question is about the second and third ways. Why in some sources they used pd.DatetimeIndex() with df.set_index() (like third code) while the second code was enough?

SaeedMMX
  • 23
  • 1
  • 7

1 Answers1

0

In case you are not changing the 'datetime' column with to_datetime():

df = pd.DataFrame(columns=['datetime', 'Bid32', 'Ask32'])
df.loc[0] = ['2019-01-01 22:06:11.699', '1.14587', '1.14727']

df.set_index('datetime', inplace=True)  # option 2
print(type(df.index))

Result: pandas.core.indexes.base.Index

vs.

df = pd.DataFrame(columns=['datetime', 'Bid32', 'Ask32'])
df.loc[0] = ['2019-01-01 22:06:11.699', '1.14587', '1.14727']

df.set_index(pd.DatetimeIndex(df['datetime']), inplace=True)  # option 3
print(type(df.index))

Result: pandas.core.indexes.datetimes.DatetimeIndex

So the third one with pd.DatetimeIndex() makes it an actual datetime index, which is what you want.

Documentation:

pandas.Index

pandas.DatetimeIndex

PythonSherpa
  • 2,560
  • 3
  • 19
  • 40
  • I examined in your way. With `datetime64` column format, your first method gives the same result that you get in your second method. Thanks – SaeedMMX Jul 23 '19 at 16:32