Index 0 Is Out of Bounds for Axis 0 With Size 0

IndexError: index 0 is out of bounds for axis 0 with size 0 for trying to find mode (most frequent value)

I think your problem is one or more of your groupby is returning all NaN heights:

See this example, where I added a number 4 with np.NaN as its heights.

df_test = pd.DataFrame({"number": [1,1,1,1,2,2,2,2,3,3,3,3,4,4], 
'temperature': [2,3,4,5,4,3,4,5,5, 3, 4, 4, 5, 5],
'height': [100, 100, 0, 100, 100, 90, 90, 100, 100, 90, 80, 80, np.nan, np.nan]})

df_test.groupby('number')['temperature'].first()

df_test.groupby('number')['height'].agg(lambda x: x.value_counts().index[0])

Output:

IndexError: index 0 is out of bounds for axis 0 with size 0

Let's fill those NaN with zero and rerun.

df_test = pd.DataFrame({"number": [1,1,1,1,2,2,2,2,3,3,3,3,4,4], 
'temperature': [2,3,4,5,4,3,4,5,5, 3, 4, 4, 5, 5],
'height': [100, 100, 0, 100, 100, 90, 90, 100, 100, 90, 80, 80, np.nan, np.nan]})

df_test = df_test.fillna(0) #Add this line
df_test.groupby('number')['temperature'].first()

df_test.groupby('number')['height'].agg(lambda x: x.value_counts().index[0])

Output:

number
1 100.0
2 90.0
3 80.0
4 0.0
Name: height, dtype: float64

Handing error "index 0 is out of bounds for axis 0 with size 0" within a function

I would do something like this:

def getcon(name, Grade):
con = df1.loc[(df1['name'] == name) & (df1['Grade'] == Grade), 'Country']

return con.iloc[0] if len(con) else None

What does 'index 0 is out of bounds for axis 0 with size 0' mean and how can I fix this error?

pred_price is an empty np.array so trying to index it like you have done on line 9 ("pred_price[0]") will give an error. There has to be something in the array first.

index 0 is out of bounds for axis 0 with size 0

My guess is that the array field_in_k_space_REAL is actually of length 0, most likely because you set n = 0 further up in your code (do you use n in a loop maybe?). I can reproduce the error when I directly initialize an array of length 0.

Python Index Error - Out of Bounds for axis 0

As you have tagged the question with pandas, here is one way of approaching the problem with str.get_dummies method of the pandas Series:

df = pd.read_csv('datausers.txt', sep='\s+', names=['userid', 'locationid'], index_col=0)
out = df['locationid'].astype(str).str.get_dummies().sum(level=0)

Result

For the sample data

>>> out
10201 10259 14470 15810 19264 32332 33847 34041 34827 34834 35407 36115
userid
801 0 0 1 0 0 1 1 0 0 0 0 0
501 1 1 0 0 0 0 0 1 0 0 0 0
301 0 0 0 1 1 0 0 0 1 0 0 0
401 0 0 0 0 0 0 0 0 0 1 1 1

If you need numpy array instead:

>>> out.to_numpy()

array([[0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1]])

Why I am getting index 0 is out of bounds for axis 0 with size 0?

this fixed the problem

def get_voltageStatus(r,t):
all=[]
for i in range (1,len(data[0]),3):
m=np.where((data[1:,i]>=r) & (data[1:,i]<=t))
print(i)
mm_raws = []
mm=m[0]
if mm.any():
start=mm[0]
end=mm[-1]
print(data[0,i])
temp=duration(start,end)
all.append([data[0,i],temp])


Related Topics



Leave a reply



Submit