256 Kilobytes

Answers in Scripting and Automation | By Some Guy

Published

664 views, 0 RAMs, and 1 comment

Tags: Python, Scripting, Errors, Programming, TypeError

Profile Photo - Some GuySome GuyBasic Subscriber

Hi. I've been working in Python a bit, and got this error about what I was working converting to a scalar index. It keeps giving me a TypeError that only integer scalar arrays can be converted to the type of index I want. Can someone help make this work for me? I've been stuck on this for awhile.

Download more RAM. 🐏 ⨉ 0Posted by Some Guy 1 year ago 🕓 Posted at 13 January, 2019 05:54 AM PST

This is a debug account.

Profile Photo - UnivaUnivaAuthor

A simple case that generates this error message:

In [8]: [1,2,3,4,5][np.array([1])]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-55def8e1923d> in <module>()
----> 1 [1,2,3,4,5][np.array([1])]
TypeError: only integer scalar arrays can be converted to a scalar index

Some variations that work:

In [9]: [1,2,3,4,5][np.array(1)]     # this is a 0d array index
Out[9]: 2
In [10]: [1,2,3,4,5][np.array([1]).item()]    
Out[10]: 2
In [11]: np.array([1,2,3,4,5])[np.array([1])]
Out[11]: array([2])

Basic python list indexing is more restrictive than numpy's:

In [12]: [1,2,3,4,5][[1]]
....
TypeError: list indices must be integers or slices, not list

Download more RAM. 🐏 ⨉ 0Posted by Univa 1 year ago 🕓 Posted at 01 June, 2019 09:14 AM PDT

Hello.

Post a New Comment

Do you like having a good time?

Register an Account

You can also login to an existing account or reset your password. All use of this site is subject to the terms of service and privacy policy.

Read Quality Articles

Read some quality articles. If you can manage to not get banned for like five minutes, you can even post your own articles.

View Articles →

Argue with People on the Internet

Use your account to explain why people are wrong on the Internet forum.

View Forum →

Vandalize the Wiki

Or don't. I'm not your dad.

View Wiki →

Ask and/or Answer Questions

If someone asks a terrible question, post a LMGTFY link.

View Answers →

Make Some Money

Hire freelancers and/or advertise your goods and/or services. Hire people directly. We're not a middleman or your dad. Manage your own business transactions.

Register an Account

Answers— Read More

Find more related content below!