[英]Too many indices for array | python
I'm trying to copy arrays to a pandas Dataframe and get the error "too many indices for array".我正在尝试将 arrays 复制到 pandas Dataframe 并得到错误“数组索引过多”。
temp = pd.date_range(date_from, date_to)[:len(pr_daily)]
for index in range(len(a_id)):
if index == 0:
finalDataframe['date'] = temp
finalDataframe[f'pr_{a_id[index]}'] = pr_daily[:, index]
finalDataframe[f'gloabl_irradiance_tilted_in_kWh_per_m2_{a_id[index]}'] = rad_daily[:, index]
finalDataframe[f'system_id_{a_id[index]}'] = a_id[index]
The error occurs in these lines:错误发生在这些行中:
finalDataframe[f'pr_{a_id[index]}'] = pr_daily[:, index]
finalDataframe[f'gloabl_irradiance_tilted_in_kWh_per_m2_{a_id[index]}'] = rad_daily[:, index]
pr_daily and rad_daily are numpy arrays of of the same length. pr_daily 和 rad_daily 是相同长度的 numpy arrays。
Traceback (most recent call last):回溯(最近一次通话最后):
File "C:\Users...\Downloads\python_scripts\pv.py", line 277, in finalDataframe[f'pr_{a_id[index]}'] = (pr_daily[:,index])文件“C:\Users...\Downloads\python_scripts\pv.py”,第 277 行,在 finalDataframe[f'pr_{a_id[index]}'] = (pr_daily[:,index])
IndexError: too many indices for array IndexError:数组的索引过多
This error is thrown when you try to access an array element by providing too much indices.当您尝试通过提供过多索引来访问数组元素时会引发此错误。
eg You try to access the second dimension of a 1-dimension array.例如,您尝试访问一维数组的第二维。
Check the shape of pr_daily
and rad_daily
if they indeed are 2D arrays.检查pr_daily
和rad_daily
的形状,如果它们确实是 2D arrays。
a = np.random.rand(5,)
b = np.random.rand(5,5)
print(f'b[:,1] :: {b[:,1]}') --> OK
print(f'{a[:,1]}') --> IndexError: too many indices for array
You can access a numpy array shape with the shape attribute您可以使用shape 属性访问 numpy 数组形状
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.