I came across the following errors when I ran the codes for performing a loop operation. Would somebody kindly able to point out the mistake I made so I may fix it?
The aim of the codes is trying to see if the next row of "Dividend" equals to zero or not and if not equal to zero, then the next row of "Adjusting Factor" will perform the action on the right hand side of the equation. I really don't know how I should fix it. Please give some help, thank you so much.
for i in range(data.shape[0]):
if i == (data.shape[0]-1):
continue
data.loc[data['Dividend'].iloc[i+1] != 0, data['Adjusting Factor'].iloc[i+1]] = (data['EQIX US EQUITY'].iloc[i] - data['Dividend'].iloc[i])
data['Adjusted Premium'].iloc[i] = data['Put'].iloc[i] * data['Adjusting Factor']
KeyError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2896 try:
-> 2897 return self._engine.get_loc(key)
2898 except KeyError:
pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
KeyError: 1
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\pandas\core\indexes\datetimes.py in get_loc(self, key, method, tolerance)
1056 try:
-> 1057 return Index.get_loc(self, key, method, tolerance)
1058 except (KeyError, ValueError, TypeError):
~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
2898 except KeyError:
-> 2899 return self._engine.get_loc(self._maybe_cast_indexer(key))
2900 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.DatetimeEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
KeyError: 1
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-49-0fba1ee2e5e8> in <module>
12 if i == (data.shape[0]-1): # skip the last run to avoid error occur
13 continue
---> 14 data.loc[data['Dividend'].iloc[i+1] != 0, data['Adjusting Factor'].iloc[i+1]] = (data['EQIX US EQUITY'].iloc[i] - data['Dividend'].iloc[i]) / data['EQIX US EQUITY'].iloc[i]
15 data['Adjusted Premium'].iloc[i] = data['Put'].iloc[i] * data['Adjusting Factor']
16 data.loc[data['Adjust Factor'].iloc[i] !=data['Adjust Factor'].iloc[i-1], 'Adjusted Contract Multiplier'] = (data['Adjusted Contract Multiplier'].iloc[i-1]) / data['Adjusting Factor'].iloc[i]
~\Anaconda3\lib\site-packages\pandas\core\indexing.py in __setitem__(self, key, value)
202 else:
203 key = com.apply_if_callable(key, self.obj)
--> 204 indexer = self._get_setitem_indexer(key)
205 self._setitem_with_indexer(indexer, value)
206
~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _get_setitem_indexer(self, key)
180 if isinstance(key, tuple):
181 try:
--> 182 return self._convert_tuple(key, is_setter=True)
183 except IndexingError:
184 pass
~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _convert_tuple(self, key, is_setter)
260 if i >= self.obj.ndim:
261 raise IndexingError("Too many indexers")
--> 262 idx = self._convert_to_indexer(k, axis=i, is_setter=is_setter)
263 keyidx.append(idx)
264 return tuple(keyidx)
~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _convert_to_indexer(self, obj, axis, is_setter, raise_missing)
1286 else:
1287 try:
-> 1288 return labels.get_loc(obj)
1289 except LookupError:
1290 # allow a not found key only if we are a setter
~\Anaconda3\lib\site-packages\pandas\core\indexes\datetimes.py in get_loc(self, key, method, tolerance)
1063
1064 try:
-> 1065 stamp = Timestamp(key)
1066 if stamp.tzinfo is not None and self.tz is not None:
1067 stamp = stamp.tz_convert(self.tz)
pandas/_libs/tslibs/timestamps.pyx in pandas._libs.tslibs.timestamps.Timestamp.__new__()
pandas/_libs/tslibs/conversion.pyx in pandas._libs.tslibs.conversion.convert_to_tsobject()
TypeError: Cannot convert input [True] of type <class 'numpy.bool_'> to Timestamp
