1 Replies - 281 Views - Last Post: 14 March 2019 - 07:32 AM Rate Topic: -----

#1 Newport_j   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-February 19

Getting Error in Dayofweek code

Posted 14 March 2019 - 06:38 AM

I started a thread in the forum that had to do with using fast.ai. But, I could never get fast.ai installed since that would require a software upgrade (Python 3.5.2 to Python 6) that I am reluctant to do. Fortunately,in the article that I was using there was a workaround.

I tried that and got the following error as shown in code box

#setting index as data values
df['Date'] = pd.to_datetime(df.Date,format='%Y-%m-%d')
df.index = df['Date']

#sorting
data = df.sort_index(ascending=True, axis = 0)

#creating a separate dataset
new_data = pd.DataFrame(index=range(0,len(df)),columns=['Date','Close'])

for i in range(len(data)):
    new_data['Date'][i] = data['Date'][i]
    new_data['Close'][i] = data['Close'][i]
    
#create features
#from fastai.structured import add_datepart

new_data['mon_fri'] = 0
for i in range(0,len(new_data)):
    if (new_data['Dayofweek'][i] == 0 or new_data['Dayofweek'][i] == 4):
        new_data['mon_fri'][i] = 1  
    else:
        new_data['mon_fri'][i] = 0
#add_datepart(new_data, 'Date')
#new_data.drop('Elapsed', axis=1, inplace=True)  #elapsed will be the time stamp


--------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~/.local/lib/python3.5/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2601             try:
-> 2602                 return self._engine.get_loc(key)
   2603             except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Dayofweek'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-29-8a2a82cb9b28> in <module>
     18 new_data['mon_fri'] = 0
     19 for i in range(0,len(new_data)):
---> 20     if (new_data['Dayofweek'][i] == 0 or new_data['Dayofweek'][i] == 4):
     21         new_data['mon_fri'][i] = 1
     22     else:

~/.local/lib/python3.5/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2915             if self.columns.nlevels > 1:
   2916                 return self._getitem_multilevel(key)
-> 2917             indexer = self.columns.get_loc(key)
   2918             if is_integer(indexer):
   2919                 indexer = [indexer]

~/.local/lib/python3.5/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2602                 return self._engine.get_loc(key)
   2603             except KeyError:
-> 2604                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2605         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2606         if indexer.ndim > 1 or indexer.size > 1:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Dayofweek'




The attached pdf file shows the work around as described in the paper.

So I guess what I need is the work around to the work around. I simply cannot use:

 for i in range(0,len(new_data)):
    if (new_data['Dayofweek'][i] == 0 or new_data['Dayofweek'][i] == 4):
        new_data['mon_fri'][i] = 1  
    else:
        new_data['mon_fri'][i] = 0



That code also creates problems.

Any help appreciated. Thanks in advance.

Respectfully,


Newport_j

Is This A Good Question/Topic? 0
  • +

Replies To: Getting Error in Dayofweek code

#2 andrewsw   User is offline

  • quantum multiprover
  • member icon

Reputation: 6775
  • View blog
  • Posts: 27,938
  • Joined: 12-December 12

Re: Getting Error in Dayofweek code

Posted 14 March 2019 - 07:32 AM

it says Key Error: Dayofweek and points to this line:

    if (new_data['Dayofweek'][i] == 0 or new_data['Dayofweek'][i] == 4):

If that key does not exist then you cannot look into it to find [i].

Either first check if the key exists, or check why it does not exist if it should.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1