我正在尝试从熊猫数据框中将数据绘制为时间(年)的函数。数据摘要如下所示:
DATE WALCL
0 2010-08-18 2313662
1 2010-08-25 2301015
2 2010-09-01 2301996
3 2010-09-08 2305802
4 2010-09-15 2296079
517 2020-07-15 6958604
518 2020-07-22 6964755
519 2020-07-29 6949032
520 2020-08-05 6945237
521 2020-08-12 6957277
我尝试使用以下代码绘制数据:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
years = mdates.YearLocator() # every year
months = mdates.MonthLocator() # every month
years_fmt = mdates.DateFormatter('%Y')
dfData = pd.read_csv(sPathIn+sFname, skiprows = 0)
ax = dfData.plot()
ax.xaxis.set_major_locator(years)
ax.xaxis.set_major_formatter(years_fmt)
ax.xaxis.set_minor_locator(months)
datemin = np.datetime64(dfData['DATE'][0], 'Y')
datemax = np.datetime64(dfData['DATE'].iloc[-1], 'Y') + np.timedelta64(1, 'Y')
ax.set_xlim( datemin, datemax)
plt.show()
当我运行此代码时,绘图轴正确显示,但时间序列数据 (WALCL) 未出现。
如果我省略ax.set_xlim( datemin, datemax)
,则会显示时间序列数据,但 x 轴的格式不再正确(从 1970 年开始,一直运行到 1971 年)。
这是一个修改后的代码示例:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
years = mdates.YearLocator() # every year
months = mdates.MonthLocator() # every month
years_fmt = mdates.DateFormatter('%Y')
sPathIn = "C:\\Users\\reg\\projects\\notes\\Political_Economy\\S&P+Fed-Assets\\"
sFname = "WALCL.csv"
这是回溯:
Traceback (most recent call last):
File "C:\Users\reg\projects\Notes\Political_Economy\S&P+Fed-Assets\Python\s&p-fed-assets-v0.2.3.py", line 25, in <module>
dfData.set_index('DATE', inplace=True)
File "C:\Users\reg\Anaconda3\lib\site-packages\pandas\core\frame.py", line 4545, in set_index
raise KeyError(f"None of {missing} are in the columns")
KeyError: "None of ['DATE'] are in the columns"
# load data
dfData = pd.read_csv(sPathIn+sFname, skiprows = 0, parse_dates=['DATE'], index_col='DATE')
#set up plot fxn
dfData.set_index('DATE', inplace=True)
ax = dfData.plot('DATE', 'WALCL')
# format the ticks
ax.xaxis.set_major_locator(years)
ax.xaxis.set_major_formatter(years_fmt)
ax.xaxis.set_minor_locator(months)
datemin = np.datetime64(dfData['DATE'][0], 'Y')
datemax = np.datetime64(dfData['DATE'].iloc[-1], 'Y') + np.timedelta64(1, 'Y')
ax.set_xlim( datemin, datemax)
plt.show()