Skip to content

Ensure TimeDelta can be initialized with empty quantity.#15604

Merged
pllim merged 1 commit into
astropy:mainfrom
mhvk:time-add-empty-quantity
Nov 13, 2023
Merged

Ensure TimeDelta can be initialized with empty quantity.#15604
pllim merged 1 commit into
astropy:mainfrom
mhvk:time-add-empty-quantity

Conversation

@mhvk

@mhvk mhvk commented Nov 12, 2023

Copy link
Copy Markdown
Contributor

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

  • By checking this box, the PR author has requested that maintainers do NOT use the "Squash and Merge" button. Maintainers should respect this when possible; however, the final decision is at the discretion of the maintainer that merges the PR.

@mhvk mhvk added time Bug Affects-dev PRs and issues that do not impact an existing Astropy release no-changelog-entry-needed labels Nov 12, 2023
@mhvk mhvk added this to the v6.0 milestone Nov 12, 2023
@mhvk mhvk requested a review from taldcroft as a code owner November 12, 2023 01:33
@github-actions

github-actions Bot commented Nov 12, 2023

Copy link
Copy Markdown
Contributor

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.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see instructions for rebase and squash.
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the "Extra CI" label. Codestyle issues can be fixed by the bot.
  • Is a change log needed? If yes, did the change log check pass? If no, add the "no-changelog-entry-needed" label. If this is a manual backport, use the "skip-changelog-checks" label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • Is a milestone set? Milestone must be set but we cannot check for it on Actions; do not let the green checkmark fool you.
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate "backport-X.Y.x" label(s) before merge.

Comment thread astropy/time/core.py Outdated
# 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):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put the not isinstance(...) condition first to not do the potentially expensive all(mask) when it's not needed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@taldcroft taldcroft left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me and I agree with the comment from @maxnoe .

@mhvk mhvk force-pushed the time-add-empty-quantity branch from 8118d21 to 8e9c3a1 Compare November 13, 2023 01:27
Comment thread astropy/time/core.py
"""
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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
            ):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@mhvk mhvk force-pushed the time-add-empty-quantity branch from 8e9c3a1 to e0bbf6c Compare November 13, 2023 13:09

@pllim pllim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@pllim pllim merged commit 4dbaa03 into astropy:main Nov 13, 2023
meeseeksmachine pushed a commit to meeseeksmachine/astropy that referenced this pull request Nov 13, 2023
@mhvk mhvk deleted the time-add-empty-quantity branch November 13, 2023 14:56
pllim added a commit that referenced this pull request Nov 13, 2023
…604-on-v6.0.x

Backport PR #15604 on branch v6.0.x (Ensure TimeDelta can be initialized with empty quantity.)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Affects-dev PRs and issues that do not impact an existing Astropy release Bug no-changelog-entry-needed time

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adding empty quantity to time raises misleading error in 6.0rc1

4 participants