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

Fixing issue #8770: Improved frequency parameter logic to set it to 'D' only if periods, start, or end are None. #8774

Merged
merged 7 commits into from
Feb 24, 2024

Conversation

rjavierch
Copy link
Contributor

This Pull Request addresses the issue reported regarding the default setting of the frequency (freq) parameter in Xarray. Currently, the frequency is explicitly set to "D" by default, which may not always be appropriate behavior. The proposed improvement adjusts the logic to set the frequency to "D" only if any of the parameters (periods, start, or end) are None, providing more flexibility and aligning with expected behavior.

Changes:

Updated logic for setting the frequency parameter to consider periods, start, or end values.
Added checks to ensure that the frequency is set to "D" only when necessary.

Please review and provide feedback on the proposed changes. Any suggestions for further improvements are welcome.

…D' only if periods, start, or end are None.
Copy link

welcome bot commented Feb 21, 2024

Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient.
If you have questions, some answers may be found in our contributing guidelines.

@mathause
Copy link
Collaborator

Looks good. Can you add a small test (let us know if you need support for that) and maybe an entry to the whats-new doc.

@rjavierch
Copy link
Contributor Author

Looks good. Can you add a small test (let us know if you need support for that) and maybe an entry to the whats-new doc.

Oh, yes, I would like to receive the support for that. I would love to add a small test and an entry to the whats-new doc.

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.

Indeed thanks @rjavierch—just one minor rethink on my part. In terms of testing, you could add a test similar to:

def test_cftime_range_same_as_pandas(start, end, freq):
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)

Only in this case you would test the situation where freq is not provided as an argument to date_range, but start, end, and periods are.

Comment on lines 1221 to 1222
if freq is None and any(arg is None for arg in [periods, start, end]):
freq = "D"
Copy link
Member

Choose a reason for hiding this comment

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

Sorry on second thought could you move this logic into cftime_range (defined a little farther up in this file)? That way we can harmonize the default arguments across pandas.date_range, xarray.cftime_range, and xarray.date_range.

… to ensure consistency across date range functions
@rjavierch
Copy link
Contributor Author

Thanks for the feedback! I moved the logic from date_range to cftime_range since indeed, it was redundant in date_range as pandas should handle it. Now the lines are in cftime_range as it lacks the logic.
Also, the tests were also included.

However, I wondered whether to change the docstrings defaults from "D" to None. Pandas date_range function has default input parameter None but the docstring says the default is "D" pandas.date_range.

Copy link
Collaborator

@mathause mathause left a comment

Choose a reason for hiding this comment

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

However, I wondered whether to change the docstrings defaults from "D" to None. Pandas date_range function has default input parameter None but the docstring says the default is "D" pandas.date_range.

No strong opinion. I would probably have kept it as is (it's good enough for pandas...)


There is a failing test, because only passing start and end works now. Can you update the test_invalid_cftime_range_inputs by removing the line:

-("2000", "2001", None, None, None),

Your new tests are good, but they don't check the issue you reported in #8770, yet. Can you add a test that ensures the following is valid:

xr.cfdate_range(start="2001-01-01", end="2002-01-01", periods=2)

(and also for date_range)

doc/whats-new.rst Outdated Show resolved Hide resolved
xarray/tests/test_cftime_offsets.py Outdated Show resolved Hide resolved
xarray/tests/test_cftime_offsets.py Outdated Show resolved Hide resolved
@rjavierch
Copy link
Contributor Author

Thanks for the feedback! I have updated the test_invalid_cftime_range_inputs inputs and added the periods argument as input in tests test_cftime_range_no_freq and test_date_range_no_freq.

Copy link
Collaborator

@mathause mathause left a comment

Choose a reason for hiding this comment

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

LGTM

@rjavierch
Copy link
Contributor Author

Perfect! I close the issue with your approval.

@rjavierch rjavierch closed this Feb 23, 2024
@mathause
Copy link
Collaborator

I reopen the PR - we still have to merge it 🙂 we can do that soon, but I want to give others the chance to have a look if they want

@mathause mathause reopened this Feb 23, 2024
@mathause mathause added the plan to merge Final call for comments label Feb 23, 2024
@mathause
Copy link
Collaborator

Failure is unrelated.

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.

Looks good to me too—thanks @rjavierch!

doc/whats-new.rst Outdated Show resolved Hide resolved
@rjavierch
Copy link
Contributor Author

I reopen the PR - we still have to merge it 🙂 we can do that soon, but I want to give others the chance to have a look if they want

Oh true, my bad! if there is something extra I need to do, please do not hesitate to let me know.

Co-authored-by: Spencer Clark <[email protected]>
@spencerkclark
Copy link
Member

All good @rjavierch—merging now.

@spencerkclark spencerkclark merged commit f63ec87 into pydata:main Feb 24, 2024
29 checks passed
Copy link

welcome bot commented Feb 24, 2024

Congratulations on completing your first pull request! Welcome to Xarray! We are proud of you, and hope to see you again! celebration gif

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error in xr.date_range with exactly three parameters provided.
3 participants