Skip to content

Allow empty Time objects #7866

@astrofrog

Description

@astrofrog

Numpy arrays, Quantity objects, SkyCoord objects, and others allow arrays with no elements, but Time doesn't. For example it would be nice for the following to work:

Initialization

In [1]: from astropy.time import Time

In [2]: t = Time([], format='iso')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-e9a03eac829b> in <module>()
----> 1 t = Time([], format='iso')

~/Dropbox/Code/Astropy/astropy/astropy/time/core.py in __init__(self, val, val2, format, scale, precision, in_subfmt, out_subfmt, location, copy)
    348         else:
    349             self._init_from_vals(val, val2, format, scale, copy,
--> 350                                  precision, in_subfmt, out_subfmt)
    351             self.SCALES = TIME_TYPES[self.scale]
    352 

~/Dropbox/Code/Astropy/astropy/astropy/time/core.py in _init_from_vals(self, val, val2, format, scale, copy, precision, in_subfmt, out_subfmt)
    403         # Parse / convert input values into internal jd1, jd2 based on format
    404         self._time = self._get_time_fmt(val, val2, format, scale,
--> 405                                         precision, in_subfmt, out_subfmt)
    406         self._format = self._time.name
    407 

~/Dropbox/Code/Astropy/astropy/astropy/time/core.py in _get_time_fmt(self, val, val2, format, scale, precision, in_subfmt, out_subfmt)
    454                 pass
    455         else:
--> 456             raise ValueError('Input values did not match {0}'.format(err_msg))
    457 
    458     @classmethod

ValueError: Input values did not match the format class iso

Slicing

In [9]: t[1:1]
Out[9]: ---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/miniconda3/envs/dev/lib/python3.7/site-packages/IPython/core/formatters.py in __call__(self, obj)
    700                 type_pprinters=self.type_printers,
    701                 deferred_pprinters=self.deferred_printers)
--> 702             printer.pretty(obj)
    703             printer.flush()
    704             return stream.getvalue()

~/miniconda3/envs/dev/lib/python3.7/site-packages/IPython/lib/pretty.py in pretty(self, obj)
    398                         if cls is not object \
    399                                 and callable(cls.__dict__.get('__repr__')):
--> 400                             return _repr_pprint(obj, self, cycle)
    401 
    402             return _default_pprint(obj, self, cycle)

~/miniconda3/envs/dev/lib/python3.7/site-packages/IPython/lib/pretty.py in _repr_pprint(obj, p, cycle)
    693     """A pprint that just redirects to the normal repr function."""
    694     # Find newlines and replace them with p.break_()
--> 695     output = repr(obj)
    696     for idx,output_line in enumerate(output.splitlines()):
    697         if idx:

~/Dropbox/Code/Astropy/astropy/astropy/time/core.py in __repr__(self)
    580         return ("<{0} object: scale='{1}' format='{2}' value={3}>"
    581                 .format(self.__class__.__name__, self.scale, self.format,
--> 582                         getattr(self, self.format)))
    583 
    584     def __str__(self):

~/Dropbox/Code/Astropy/astropy/astropy/time/core.py in __getattr__(self, attr)
   1409                 else:
   1410                     tm = self.replicate(format=attr)
-> 1411                 value = tm._shaped_like_input(tm._time.to_value(parent=tm))
   1412                 cache[attr] = value
   1413             return cache[attr]

~/Dropbox/Code/Astropy/astropy/astropy/time/formats.py in to_value(self, timezone, parent)
    667         iterator = np.nditer([iys, ims, ids, ihrs, imins, isecs, ifracs, None],
    668                              flags=['refs_ok'],
--> 669                              op_dtypes=7*[iys.dtype] + [object])
    670 
    671         for iy, im, id, ihr, imin, isec, ifracsec, out in iterator:

ValueError: Iteration of zero-sized operands is not enabled

Of course it's unlikely people will deliberately create empty time series, but this might happen during scripts that create or slice many time series programmatically (as for Numpy arrays).

@taldcroft @mhvk - what do you think?

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