Это сбивает с толку, учитывая, насколько простой код, и все же он дает ту же ошибку на отдельных компьютерах Linux и OSX. Если df.set_index ('Date', inplace = True) запущен, то plot (x = 'Date') возвращает KeyError: "['Date'] not in index" - Но если df.set_index () закомментирован ошибка уходит.
import pandas as pd
import matplotlib.pyplot as plot
df = pd.read_csv('historical_price_data.csv')
# Seemingly makes no difference either way.
df.columns = ['Date', 'Close']
df['Date'] = pd.to_datetime(df.Date, infer_datetime_format=True)
# Uncommenting this line results in error (below) when plot(x='Date') is called.
df.set_index('Date', inplace=True)
# Seemingly makes no difference.
# df.sort_index(inplace=True)
# If set_index('Date') above, then plot(x='Date') returns KeyError: "['Date'] not in index"
df[['Date', 'Close']].plot(x='Date')
plot.show()
Это набор данных, который я использую:
Date,Close
2018-08-29,7059.7
2018-08-28,7071.01
2018-08-27,6911.9
2018-08-26,6709.98
2018-08-25,6737.52
2018-08-24,6690.88
2018-08-23,6526.36
2018-08-22,6359.99
2018-08-21,6475.9
2018-08-20,6258.74