Search code examples
pythonpandasdataframetime-seriesalpha-vantage

How to filter for dates range in timeseries or dataframe using python


Still a newbie with Python just trying to learn this stuff. Appreciate any help.

Right now when I connect to Alpha Vantage I get the full range of data for all the dates and it looks like this enter image description here

I found some good sources for guides, but I keep getting empty dataframes or errors enter image description here

This is how the code looks so far

import pandas as pd
from pandas import DataFrame
import datetime
from datetime import datetime as dt
from alpha_vantage.timeseries import TimeSeries
import numpy as np

stock_ticker = 'SPY'
api_key = open('/content/drive/My Drive/Colab Notebooks/key').read()

ts = TimeSeries (key=api_key, output_format = "pandas")
data_daily, meta_data = ts.get_daily_adjusted(symbol=stock_ticker, outputsize ='full')
#data_date_changed = data[:'2019-11-29']

data = pd.DataFrame(data_daily)

df.loc[datetime.date(year=2014,month=1,day=1):datetime.date(year=2015,month=2,day=1)]


Solution

  • The answer for this is

    stock_ticker = 'SPY'
    api_key = 'apikeyddddd'
    
    ts = TimeSeries (key=api_key, output_format = "pandas")
    data_daily, meta_data = ts.get_daily_adjusted(symbol=stock_ticker, outputsize ='full')
    
    test = data_daily[(data_daily.index > '2014-01-01') & (data_daily.index <= '2017-08-15')]
    
    print(data_daily)
    print(test)