Python 和 Pandas - 按日期排序
Python and Pandas - Sorting by Date
我尝试过的某种 "should" 正在发挥作用;但事实并非如此。
我使用 "alpha_vantage" Python 库查询了 "Alpha Vantage" API。下面是我的代码。我要求按日期排序;但是,正如您从 df.head() 的输出中看到的那样,按日期排序的方向是错误的。然而,剧情正在朝着正确的方向发展。
df, meta_data = ts.get_daily(symbol='AAPL',outputsize=365)
df['4. close'].plot()
plt.title('Intraday Times Series for the AAPL stock (5 min)')
# Attempting sort here! Also I've tried df.sort_values(by=['date'])
df.sort_values(by=['date'])
print(plt.show())
print(df.head())
输出:
None
date 1. open 2. high 3. low 4. close 5. volume
2019-11-13 261.13 262.95 261.07 262.4307 3284781.0
2019-11-12 261.55 262.79 260.92 261.9600 21826100.0
2019-11-11 258.30 262.47 258.28 262.2000 20455300.0
2019-11-08 258.69 260.44 256.85 260.1400 17496600.0
2019-11-07 258.74 260.35 258.11 259.4300 23735100.0
df = df.sort_values(by=['date'])
应该搞定
我尝试过的某种 "should" 正在发挥作用;但事实并非如此。
我使用 "alpha_vantage" Python 库查询了 "Alpha Vantage" API。下面是我的代码。我要求按日期排序;但是,正如您从 df.head() 的输出中看到的那样,按日期排序的方向是错误的。然而,剧情正在朝着正确的方向发展。
df, meta_data = ts.get_daily(symbol='AAPL',outputsize=365)
df['4. close'].plot()
plt.title('Intraday Times Series for the AAPL stock (5 min)')
# Attempting sort here! Also I've tried df.sort_values(by=['date'])
df.sort_values(by=['date'])
print(plt.show())
print(df.head())
输出:
None
date 1. open 2. high 3. low 4. close 5. volume
2019-11-13 261.13 262.95 261.07 262.4307 3284781.0
2019-11-12 261.55 262.79 260.92 261.9600 21826100.0
2019-11-11 258.30 262.47 258.28 262.2000 20455300.0
2019-11-08 258.69 260.44 256.85 260.1400 17496600.0
2019-11-07 258.74 260.35 258.11 259.4300 23735100.0
df = df.sort_values(by=['date'])
应该搞定