1

I load data from yahoo finance using the motor_daily function. It takes in a list of tickers and gets me the data. Here are the used libs:

import pandas as pd
import numpy as np
import datetime as dt
import yfinance as yf

Here is the function definition:

def motor_daily(ticker_file):   
    tickers_list = ticker_file #SP100
    stocks = yf.download(tickers_list, start = start, end = tomorrow) #YYYY-MM-DD
    company_name = []
    ticker_code = []
    for ticker in tickers_list:
        loaded_ticker = yf.Ticker(ticker)
        tickers = ticker
        ticker_code.append(tickers)
    finance = pd.DataFrame(ticker_code)
    finance["Ticker"] = pd.DataFrame(ticker_code)
    finance["Ticker_start"] = finance["Ticker"].str.split('-').str[0]
    finance= finance.drop(columns=[0])
    stocks_close = stocks.Close
    stocks_close = stocks_close.reset_index()
    return stocks_close
def ticker_data(list):
    data = []
    for ticks in list:
        data.append(motor_daily(ticks))
    return data

The above function loads closing prices for each ticker / stock name in the list (therefore the loop) and stores this in data.

list_of_lists includes:

[['VOW3.DE', 'BMW.DE', 'BEI.DE', 'DPW.DE', 'FME.DE'],
 ['ISS.CO', 'LUN.CO', 'CARL-B.CO', 'TRYG.CO', 'SIM.CO']]

Output of print(ticker_data(list_of_list))

[ Date        BEI.DE     BMW.DE     DPW.DE     FME.DE     VOW3.DE
0 2021-03-10  86.860001  81.339996  43.650002  60.840000  196.020004
1 2021-03-11  86.139999  78.519997  44.549999  61.340000  192.039993
2 2021-03-12  87.080002  77.480003  45.060001  60.939999  190.220001
3 2021-03-15  86.959999  77.800003  44.919998  60.759998  194.779999
4 2021-03-16  87.680000  80.500000  45.580002  61.259998  207.850006
5 2021-03-17  88.260002  85.459999  45.419998  60.779999  230.800003,         
Date        CARL-B.CO      ISS.CO      LUN.CO    SIM.CO     TRYG.CO
0 2021-03-10     1012.0  122.599998  243.600006   768.0  135.399994
1 2021-03-11     1009.0  120.300003  235.300003   780.0  143.500000
2 2021-03-12     1006.0  121.150002  237.000000   772.5  143.699997
3 2021-03-15     1006.5  124.250000  236.300003   783.0  145.100006
4 2021-03-16      983.0  125.550003  236.100006   795.5  147.399994
5 2021-03-17      982.0  121.949997  230.300003   778.0  143.899994]

When I try to convert the output to a dataframe using: df = pd.DataFrame(ticker_data(list_of_list)) output is

ValueError: Must pass 2-d input. shape=(2, 6, 6)

I cannot convert this to a pandas dataframe, how should I go about doing this?

3
  • Can you show your imports and function definitions please? Commented Mar 17, 2021 at 19:53
  • sure, I can, NP. Commented Mar 17, 2021 at 20:36
  • Added for ya mate Commented Mar 18, 2021 at 1:32

2 Answers 2

2

Your motor_daily has a bunch of unused elements. Also, I had to define the start and end times.

import pandas as pd
import numpy as np
import datetime as dt
import yfinance as yf

def motor_daily(ticker_list):
    start = pd.Timestamp('now').normalize() - pd.offsets.Day(7)
    end = pd.Timestamp('now').normalize() + pd.offsets.BusinessDay(0)
    return yf.download(ticker_list, start=start, end=end).Close

list_of_lists = [
    ['VOW3.DE', 'BMW.DE', 'BEI.DE', 'DPW.DE', 'FME.DE'],
    ['ISS.CO', 'LUN.CO', 'CARL-B.CO', 'TRYG.CO', 'SIM.CO']
]

df = pd.concat(map(motor_daily, list_of_lists), axis=1)

# I transposed for prettier printing
df.T

Date        2021-03-10   2021-03-11   2021-03-12   2021-03-15  2021-03-16
BEI.DE       86.860001    86.139999    87.080002    86.959999   87.680000
BMW.DE       81.339996    78.519997    77.480003    77.800003   80.500000
DPW.DE       43.650002    44.549999    45.060001    44.919998   45.580002
FME.DE       60.840000    61.340000    60.939999    60.759998   61.259998
VOW3.DE     196.020004   192.039993   190.220001   194.779999  207.850006
CARL-B.CO  1012.000000  1009.000000  1006.000000  1006.500000  983.000000
ISS.CO      122.599998   120.300003   121.150002   124.250000  125.550003
LUN.CO      243.600006   235.300003   237.000000   236.300003  236.100006
SIM.CO      768.000000   780.000000   772.500000   783.000000  795.500000
TRYG.CO     135.399994   143.500000   143.699997   145.100006  147.399994
Sign up to request clarification or add additional context in comments.

2 Comments

can I use group by on date?
Looks good, thanks for the optimization of motor_daily
0

You can iterate through ticker_data(list_of_list) and make multiple dataframes:

lol = [['VOW3.DE', 'BMW.DE', 'BEI.DE', 'DPW.DE', 'FME.DE'],
 ['ISS.CO', 'LUN.CO', 'CARL-B.CO', 'TRYG.CO', 'SIM.CO']]

res = ticker_data(lol)
dataframes = [pd.DataFrame(lst) for lst in res]
print(dataframes[0])

#
           Date     BEI.DE     BMW.DE     DPW.DE     FME.DE     VOW3.DE
0    1996-11-08        NaN  18.171000        NaN        NaN         NaN
1    1996-11-11        NaN  18.122000        NaN        NaN         NaN
2    1996-11-12        NaN  18.259001        NaN        NaN         NaN
3    1996-11-13        NaN  18.230000        NaN        NaN         NaN
4    1996-11-14        NaN  18.289000        NaN        NaN         NaN
...         ...        ...        ...        ...        ...         ...
6241 2021-03-11  86.139999  78.519997  44.549999  61.340000  192.039993
6242 2021-03-12  87.080002  77.480003  45.060001  60.939999  190.220001
6243 2021-03-15  86.959999  77.800003  44.919998  60.759998  194.779999
6244 2021-03-16  87.680000  80.500000  45.580002  61.259998  207.850006
6245 2021-03-17  88.260002  85.459999  45.419998  60.779999  230.800003

1 Comment

Is there a way in which I can merge the dataframes to a single DF instead of having to go through the different dfs in the df?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.