I have a question for you. (Second)

Asked 2 years ago, Updated 2 years ago, 133 views

    import pandas as pd
    import pandas_datareader.data as web
    from pandas import Series, DataFrame
    import datetime
    import matplotlib.pylab as plt
    import numpy
    import seaborn as sns

    stock = {'msft' : 'MSFT','tencent' : '0700.HK'}
    start = datetime.datetime(2017, 1, 1)
    end = datetime.datetime(2020, 1, 27)

    for name, code in stock.items():

    foo = web.DataReader(code, 'yahoo', start, end)
    bar = name + '_close'
    foo.rename(columns = {'Adj Close': bar}, inplace = True)
    stock_price = pd.concat(foo[bar], axis = 1)

---- Error below

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-104-3bd2e868540e> in <module>
      8     bar = name + '_close'
      9     foo.rename(columns = {'Adj Close': bar}, inplace = True)
---> 10     stock_price = pd.concat(foo[bar], axis = 1)

C:\conda\lib\site-packages\pandas\core\reshape\concat.py in concat(objs, axis, join, join_axes, ignore_index, keys, levels, names, verify_integrity, sort, copy)
    253         verify_integrity=verify_integrity,
    254         copy=copy,
--> 255         sort=sort,
    256     )
    257 

C:\conda\lib\site-packages\pandas\core\reshape\concat.py in __init__(self, objs, axis, join, join_axes, keys, levels, names, ignore_index, verify_integrity, copy, sort)
    282                 "first argument must be an iterable of pandas "
    283                 "objects, you passed an object of type "
--> 284                 '"{name}"'.format(name=type(objs).__name__)
    285             )
    286 

TypeError: first argument must be an iterable of pandas objects, you passed an object of type "Series"
----------------------

What's the solution?

for pandas python

2022-09-22 19:08

1 Answers

Are you saying that you want to get closing price information for each company?

import pandas as pd
import pandas_datareader.data as web
from pandas import Series, DataFrame
import datetime
import matplotlib.pylab as plt
import numpy
import seaborn as sns

stock = {'msft' : 'MSFT','tencent' : '0700.HK'}
start = datetime.datetime(2017, 1, 1)
end = datetime.datetime(2020, 1, 27)

def get_stock_company(name, code):
    foo = web.DataReader(code, 'yahoo', start, end)
    bar = f'{name}_close'
    foo.rename(columns = {'Adj Close': bar}, inplace = True)
    return foo[bar]

stock_price = pd.concat([get_stock_company(name, code) for name, code in stock.items()], axis=1)


2022-09-22 19:08

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.