2

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
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
billv1179
  • 323
  • 5
  • 15
  • `sort_values` does not work in place. `df = df.sort_values(by=['date'])` instead. No sorting has taken place here so presumably the API returns the values in a sorted manner already (or a coincidence), but in the opposite direction than you want: `df = df.sort_values(by=['date'], ascending=False)` is needed there – roganjosh Nov 13 '19 at 15:44

1 Answers1

2

df = df.sort_values(by=['date'])

should get it done

DBA108642
  • 1,995
  • 1
  • 18
  • 55