'How to import data with dates as index from excel with pandas
Solution 1:[1]
I found the answer.Adding parse_dates=True, index_col=0 to the import command like this:
df = pd.read_excel('C:/Users/Me/Data.xlsx', sheet_name='Prices', parse_dates=True, index_col=0)
The output is this:
Solution 2:[2]
What you are trying to do is to set Date as an index, if I get it right:
df.set_index('Date')
Solution 3:[3]
I found a better way to import them, because when I was trying to calculate the monthly returns it does not work. So I use this new code and the date were perfect.
df = pd.read_excel('C:/Users/Me/Data.xlsx', sheet_name='Prices')
df.index = pd.to_datetime(df['Date'])
df.drop(['Date'], axis = 'columns', inplace=True)
df.head()
and this is the result:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rodalm |
| Solution 2 | Ignatius Reilly |
| Solution 3 |




