Skip to content

Conversation

@mhvk
Copy link
Contributor

@mhvk mhvk commented Oct 14, 2013

Multiplying an nd.array with a TimeDelta fails, as the numpy function tries to get an item that does not exist (independent of whether TimeDelta is an array or not. It seems TimeDelta never gets a chance to try (the reverse -- TimeDelta * Array works fine); somehow, numpy fails to return NotImplemented? Mysterious.

In [1]: from astropy.time import Time, TimeDelta; import numpy as np

In [2]: np.array(1)*TimeDelta(100., format='sec')
ERROR: TypeError: scalar 'TimeDelta' object is not subscriptable. [astropy.time.core]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-91a8a4ea9a5c> in <module>()
----> 1 np.array(1)*TimeDelta(100., format='sec')

/home/mhvk/packages/astropy/astropy/time/core.py in __getitem__(self, item)
    506         if self.isscalar:
    507             raise TypeError('scalar {0!r} object is not subscriptable.'.format(
--> 508                 self.__class__.__name__))
    509         tm = self.replicate()
    510         jd1 = self._time.jd1[item]

TypeError: scalar 'TimeDelta' object is not subscriptable.

@mhvk
Copy link
Contributor Author

mhvk commented Oct 14, 2013

@taldcroft - the odd behaviour I noticed with array*time was due to Time not being properly prepared to deal with arrays. One needs to set __array_priority__ and also ensure that __len__ fails for scalar values (see a stackoverflow query by @astrofrog [1]). With that introduced,

from astropy.time import Time, TimeDelta; import numpy as np
np.arange(10.)*TimeDelta(100., format='sec')

yields

<TimeDelta object: scale='tai' format='sec' value=[   0.  100.  200.  300.  400.  500.  600.  700.  800.  900.]>

as it should. The one disadvantage is that len(t) for single times now gives a TypeError (but the same is true for arrays, quantities, etc.).

[1] http://stackoverflow.com/questions/7667799/numpy-object-arrays

@mhvk
Copy link
Contributor Author

mhvk commented Oct 18, 2013

@taldcroft - if you have a chance to review -- this is really quite a small bugfix...

taldcroft added a commit that referenced this pull request Oct 18, 2013
Bugfix: Array * TimeDelta fails for mysterious reason
@taldcroft taldcroft merged commit 0b0a85f into astropy:master Oct 18, 2013
@mhvk mhvk deleted the time-array-priority branch October 18, 2013 03:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants