Home > Blockchain >  Python data as string from dataframe
Python data as string from dataframe

Time:10-01

I'm having trouble figuring out how to make this work. I have this dataframe read from a csv:

        Ticker Nome File Ticker No - Data Inizio   Data Fine
0  AABA-201910   AABA_NQ        AABA  16/09/1998  16/06/2017
1          AAL    AAL_NQ         AAL  22/12/2014  17/04/2020
2         AAPL   AAPL_NQ        AAPL  03/01/1995  12/09/2021
3  ABGX-200603   ABGX_NQ        ABGX  18/12/2000  20/12/2002

And i need to use data from every row in a link like this: "https://api.tiingo.com/tiingo/daily/AAPL/prices?startDate=1995-03-01&endDate=2021-01-01"

With the code below I get data in the right format (I suppose) but I still get error because of wrong format data:

        Ticker Nome File Ticker No - Data Inizio   Data Fine
0  AABA-201910   AABA_NQ        AABA  1998-09-16  2017-06-16
1          AAL    AAL_NQ         AAL  2014-12-22  2020-04-17
2         AAPL   AAPL_NQ        AAPL  1995-03-01  2021-12-09
3  ABGX-200603   ABGX_NQ        ABGX  2000-12-18  2002-12-20

Thats the code I'm trying to use:

import requests
import time
from bs4 import BeautifulSoup
import json
import os
import pandas as pd
from win32com.client import Dispatch

df= pd.read_csv('NQ TICKER BIAS 1.csv',delimiter=";")
symbol=df["Ticker No -"].to_string(index=False)
symbolfile=df["Nome File"].to_string(index=False)
df["Data Inizio"]=pd.to_datetime(df["Data Inizio"]).dt.strftime('%Y-%m-%d')
df["Data Fine"]=pd.to_datetime(df["Data Fine"]).dt.strftime('%Y-%m-%d')
start_date=df["Data Inizio"].to_string(index=False)
end_date=df["Data Fine"].to_string(index=False)
headers = {MY API}     
  
#print ("downloading",symbol, "...")

try:
     response = requests.get("https://api.tiingo.com/tiingo/daily/" symbol "/prices?startDate=" start_date "&endDate=" end_date,headers=headers)
     soup = BeautifulSoup(response.content, "html.parser")
     print(soup)     
     print ("finished writing ",symbol,"txt file.") 
except: 
   print ("error downloading ",symbol)

The result of print(soup):

["Error: Symbol format was not correct."]
["Error: Start date format was not correct. Must be in YYYY-MM-DD format."]
["Error: End date format was not correct. Must be in YYYY-MM-DD format."]

When I use as end_date dateToday I dont get any error for End date:

dateToday = str(time.strftime("%Y-%m-%d"))

So I believe I'm doing something wrong on converting dataframe to string

CodePudding user response:

You can use this example how to construct the url:

# if columns are converted already, skip it:
df["Data Inizio"] = pd.to_datetime(df["Data Inizio"])
df["Data Fine"] = pd.to_datetime(df["Data Fine"])

url = "https://api.tiingo.com/tiingo/daily/{ticker}/prices?startDate={start_date}&endDate={end_date}"
df["url"] = df.apply(
    lambda x: url.format(
        ticker=x["Ticker No -"],
        start_date=x["Data Inizio"].strftime("%Y-%m-%d"),
        end_date=x["Data Fine"].strftime("%Y-%m-%d"),
    ),
    axis=1,
)

# now you can use the `url` column:
for u in df["url"]:
    print(u)
    # response = requests.get(u)
    # soup = BeautifulSoup(response.content, "html.parser")
    # ...

Prints:

https://api.tiingo.com/tiingo/daily/AABA/prices?startDate=1998-09-16&endDate=2017-06-16
https://api.tiingo.com/tiingo/daily/AAL/prices?startDate=2014-12-22&endDate=2020-04-17
https://api.tiingo.com/tiingo/daily/AAPL/prices?startDate=1995-03-01&endDate=2021-12-09
https://api.tiingo.com/tiingo/daily/ABGX/prices?startDate=2000-12-18&endDate=2002-12-20

CodePudding user response:

Make sure you fully use the dataframe capabilites, like iterating over rows etc. Your code seems correct so far, I've cleaned it up a little and parsed the reponses.

import requests
import pandas as pd
import json

df= pd.read_csv('NQ TICKER BIAS 1.csv',delimiter=",")

df["Data Inizio"]=pd.to_datetime(df["Data Inizio"]).dt.strftime('%Y-%m-%d')
df["Data Fine"]=pd.to_datetime(df["Data Fine"]).dt.strftime('%Y-%m-%d')


headers = {'Content-Type': 'application/json'}     

for symbol, file_name, ticker_number, start_date, end_date in df.itertuples(index=None):
    try:
        url = "https://api.tiingo.com/tiingo/daily/" symbol "/prices?startDate=" start_date "&endDate=" end_date "&token=" "4723bf31f081f38876619769ff973de887639b01"
        print(f"Getting {url}")
        response = requests.get(url ,headers=headers)
        resp_json = response.json()
        with open(f"{symbol}.json", "w") as fp:
            json.dump(resp_json, fp)
    except: 
        print ("error downloading ",symbol)
  • Related