8000 Cannot use unscaled integer quantities as indices · Issue #3046 · astropy/astropy · GitHub
[go: up one dir, main page]

Skip to content
Cannot use unscaled integer quantities as indices #3046
@bnordgren

Description

@bnordgren

I have an output from a calculation which I need to use as an index to an array. I've converted the type to int and made sure the units are "dimensionless_unscaled", but I keep getting a TypeError.

The below code is kind of an exemplar which directly creates a dimensionless_unscaled, integer quantity, and tries to use it as an index. I believe this should work, but it raises the same TypeError.

import numpy as np
import astropy.units as u
x = [1,2,3,4]
ind = u.Quantity(np.array([0,1]), dtype=np.int, unit=u.dimensionless_unscaled)
ind.dtype
Out[6]: dtype('int32')
ind.unit
Out[7]: Unit(dimensionless)
x[ind]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-0528873d532e> in <module>()
----> 1 x[ind]

C:\Users\bnordgren\AppData\Local\Enthought\Canopy\User\lib\site-packages\astropy\units\quantity.pyc in __index__(self)
    847             return self.value.__index__()
    848         except:
--> 849             raise TypeError('Only integer dimensionless scalar quantities '
    850                             'can be converted to a Python index')
    851 

TypeError: Only integer dimensionless scalar quantities can be converted to a Python index 
x[np.array([0,1])]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-c81fa8325335> in <module>()
----> 1 x[np.array([0,1])]

TypeError: only integer arrays with one element can be converted to an index 
ind = u.Quantity([0,1], dtype=np.int, unit=u.dimensionless_unscaled)

ind.dtype
Out[11]: dtype('int32')

ind.unit
Out[12]: Unit(dimensionless)

x[ind]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-0528873d532e> in <module>()
----> 1 x[ind]

C:\Users\bnordgren\AppData\Local\Enthought\Canopy\User\lib\site-packages\astropy\units\quantity.pyc in __index__(self)
    847             return self.value.__index__()
    848         except:
--> 849             raise TypeError('Only integer dimensionless scalar quantities '
    850                             'can be converted to a Python index')
    851 

TypeError: Only integer dimensionless scalar quantities can be converted to a Python index 

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0