'Create a new instance, applying a method to the underlying data.
In typical usage, the method is any of the shape-changing methods for
`~numpy.ndarray` (``reshape``, ``swapaxes``, etc.), as well as those
picking particular elements (``__getitem__``, ``take``, etc.), which
are all defined in `~astropy.utils.misc.ShapedLikeNDArray`. It will be
applied to the underlying arrays in the representation (e.g., ``x``,
``y``, and ``z`` for `~astropy.coordinates.CartesianRepresentation`),
as well as to any frame attributes that have a shape, with the results
used to create a new instance.
Internally, it is also used to apply functions to the above parts
(in particular, `~numpy.broadcast_to`).
Parameters
method : str or callable
If str, it is the name of a method that is applied to the internal
``components``. If callable, the function is applied.
args : tuple
Any positional arguments for ``method``.
kwargs : dict
Any keyword arguments for ``method``.'

def _apply(self, method, *args, **kwargs):

    if (u'_framedata' in kwargs):
        data = kwargs.pop(u'_framedata')
    else:
        data = (self.data if self.has_data else None)
    representation_cls = kwargs.pop(u'representation_cls', self.representation)
    differential_cls = kwargs.pop(u'differential_cls', self.get_representation_cls(u's'))
    def apply_method(value):
        if isinstance(value, ShapedLikeNDArray):
            if ((method == u'replicate') and (not hasattr(value, method))):
                return value
            else:
                return value._apply(method, *args, **kwargs)
        elif callable(method):
            return method(value, *args, **kwargs)
        elif ((method == u'replicate') and (not hasattr(value, method))):
            return value
        else:
            return getattr(value, method)(*args, **kwargs)
    if (data is not None):
        data = apply_method(data)
    frattrs = {u'representation': representation_cls, u'differential_cls': differential_cls}
    for attr in self.get_frame_attr_names():
        if (attr not in self._attr_names_with_defaults):
            if (((method == u'copy') or (method == u'replicate')) and (attr in kwargs)):
                value = kwargs[attr]
            else:
                value = getattr(self, attr)
            if (getattr(value, u'size', 1) > 1):
                value = apply_method(value)
            elif ((method == u'copy') or (method == u'flatten')):
                value = copy.copy(value)
            frattrs[attr] = value
    return self.__class__(data, **frattrs)
