Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow negative freq strings #8651

Merged
merged 13 commits into from Feb 1, 2024
Merged

Conversation

mathause
Copy link
Collaborator

@mathause mathause commented Jan 24, 2024

  • Closes #xxxx
  • Tests added
  • User visible changes (including notable bug fixes) are documented in whats-new.rst

This allows negative freq strings as discussed in #8627 (comment) Deciding which tests to update was not easy.

The pandas _generate_range function was moved to https://github.com/pandas-dev/pandas/blob/3c96b8ff6d399fbec8d4d533e8e8618c592bb64b/pandas/core/arrays/datetimes.py#L2725 They no longer rollback the end. I had to remove this as well such that the following are eqivalent:

xr.date_range("2001", "2000", freq="-1YE", calendar="noleap")
pd.date_range("2001", "2000", freq="-1YE")

I am slightly nervous about this but all the tests still pass...

Once again cc @spencerkclark

@dcherian
Copy link
Contributor

Just so I understand, this is not backwards-incompatible because we did not allow negative frequency strings earlier?

@mathause
Copy link
Collaborator Author

Just so I understand, this is not backwards-incompatible because we did not allow negative frequency strings earlier?

Yes that's correct.

Copy link
Member

@spencerkclark spencerkclark left a comment

Choose a reason for hiding this comment

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

This looks great @mathause—thanks!

I think the tests you updated look pretty solid. In my testing it seems like it just works, but maybe one more you could add would be an explicit test that calling infer_freq on a decreasing CFTimeIndex returns a negative frequency? (Edit this is tested in the test_date_range_like test).

xarray/tests/test_cftime_offsets.py Outdated Show resolved Hide resolved
xarray/tests/test_cftime_offsets.py Outdated Show resolved Hide resolved
@mathause mathause added plan to merge Final call for comments topic-cftime labels Jan 31, 2024
Comment on lines 1623 to 1627
result = date_range(start, end, freq=freq, calendar="standard", use_cftime=True)
expected = date_range(start, end, freq=freq, use_cftime=False)

# end of month/ year is not convertable to datetimeindex
assert result.size == expected.size
Copy link
Member

Choose a reason for hiding this comment

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

I think we can check for equality now, similar to the test above:

Suggested change
result = date_range(start, end, freq=freq, calendar="standard", use_cftime=True)
expected = date_range(start, end, freq=freq, use_cftime=False)
# end of month/ year is not convertable to datetimeindex
assert result.size == expected.size
result = date_range(start, end, freq=freq, calendar="standard", use_cftime=True)
result = result.to_datetimeindex()
expected = date_range(start, end, freq=freq, use_cftime=False)
np.testing.assert_array_equal(result, expected)

Copy link
Member

Choose a reason for hiding this comment

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

Ultimately I suppose this test could now be merged with the one above—i.e. just merge the frequency strings.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

of course - thanks

@mathause mathause enabled auto-merge (squash) February 1, 2024 08:16
@mathause mathause merged commit c9ba2be into pydata:main Feb 1, 2024
28 of 29 checks passed
@mathause mathause deleted the allow_negative_freq branch February 1, 2024 09:17
flamingbear pushed a commit to flamingbear/xarray that referenced this pull request Feb 12, 2024
* allow negative freq strings

* update docstring

* fix date_range_like

* whats-new entry

* Apply suggestions from code review

Co-authored-by: Spencer Clark <spencerkclark@gmail.com>

* only test standard calendar

* merge tests

---------

Co-authored-by: Spencer Clark <spencerkclark@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plan to merge Final call for comments topic-cftime
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants