In Place Modification of Coordinates

Coordinates are generally considered to be immutable. If you want to create another coordinate frame with different data you should use realize_frame, this is the safest way to change the data in a frame as it creates a new frame with that data. Creating a new frame can be relatively slow, however, particularly for scalar coordinates. Hence some situations may require that data by changed in-place in an already existing frame object. This modification can be done by modifying the values of the representation data as follows:

>>> import astropy.units as u
>>> from astropy.coordinates import SkyCoord
>>> c = SkyCoord([1,2],[3,4], unit='deg')
>>> c.data.lon[()] = [10, 20] * u.deg
>>> c  
<SkyCoord (ICRS): (ra, dec) in deg
    [(10., 3.), (20., 4.)]>

This changes the longitude values of the frame. Unfortunately, doing just this introduces problems: SkyCoord and BaseCoordinateFrame cache various kinds of information to speed up some repeated operations. So we need to tell the cache that it should be cleared so that it can be re-calculated from the new data. This can be achieved by doing:

>>> c.cache.clear()

It should be noted that the only way to modify the data in a frame is by using the .data attribute directly and not the aliases for components on the frame i.e. the following will not work::

>>> c.ra[()] = 20 * u.deg

This is because a different representation object is used when accessing the aliased component names. If you wish to inspect the mapping between frame attributes i.e. .ra and representation attributes i.e. .lon you can look at the following dictionary.:

>>> c.representation_component_names
OrderedDict([('ra', 'lon'), ('dec', 'lat'), ('distance', 'distance')])