0

I have an astropy Table input_table with a field/column 'observation_id', which contains the objects like 81-126. I wish to separate the two numbers (81 and 126) and save them as integers. I am writing all the steps here to show the type of objects I have at each step. Please let me know if something is not clear.

In [62]: id=input_table['observation_id']

In [63]: str=id[0]

In [64]: print(str)
81-126

In [65]: print(type(str))
<class 'numpy.str_'>

In [66]: nx,ny=str.split("-")

In [67]: print(nx,ny)
81 126

In [68]: print(type(nx),type(ny))
<class 'str'> <class 'str'>

When I do the following to convert the string into integer,

In [70]: int(float(nx))

I get:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-70-7cfc33470a5c> in <module>()
----> 1 int(float(nx))

ValueError: could not convert string to float: '8\x00\x00\x001\x00\x00\x00'

Looking at the link for the type of string '8\x00\x00\x001\x00\x00\x00'

I tried

In [71]: nx,ny=str.decode(encode='utf-16-le').split('-')

But I get

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-71-5f8d49af6ed1> in <module>()
----> 1 nx,ny=str.decode(encode='utf-16-le').split('-')

AttributeError: 'numpy.str_' object has no attribute 'decode'

I don't know how to proceed further. Could someone help please?

Community
  • 1
  • 1
nk_science
  • 23
  • 4

1 Answers1

0

I can't reproduce your example in Python 2.7 or 3.3 with numpy 1.7. Can you give more specifics about versions and platform?

One point is that you are using str as a variable which overloads the built-in str type. I don't think this is directly a problem here but it is certainly not a good idea in general.

Tom Aldcroft
  • 2,339
  • 1
  • 14
  • 16
  • Thanks for your answer, here are the versions I am using: Python 3.4.0 , Numpy 1.8.1, IPython 2.0.0, and my astropy table is in fits format. – nk_science Apr 11 '14 at 09:53