Skip to content

Angle.to_string() returns a 0-d array with dtype=object - #1374

Merged
mdboom merged 1 commit into
astropy:masterfrom
mdboom:coordinates/angle-to-string-scalars
Sep 3, 2013
Merged

mdboom merged 1 commit into
astropy:masterfrom
mdboom:coordinates/angle-to-string-scalars

Conversation

@mdboom

@mdboom mdboom commented Aug 26, 2013

Copy link
Copy Markdown
Contributor

Not sure if this is intended:

In [1]: from astropy.coordinates.angles import Angle
In [2]: a = Angle('45d')
In [3]: a.to_string()
Out[3]: array(u'45d00m00.00000s', dtype=object)

I would have expected a plain string (or unicode obj) here.

In the case where the Angle is representing an array of angles, I would expect to_string() to return a numpy array with dtype='S..'.

@astrofrog

Copy link
Copy Markdown
Member

I've run into this before and I'm +1 on changing this to return a unicode object.

@ghost ghost assigned mdboom Aug 26, 2013
@mdboom

mdboom commented Aug 26, 2013

Copy link
Copy Markdown
Contributor

I commented on this already here: #1006 (comment)

Practically speaking, what is the downside here. Arrays of objects is, unfortunately, the "standard" way to represent variable length strings in Numpy, and Numpy provides no good way to do otherwise.

@taldcroft

Copy link
Copy Markdown
Member Author

When I ask for something as a string, then I expect string operations to work:

In [10]: a = Angle('45d')
In [12]: out = a.to_string()
In [13]: out[:2]  # get the first two characters.
ERROR: ValueError: cannot slice a 0-d array [IPython.core.interactiveshell]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-13-d16174cd7ed4> in <module>()
----> 1 out[:2]

ValueError: cannot slice a 0-d array

What would be the downside of a final check for out.ndim == 0 in to_string() and then returning unicode(out) instead (assuming out is the result of the final format_ufunc(values) in to_string().

@mdboom

mdboom commented Aug 26, 2013

Copy link
Copy Markdown
Contributor

I see. That makes sense. It seems that in the case of an array (not scalar), this requirement already holds, so there's nothing to do there:

>>> import numpy as np
>>> x = np.array(['foo', 'bar', 'baz'], dtype=object)
>>> x[0][:2]
'fo'

mdboom added a commit that referenced this pull request Sep 3, 2013
Angle.to_string() returns a 0-d array with dtype=object
@mdboom
mdboom merged commit bdb2eb6 into astropy:master Sep 3, 2013
taldcroft added a commit to taldcroft/astropy that referenced this pull request Sep 3, 2013
taldcroft added a commit that referenced this pull request Sep 3, 2013
@mdboom
mdboom deleted the coordinates/angle-to-string-scalars branch May 21, 2014 23:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants