Ensure TimeDelta can be initialized with empty quantity.#15604
Conversation
|
Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.
|
| # long as it has time units, but that will be checked later). | ||
| empty_array = val.size == 0 and (val2 is None or val2.size == 0) | ||
| if empty_array or np.all(mask): | ||
| if (empty_array or np.all(mask)) and not isinstance(val, u.Quantity): |
There was a problem hiding this comment.
Put the not isinstance(...) condition first to not do the potentially expensive all(mask) when it's not needed?
There was a problem hiding this comment.
I had wondered about it but felt quantity input was less likely, but thinking again, the isinstance is really negligible compared to everything else while going over the full mask may not be. Anyway, done now.
8118d21 to
8e9c3a1
Compare
| """ | ||
| if format is None: | ||
| # If val and val2 broadcasted shape is (0,) (i.e. empty array input) then we | ||
| # cannot guess format from the input values. Instead use the default |
There was a problem hiding this comment.
Is that really important though? The internal representation is always the same. So why not simply always create an empty time instance for empty input?
There was a problem hiding this comment.
I guess the question then is what the format of the empty time instance should be.
Also, don't forget this line is also for all-masked values.
There was a problem hiding this comment.
I guess the question then is what the format of the empty time instance should be.
That's always confusing me. Why are things like "format" and "precision" instance properties? The internal representation is always the same. It's affecting only affecting the default repr, right?
There was a problem hiding this comment.
Oops, this change is actually introducing a new regression since Quantity is only allowed for TimeDelta not Time.
In [3]: Time([] * u.s)
Out[3]: <Time object: scale='utc' format='ymdhms' value=[]>
Instead for Time this should raise an exception like it currently does. The logic is getting ugly, but I think it could look like:
if not (isinstance(self, TimeDelta) and isinstance(val, u.Quantity)) and (
empty_array or np.all(mask)
):
There was a problem hiding this comment.
I guess the question then is what the format of the empty time instance should be.
For TimeDelta the default format of jd is fine. As mentioned for Time we don't have to worry about it since an empty input with no format should always raise an exception.
There was a problem hiding this comment.
@taldcroft - OK, I pushed your change
@maxnoe - I agree that it is a little strange to error for what indeed is in a sense a change in repr - though this affects things like .value as well. But it is a bit out of scope here - I just want to fix a regression! Also, as noted earlier, the same logic is applied for fully masked input.
There was a problem hiding this comment.
@mhvk's point about .value is crucial. Without a defined .format then .value is undefined. The .value is a primary functional output of the Time/TimeDelta classes.
There was a problem hiding this comment.
And format also tells the class initializer and the object how to interpret input values. E.g.
tm = Time(["2023-01-01"])
tm[0] = 100.0
# ValueError
tm.format = "unix"
tm[0] = 100.0
# OK
8e9c3a1 to
e0bbf6c
Compare
pllim
left a comment
There was a problem hiding this comment.
Approving by proxy, since the person who originally reported this is satisfied. Thanks for the quick turnaround! Let's merge so this does not miss the RC2 boat.
…604-on-v6.0.x Backport PR #15604 on branch v6.0.x (Ensure TimeDelta can be initialized with empty quantity.)
Description
This pull request is to address a small regression introduced by #15264, where
TimeDelta([] * u.s)no longer works. Would be nice to include in 6.0 to avoid a regression in released astropy versions.Fixes #15601