Home > Software engineering >  set_index does not effectively set the index
set_index does not effectively set the index

Time:02-13

I have imported a timeseries dataset in pandas from a csv file.

data = pd.read_csv('djia_data.csv')
data.set_index('date')

When I print the header withdata.head(), I correctly get

enter image description here

If instead I visualize the dataframe (double click on the variable in VS code with Jupiter), I get this

enter image description here

I do not understand while I do not get date as the new index anymore. If instead I type

data = pd.read_csv('djia_data.csv')
data.set_index('date',inplace=True)
data.head()

I get this

enter image description here

When I visualize again the dataframe as a variable instead I obtain

enter image description here

In this case, date column is empty. I've searched on the net but I wasn't able to solve it. Can you please explain what I am doing wrong?

CodePudding user response:

Try to set index earlier:

data = pd.read_csv('djia_data.csv', index_col='date', parse_dates=['date'])
  • Related