简体   繁体   中英

Python and Pandas - Sorting by Date

What I've tried for a sort "should" be working; but it is not.

I've queried the "Alpha Vantage" API using the "alpha_vantage" Python library. Below is my code. I am requesting to sort by date; but, as you can see from the output in the df.head() the sorting by date is in the wrong direction. However, the plot is going in the right direction.

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())

Output:

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 get it done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM