-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Conversation
…D' only if periods, start, or end are None.
Thank you for opening this pull request! It may take us a few days to respond here, so thank you for being patient. |
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. |
There was a problem hiding this 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:
xarray/xarray/tests/test_cftime_offsets.py
Lines 1730 to 1735 in 8e1dfcf
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.
xarray/coding/cftime_offsets.py
Outdated
if freq is None and any(arg is None for arg in [periods, start, end]): | ||
freq = "D" |
There was a problem hiding this comment.
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
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. 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. |
There was a problem hiding this 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
)
Co-authored-by: Mathias Hauser <[email protected]>
Co-authored-by: Mathias Hauser <[email protected]>
Co-authored-by: Mathias Hauser <[email protected]>
…date_range_no_freq following #8770
Thanks for the feedback! I have updated the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Perfect! I close the issue with your approval. |
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 |
Failure is unrelated. |
There was a problem hiding this 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!
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]>
All good @rjavierch—merging now. |
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.