Why am I getting this error - KeyError: ('datetime64[ns]', 'left')? - python-3.x

When I run the code below this error is displayed:
KeyError: ('datetime64[ns]', 'left')
Anyone have a answer ? Please find the code below:
def transform_in_intervals(df,freq=to_offset('5t')):
df = df.copy()
time = df.index
interval = pd.interval_range(start=time[0],periods=(time[-1]-time[0])/pd.Timedelta(freq)+1,freq=freq,closed="left")
df.reset_index(False,inplace=True)
i, c = 0, 0
while i < len(df):
if df.loc[i,"time"] in interval[c]:
df.loc[i,"interval"] = interval[c]
i += 1
else:
c += 1
df_res = pd.DataFrame([])
for iv, df_left in df.groupby("interval"):
df_res = df_res.append(df_left.drop("time",axis=1).mean(),ignore_index=True)
df_res.set_index(interval,inplace=True)
return df_res
start= time.time()
trans_df = transform_in_intervals(merged_df)
end = time.time()
print("time:{:.3f} s".format(end-start))
KeyError Traceback (most recent call last)
<ipython-input-31-bfcffbdad696> in <module>()
1 start= time.time()
----> 2 trans_df = transform_in_intervals(merged_df)
3 end = time.time()
4 print("time:{:.3f} s".format(end-start))
<ipython-input-30-2d97dbfdb4d1> in transform_in_intervals(df, freq)
14 for iv, df_left in df.groupby("interval"):
15 df_res = df_res.append(df_left.drop("time",axis=1).mean(),ignore_index=True)
---> 16 df_res.set_index(interval,inplace=True)
17 return df_res
~\AppData\Local\Continuum\anaconda2\envs\py35\lib\site-packages\pandas\core\frame.py in set_index(self, keys, drop, append, inplace, verify_integrity)
3921
3922 # clear up memory usage
-> 3923 index._cleanup()
3924
3925 frame.index = index
~\AppData\Local\Continuum\anaconda2\envs\py35\lib\site-packages\pandas\core\indexes\base.py in _cleanup(self)
1912
1913 def _cleanup(self):
-> 1914 self._engine.clear_mapping()
1915
1916 #cache_readonly
pandas\_libs\properties.pyx in pandas._libs.properties.CachedProperty.__get__()
~\AppData\Local\Continuum\anaconda2\envs\py35\lib\site-packages\pandas\core\indexes\interval.py in _engine(self)
366 #cache_readonly
367 def _engine(self):
--> 368 return IntervalTree(self.left, self.right, closed=self.closed)
369
370 #property
pandas\_libs\intervaltree.pxi in pandas._libs.interval.IntervalTree.__init__()
KeyError: ('datetime64[ns]', 'left')

Related

cannot set a frame with no defined index and a scalar using "Loc" function

I'm using python 3
i was calculating RSI strategy but getting a "cannot set a frame with no defined index and a scalar"error
with data :
ticker_list
def RSIcalc(ticker):
df = yf.download(ticker, start='2012-01-01')
df['MA200'] = df['Adj Close'].rolling(window=200).mean()
df['price change'] = df['Adj Close'].pct_change()
df['Upmove'] = df['price change'].apply(lambda x: x if x>0 else 0)
df['Downmove'] = df['price change'].apply(lambda x: abs(x) if x<0 else 0)
df['avg Up'] = df['Upmove'].ewm(span=90).mean()
df['avg Down'] = df['Downmove'].ewm(span=90).mean()
df = df.dropna()
df['RS'] = df['avg Up']/df['avg Down']
df['RSI'] = df['RS'].apply(lambda x:100-(100/(x+1)))
df.loc[(df['Adj Close']>df['MA200']) &(df['RSI']<30),'Buy'] = 'Yes'
df.loc[(df['Adj Close']<df['MA200']) | (df['RSI']>30),'Buy'] = 'No'
return df
def getSignals(df):
Buying_dates = []
Selling_dates = []
for i in range(len(df)):
if "Yes" in df['Buy'].iloc[i]:
Buying_dates.append(df.iloc[i+1].name)
print(1)
for j in range(1,11):
if df['RSI'].iloc[i+j]>40:
Selling_dates.append(df.iloc[i+j+1].name)
break
print(0)
elif j==10:
Selling_dates.append(df.iloc[i+j+1].name)
return Buying_dates,Selling_dates
after running these two cells
for tick in ticker_list['Symbol']:
frame = RSIcalc(tick)
buy, sell = getSignals(frame)
I'm getting a valueerror
ValueError Traceback (most recent call last)
Input In [43], in <cell line: 1>()
1 for tick in ticker_list['Symbol']:
----> 2 frame = RSIcalc(tick)
3 buy, sell = getSignals(frame)
Input In [14], in RSIcalc(ticker)
10 df['RS'] = df['avg Up']/df['avg Down']
11 df['RSI'] = df['RS'].apply(lambda x:100-(100/(x+1)))
---> 12 df.loc[(df['Adj Close']>df['MA200']) &(df['RSI']<30),'Buy'] = 'Yes'
13 df.loc[(df['Adj Close']<df['MA200']) | (df['RSI']>30),'Buy'] = 'No'
14 return df
File ~\Anaconda3\lib\site-packages\pandas\core\indexing.py:716, in _LocationIndexer.__setitem__(self, key, value)
713 self._has_valid_setitem_indexer(key)
715 iloc = self if self.name == "iloc" else self.obj.iloc
--> 716 iloc._setitem_with_indexer(indexer, value, self.name)
File ~\Anaconda3\lib\site-packages\pandas\core\indexing.py:1615, in _iLocIndexer._setitem_with_indexer(self, indexer, value, name)
1613 if not len(self.obj):
1614 if not is_list_like_indexer(value):
-> 1615 raise ValueError(
1616 "cannot set a frame with no "
1617 "defined index and a scalar"
1618 )
1619 self.obj[key] = value
1620 return
ValueError: cannot set a frame with no defined index and a scalar
How do i fix this
have set "pd.options.mode.chained_assignment = None" but no luck

TypeError and InvalidIndexError - Pandas/ Jupyter Notebook

I'm having problems with this pandas code, apparently it's related to index , but I haven't been able to solve it yet. When I removed the columns = cols I managed to run it but it was blank, can anyone help me?
`
def SimulaCopa(dados):
cols = ['1st', '2nd', '3rd', '4th', 'Oitavas', 'Quartas', 'Semis', 'Final', 'Campeão']
n = dados.shape[0]
m = len(cols)
aux = np.array(np.zeros(n*m).reshape(n, m))
info = pd.DataFrame(aux,columns=cols,index= dados.index)
info = info.astype(int)
top16 = []
for i in list('ABCDEFGH'):
a = JogosGrupo(dados, i)[0]
top16 += a.index[:2].tolist()
anomes = a.index.to_list()
info.at[anomes[0], '1st'] = 1
info.at[anomes[1], '2nd'] = 1
info.at[anomes[2], '3rd'] = 1
info.at[anomes[3], '4th'] = 1
qf1 = JogoMataMata(top16[0], top16[3]) #A1 x B2
qf2 = JogoMataMata(top16[2], top16[1]) #B1 x A2
qf3 = JogoMataMata(top16[4], top16[7]) #C1 x D2
qf4 = JogoMataMata(top16[6], top16[5]) #D1 x C2
qf5 = JogoMataMata(top16[8], top16[11]) #E1 x F2
qf6 = JogoMataMata(top16[10], top16[9]) #F1 x E2
qf7 = JogoMataMata(top16[12], top16[15]) #G1 x H2
qf8 = JogoMataMata(top16[14], top16[13]) #H1 x G2
top8 = [qf1, qf2, qf3, qf4, qf5, qf6, qf7, qf8]
sf1 = JogoMataMata(qf1, qf3)
sf2 = JogoMataMata(qf2, qf4)
sf3 = JogoMataMata(qf5, qf7)
sf4 = JogoMataMata(qf6, qf8)
top4 = [sf1, sf2, sf3, sf4]
f1 = JogoMataMata(sf1, sf3)
f2 = JogoMataMata(sf2, sf4)
top2 = [f1, f2]
top1 = JogoMataMata(f1, f2)
info.at[top16, 'Oitavas'] = 1
info.at[top8, 'Quartas'] = 1
info.at[top4, 'Semis'] = 1
info.at[top2, 'Final'] = 1
info.at[top1, 'Campeão'] = 1
return info
`
[[enter image description here](https://i.stack.imgur.com/QM15u.jpg)](https://i.stack.imgur.com/uMBVJ.jpg)
I removed columns=cols , I used the prints in the steps to try to identify an error and it didn't work.
Message error complete:
TypeError Traceback (most recent call last)
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexes\base.py:3800, in Index.get_loc(self, key, method, tolerance)
3799 try:
-> 3800 return self._engine.get_loc(casted_key)
3801 except KeyError as err:
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\_libs\index.pyx:138, in pandas._libs.index.IndexEngine.get_loc()
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\_libs\index.pyx:144, in pandas._libs.index.IndexEngine.get_loc()
TypeError: '['Holanda', 'Senegal', 'Inglaterra', 'Irã', 'Argentina', 'Arábia Saudita', 'França', 'Tunísia', 'Costa Rica', 'Alemanha', 'Bélgica', 'Marrocos', 'Suíça', 'Brasil', 'Portugal', 'Uruguai']' is an invalid key
During handling of the above exception, another exception occurred:
InvalidIndexError Traceback (most recent call last)
Cell In [216], line 1
----> 1 SimulaCopa(selecoes)
Cell In [214], line 45, in SimulaCopa(dados)
41 top2 = [f1, f2]
43 top1 = JogoMataMata(f1, f2)
---> 45 info.at[top16, 'Oitavas'] = 1
46 info.at[top8, 'Quartas'] = 1
47 info.at[top4, 'Semis'] = 1
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexing.py:2438, in _AtIndexer.__setitem__(self, key, value)
2435 self.obj.loc[key] = value
2436 return
-> 2438 return super().__setitem__(key, value)
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexing.py:2393, in _ScalarAccessIndexer.__setitem__(self, key, value)
2390 if len(key) != self.ndim:
2391 raise ValueError("Not enough indexers for scalar access (setting)!")
-> 2393 self.obj._set_value(*key, value=value, takeable=self._takeable)
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\frame.py:4209, in DataFrame._set_value(self, index, col, value, takeable)
4207 else:
4208 icol = self.columns.get_loc(col)
-> 4209 iindex = self.index.get_loc(index)
4210 self._mgr.column_setitem(icol, iindex, value)
4211 self._clear_item_cache()
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexes\base.py:3807, in Index.get_loc(self, key, method, tolerance)
3802 raise KeyError(key) from err
3803 except TypeError:
3804 # If we have a listlike key, _check_indexing_error will raise
3805 # InvalidIndexError. Otherwise we fall through and re-raise
3806 # the TypeError.
-> 3807 self._check_indexing_error(key)
3808 raise
3810 # GH#42269
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexes\base.py:5963, in Index._check_indexing_error(self, key)
5959 def _check_indexing_error(self, key):
5960 if not is_scalar(key):
5961 # if key is not a scalar, directly raise an error (the code below
5962 # would convert to numpy arrays and raise later any way) - GH29926
-> 5963 raise InvalidIndexError(key)
InvalidIndexError: ['Holanda', 'Senegal', 'Inglaterra', 'Irã', 'Argentina', 'Arábia Saudita', 'França', 'Tunísia', 'Costa Rica', 'Alemanha', 'Bélgica', 'Marrocos', 'Suíça', 'Brasil', 'Portugal', 'Uruguai']

Cannot cast ufunc subtract output from dtype('float64') to dtype('int64') with casting rule 'same_kind' despite forced convertion

I have a data Series ts:
0 2599.0
1 2599.0
2 3998.0
3 3998.0
4 1299.0
5 1499.0
6 1499.0
7 2997.5
8 749.5
Name: 0, dtype: float64
and I would like to predict the next period using ARIMA:
import statsmodels.tsa.api as smt
array = []
for i, row in test.iterrows():
print("row['shop_id']: ", row['shop_id'], " row['item_id']: ", row['item_id'])
ts = pd.DataFrame(sales_monthly.loc[pd.IndexSlice[:, [row['shop_id']],[row['item_id']]], :]['item_price'].values*sales_monthly.loc[pd.IndexSlice[:, [row['shop_id']],[row['item_id']]], :]['item_cnt_day'].values).T.iloc[0]
rng = range(5)
for i in rng:
for j in rng:
try:
tmp_mdl = smt.ARMA(ts, order = (i, j)).fit(method='mle', trand='nc')
tmp_aic = tmp_mdl.aic
if tmp_aic < best_aic:
best_aic = tmp_aic
best_order = (i, j)
best_mdl = tmp_mdl
except:
continue
if best_mdl.predict()<0:
y_pred = 0
else:
y_pred = best_mdl.predict()
d = {'id':row['ID'], 'item_cnt_month': y_pred}
array.append(d)
df = pd.DataFrame(array)
df
But I get:
---------------------------------------------------------------------------
UFuncTypeError Traceback (most recent call last)
<ipython-input-104-85dfa2fa67c1> in <module>()
22 except:
23 continue
---> 24 if best_mdl.predict()<0:
25 y_pred = 0
26 else:
3 frames
/usr/local/lib/python3.6/dist-packages/statsmodels/tsa/arima_model.py in geterrors(self, params)
686 k = self.k_exog + self.k_trend
687 if k > 0:
--> 688 y -= dot(self.exog, params[:k])
689
690 k_ar = self.k_ar
UFuncTypeError: Cannot cast ufunc 'subtract' output from dtype('float64') to dtype('int64') with casting rule 'same_kind'
So I used best_mdl.predict().astype('float32') but it didn't changed anything.

Using apply function Dataframe

I need help correcting an error I am getting.
I have the following dataframe:
x = [-0.75853, -0.75853, -0.75853, -0.75852]
y = [-0.63435, -0.63434, -0.63435, -0.63436]
z = [-0.10488, -0.10490, -0.10492, -0.10495]
w = [-0.10597, -0.10597, -0.10597, -0.10596]
df = pd.DataFrame([x, y, z, w], columns=['x', 'y', 'z', 'w'])
I created the following functions:
import math
def roll(qw, qx, qy, qz):
# x-axis rotation
sinr_cosp = +2.0 * (qw * qx + qy + qz)
cosr_cosp = +1.0 - 2.0 * (qx * qx + qy * qy)
roll = math.atan2(sinr_cosp, cosr_cosp)
return roll
def pitch(qw, qx, qy, qz):
# y-axis rotation
sinp = +2.0 * (qw * qy - qz * qx)
if(math.fabs(sinp) >= 1):
pitch = copysign(M_PI/2, sinp)
else:
pitch = math.asin(sinp)
return sinp
def yaw(qw, qx, qy, qz):
# z-axis rotation
siny_cosp = +2.0 * (qw * qz + qx * qy)
cosy_cosp = +1.0 - 2.0 * (qy * qy + qz * qz)
yaw = math.atan2(siny_cosp, cosy_cosp)
return yaw
Finally, using Pandas apply function, I tried to associate the result with a new column:
q_w = df['w']
q_x = df['x']
q_y = df['y']
q_z = df['z']
df['row'] = df.apply(roll(q_w, q_x, q_y, q_z))
The same error occurs when using the other functions.
I saw an issue right here on Stack where this bug was fixed using Numpy. I believe this is not possible here because I am using functions specific to the Math package.
TypeError Traceback (most recent call
last) /usr/local/lib/python3.6/dist-packages/pandas/core/series.py in
wrapper(self)
92 raise TypeError("cannot convert the series to "
---> 93 "{0}".format(str(converter)))
94
TypeError: cannot convert the series to
The above exception was the direct cause of the following exception:
SystemError Traceback (most recent call
last) 4 frames in ()
----> 1 df['row'] = df.apply(roll(q_w, q_x, q_y, q_z))
in roll(qw, qx, qy, qz)
4 sinr_cosp = +2.0 * (qw * qx + qy + qz)
5 cosr_cosp = +1.0 - 2.0 * (qx * qx + qy * qy)
----> 6 roll = math.atan2(sinr_cosp, cosr_cosp)
7 return roll
8
/usr/local/lib/python3.6/dist-packages/pandas/core/series.py in
wrapper(self)
88
89 def wrapper(self):
---> 90 if len(self) == 1:
91 return converter(self.iloc[0])
92 raise TypeError("cannot convert the series to "
/usr/local/lib/python3.6/dist-packages/pandas/core/series.py in
len(self)
593 Return the length of the Series.
594 """
--> 595 return len(self._data)
596
597 def view(self, dtype=None):
/usr/local/lib/python3.6/dist-packages/pandas/core/internals/managers.py
in len(self)
290
291 def len(self):
--> 292 return len(self.items)
293
294 def unicode(self):
SystemError: PyEval_EvalFrameEx returned a result with an error set
You should using apply like
df.apply(lambda x : roll(x['w'],x['x'],x['y'],x['z']),1)
Out[291]:
0 -2.175472
1 -1.909103
2 -0.394163
3 -0.397885
dtype: float64
You could also modify your function.
def roll(df):
# x-axis rotation
sinr_cosp = +2.0 * (df.w * df.x + df.y + df.z)
cosr_cosp = +1.0 - 2.0 * (df.x * df.x + df.y * df.y)
roll = math.atan2(sinr_cosp, cosr_cosp)
return roll
df.apply(roll, axis=1)
Out:
0 -2.175472
1 -1.909103
2 -0.394163
3 -0.397885
dtype: float64

How do i call out variable from a function in another function?-Python3

def run():
rundecision=input("What do you want to do? calculate distance(d),pace(p) time(t):")
if rundecision in ['distance', 'd']:
pace()
time()
distance=calculator(distance=None,pace=pacetotal,time=timetotal)
return str(distance) + paceunit
print (run())
my pace() is below where pace total is defined and called out above.
def pace():
while True:
pacemin=input("Enter what pace you want to run/ you ran in :00(min):")#user pace in min
pacesec=input("Enter what pace you want to run/ you ran in :00(secs):")#user pace in sec
try:
pacemin=int(pacemin)
pacesec=int(pacesec)
if 0 <= pacemin <= 59 and 0 <= pacesec <=59:
pacetotal=(to_seconds(pacemin,'min')) + (to_seconds(pacesec,'s'))
pacetotal=int(pacetotal)
return pacetotal
break
)
This is my error:
Traceback (most recent call last): File "minicapstonev2.py", line
188, in
print (run()) File "minicapstonev2.py", line 185, in run
distance=calculator(distance=None,pace=pacetotal,time=timetotal)
NameError: name 'pacetotal' is not defined
You have to assign the return values of your functions to a variable that u can use:
if rundecision in ['distance', 'd']:
pacetotal = pace() # pace returns the seconds
timetotal = time() # this should also return the value for your next computation
distance=calculator(distance=None,pace=pacetotal,time=timetotal)
return str(distance) + paceunit
You did not supply the time() function, but if it is similar to pace this should work.
Edit due to question in comments:
Some variations on how to approach returnvalues and your program:
# Different ways to return something from a function
def ModifyGivenDict(di , li):
# do some calculations
di["milesPerHour"] = 42
li.append(999)
def ReturnAValue(x):
return x ** x
def ReturnMultipleValues(x):
return [x * u for u in range(20)]
def ReturnTuple(x,y):
return (x,y,x * y,x ** y,y ** x,"someValue")
d = {"Round1": 496 }
l = [2,3,4,5,"Hurray"]
a = ModifyGivenDict(d,l)
print(d)
k = ReturnAValue(22)
print(k)
i = ReturnMultipleValues(22)
print(i)
h = ReturnTuple(4,7)
print(h)
# OOP aproach
class Timings:
#staticmethod
def toSeconds(text):
"""Understands up to '12:18:24.3545' - returns a floatvalue of seconds"""
t = [0,0] + text.split(":") # prefix with 2 * 0 if only "22" or "1:22" is given
t[-1] = float(t[-1]) # make last elements to float
t[-2] = int(t[-2]) * 60 # make integer minutes into seconds
t[-3] = int(t[-3]) * 60 * 60 # make integer hours into seconds
return sum(t)
#staticmethod
def toMeters(distance):
"""Understands '255.23 meters'"""
converterDict = {'mile':1609.34, 'mi':1609.34, 'km':1000, 'kilometer':1000,
'y':0.9144, 'yard':0.9144, 'meter':1, 'm':1}
dist,unit = distance.split(" ")
dist = float(dist)
unit = unit.rstrip("s").strip().lower()
return dist * converterDict[unit]
def __init__(self, name):
self.name = name
self.laps = {}
self.lap = 0
def addLap(self, minutesColonSeconds, distance):
t = self.toSeconds(minutesColonSeconds)
m = self.toMeters(distance)
self.laps[self.lap] = {"time":t, "distance":m}
self.lap += 1
def printLaps(self):
print("Results for " + self.name,sep="\t")
print("{:<14} {:<14} {:<14} {:<14} {:<14} {:<14} {:<14}".format(
"lap","m","s","m/s", "total time", "total dist","speed"))
tm = 0
tt = 0
# you could also use an orderedDict from collections
for k in sorted(self.laps.keys()):
m = self.laps[k]["distance"]
t = self.laps[k]["time"]
tm +=m
tt += t
print("{:<14} {:<14} {:<14} {:<14} {:<14} {:<14} {:<14}".format(
k,m,t,round(m / t,2), tt,tm,round(tm/tt,2)))
def inputTime(text):
while True:
i = input(text)
try:
t = [0,0] + i.split(":")
sec = float(t[-1])
min = int(t[-2])
hou = int(t[-3])
if sec+min*60+hou*60*60 <= 0:
raise ValueError
return i
except (ValueError,EOFError):
print("Wrong input! Use '1:12:23.99' for hour:minute:second.partials")
def inputDistance(text):
while True:
t = input(text)
try:
dis,un = t.split()
dis = float(dis)
return t
except:
print("Wrong input. Use: '23 km' - or: meter, mile(s), yard(s), m, mi, y")
print("\nClassaproach\n\n")
timing = Timings("Just Me")
while True:
dis = inputDistance("What distance did you cover?")
tim = inputTime("In what time?")
timing.addLap(tim,dis)
timing.printLaps()
Output (edited to better fit here):
{'Round1': 496, 'milesPerHour': 42}
341427877364219557396646723584
[0, 22, 44, 66, 88, 110, 132, 154, 176, 198, 220, 242, 264, 286,
308, 330, 352, 374, 396, 418]
(4, 7, 28, 16384, 2401, 'someValue')
Classaproach
What distance did you cover?100 m
In what time?12.02
Results for Just Me
lap m s m/s total time total dist speed
0 100.0 12.02 8.32 12.02 100.0 8.32
What distance did you cover?20 km
In what time?2:0:01
Results for Just Me
lap m s m/s total time total dist speed
0 100.0 12.02 8.32 12.02 100.0 8.32
1 20000.0 7201.0 2.78 7213.02 20100.0 2.79
What distance did you cover?5 mi
In what time?1:1:1
Results for Just Me
lap m s m/s total time total dist speed
0 100.0 12.02 8.32 12.02 100.0 8.32
1 20000.0 7201.0 2.78 7213.02 20100.0 2.79
2 8046.7 3661.0 2.2 10874.02 28146.7 2.59
What distance did you cover?120 km
In what time?1:02:00
Results for Just Me
lap m s m/s total time total dist speed
0 100.0 12.02 8.32 12.02 100.0 8.32
1 20000.0 7201.0 2.78 7213.02 20100.0 2.79
2 8046.7 3661.0 2.2 10874.02 28146.7 2.59
3 120000.0 3720.0 32.26 14594.02 148146.7 10.15
What distance did you cover?
...

Resources