Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

python - AttributeError: 'Index' object has no attribute 'strftime'

I'm having difficulty understanding why this error message occurs... I made a function and it functions well with small data but error shows up when iteration gets large. Any suggestion will be really appreciated..

this is the fuction i ran

KOSPI = fdr.StockListing('kospi')
sample = KOSPI.head(30)
sample

temp=pd.DataFrame([])
def get_data(df, base_date,period):
  for i in df['Symbol']:
        #get data and return
    df = fdr.DataReader(i, base_date)
    df.index = df.index.strftime('%Y-%m')
    df = df.groupby(df.index).tail(1)
    df['rtn'] = df['Close'].pct_change(period) * 100 +1
    #stack multiple stocks side by side
    #set code as col
    temp[i]=df['rtn']
  return temp

test=get_data(sample, '2020', 3)
test


<bound method NDFrame.to_clipboard of    Symbol Market  ...                       HomePage Region
1  095570  KOSPI  ...         http://www.ajnet.co.kr  ?????
2  006840  KOSPI  ...  http://www.aekyunggroup.co.kr  ?????
6  027410  KOSPI  ...           http://www.bgf.co.kr  ?????
7  282330  KOSPI  ...       http://www.bgfretail.com  ?????
8  138930  KOSPI  ...           http://www.bnkfg.com  ?????
[5 rows x 10 columns]>

and it acts well and returns the df below

<bound method NDFrame.to_clipboard of             
              095570     006840     027410     282330     138930
Date                                                          
2020-01        NaN        NaN        NaN        NaN        NaN
2020-02        NaN        NaN        NaN        NaN        NaN
2020-03        NaN        NaN        NaN        NaN        NaN
2020-04 -22.853211 -15.639209  -4.945419  -3.242424 -24.185185
2020-05  -7.435208  -8.771310   0.335548  11.231023 -16.666126
2020-06  -2.050847   4.611111  16.217391   6.303030  13.739572
2020-07 -13.591700 -27.656126 -11.746114 -20.202532   2.782178
2020-08 -15.421896 -22.963134  -7.584169 -22.652695   1.000000
2020-09  14.461538  -4.898123  -2.773585 -10.151079   2.200000
2020-10  12.912226  -4.263158  -3.631829  -4.220884   8.198444
2020-11  45.728435  27.969697  15.146341  -0.568627  11.826772
2020-12  43.218798  44.304843  20.975490  10.716599  13.252964
2021-01  28.170868  49.245614  23.166874  20.491525   5.900181>

but when I run this with the data below, it gets error message

KOSPI = fdr.StockListing('kospi')
test=get_data(KOSPI, '2020', 3)
test

<bound method NDFrame.to_clipboard of       Symbol Market  ...                       HomePage Region
1     095570  KOSPI  ...         http://www.ajnet.co.kr  ?????
2     006840  KOSPI  ...  http://www.aekyunggroup.co.kr  ?????
6     027410  KOSPI  ...           http://www.bgf.co.kr  ?????
7     282330  KOSPI  ...       http://www.bgfretail.com  ?????
8     138930  KOSPI  ...           http://www.bnkfg.com  ?????
...      ...    ...  ...                            ...    ...
2604  069260  KOSPI  ...         http://www.huchems.com  ?????
2608  000540  KOSPI  ...     http://www.insurance.co.kr  ?????
2609  000547  KOSPI  ...                            NaN    NaN
2610  000545  KOSPI  ...                            NaN    NaN
2611  003280  KOSPI  ...         http://www.heung-a.com  ?????

[1000 rows x 10 columns]>
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-49-16e5fb0f2994> in <module>()
     12   return temp
     13 
---> 14 test=get_data(KOSPI, '2020', 3)
     15 test

<ipython-input-49-16e5fb0f2994> in get_data(df, base_date, period)
      4         #get data and return
      5     df = fdr.DataReader(i, base_date)
----> 6     df.index = df.index.strftime('%Y-%m')
      7     df = df.groupby(df.index).tail(1)
      8     df['rtn'] = df['Close'].pct_change(period) * 100 +1

AttributeError: 'Index' object has no attribute 'strftime'

this error message occurs. I don't understand the difference between the sample data and KOSPI data. Is there a difference? how do i have to edit the KOSPI data?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...