data = pd.read_csv('data.csv')
print(data)
Output:
code trade_date open high low close volume
0 2GO 2012-06-04 1.750 1.750 1.750 1.750 5000.0
1 2GO 2012-06-05 1.750 1.980 1.750 1.900 8000.0
2 2GO 2012-06-07 1.960 1.960 1.800 1.800 8000.0
3 2GO 2012-06-11 1.900 1.980 1.900 1.980 50000.0
4 2GO 2012-06-13 1.990 1.990 1.900 1.900 19000.0
I want to set the trade_date as index . . .
data.set_index('trade_date', inplace=True)
print(data)
Output:
code open high low close volume
trade_date
2012-06-04 2GO 1.750 1.750 1.750 1.750 5000.0
2012-06-05 2GO 1.750 1.980 1.750 1.900 8000.0
2012-06-07 2GO 1.960 1.960 1.800 1.800 8000.0
2012-06-11 2GO 1.900 1.980 1.900 1.980 50000.0
2012-06-13 2GO 1.990 1.990 1.900 1.900 19000.0
. . . then write it to a csv file.
data.to_csv('data_reidx.csv')
However when I read the csv file again, trade_date is back to being a column and was replaced by traditional indexing.
data = pd.read_csv('data_reidx.csv')
Output:
trade_date code open high low close volume
0 2012-06-04 2GO 1.750 1.750 1.750 1.750 5000.0
1 2012-06-05 2GO 1.750 1.980 1.750 1.900 8000.0
2 2012-06-07 2GO 1.960 1.960 1.800 1.800 8000.0
3 2012-06-11 2GO 1.900 1.980 1.900 1.980 50000.0
4 2012-06-13 2GO 1.990 1.990 1.900 1.900 19000.0
How do I preserve the datetime index when writing dataframe to csv?
data.to_csv('data_reidx.csv', index_label=False), the date index is preserved even when reading the csv normally, but the 'trade_date' column name is gone. Both works just fine. The column name is not important in my case.