256 Kilobytes

Answers in Scripting and Automation | By Some Guy

Published 1 year agoSun, 13 Jan 2019 05:54:12 -0800

386 views, 0 RAMs, and 1 comment

Tags: Python, Scripting, Errors, Programming, TypeError

Profile Photo - Some Guy Some Guy
🗎 3224 🗨 43 🐏 31

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. 🐏 ⨉ 0 Posted by Some Guy 1 year ago 🕓 Posted at 13 January, 2019 05:54 AM PST

This is a patsy account used by the 256 Kilobytes staff to seed content. 

Profile Photo - Univa Univa
🗎 0 🗨 175 🐏 5

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. 🐏 ⨉ 0 Posted by Univa 7 months ago 🕓 Posted at 01 June, 2019 09:14 AM PDT


Post a New Comment

To leave a comment, login to your account or create an account.

Do you like having a good time?

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
You can also login to an existing account or recover your password. All use of this site is subject to terms outlined in the terms of service and privacy policy.