-
Notifications
You must be signed in to change notification settings - Fork 12
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
[PR] time type conversion to timedelta if timedelta64 is given #660
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #660 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 15 15
Lines 1542 1544 +2
=========================================
+ Hits 1542 1544 +2 ☔ View full report in Codecov by Sentry. |
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.
@lee1043 - thanks for creating this issue and PR. Even though this was solved with use_cftime=True
, I think this is an issue because this functionality should work with np.datetime64
axes.
I have a minor comment that I'm not really sure about the answer to (I'm curious about your opinion and @tomvothecoder's opinion).
xcdat/bounds.py
Outdated
diff = pd.to_timedelta(time.values[1] - time.values[0]) | ||
# `cftime` objects only support arithmetic using `timedelta` objects, so | ||
# the values of `diffs` must be casted from `dtype="timedelta64[ns]"` | ||
# to `timedelta` objects. |
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.
diff = pd.to_timedelta(time.values[1] - time.values[0]) | |
# `cftime` objects only support arithmetic using `timedelta` objects, so | |
# the values of `diffs` must be casted from `dtype="timedelta64[ns]"` | |
# to `timedelta` objects. | |
# `cftime` objects only support arithmetic using `timedelta` objects, so | |
# the values of `diffs` must be casted from `dtype="timedelta64[ns]"` | |
# to `timedelta` objects. | |
diff = pd.to_timedelta(time.values[1] - time.values[0]) |
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.
Suggest putting the comment ahead of the code (as is done earlier in the code).
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.
I was initially averse to adding in a pandas converter here, because in a different project I've run into issues with this (the behavior of the datetime conversion is sensitive to the input array), but your pointing out that this is already done earlier in the code makes me way less worried about this.
In the code block you pointed out in the issue, this conversion is conditioned on the array being a time axis (not needed here) and the array containing cftime
objects. It does seem like it might prevent problems to only do this conversion if necessary (i.e., add a conditional here).
But I am a little confused because the existing conditional is meant to apply to cftime
-based axes (and here it is meant to apply to axes that do not have cftime
objects).
I'm curious about:
- should there be a conditional to do this type conversion?
- should the conditional be applied if the object
type(time.values[0]) == np.datetime64)
ornot contains_cftime_datetimes(xr.as_variable(time))
?
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.
@pochedls thanks for the suggestion. I initially have coded as like below:
if isinstance(diff, np.timedelta64):
diff = pd.to_timedelta(diff)
but this interrupted the GitHub Action CI check process for build, which was complaining that the function is too complicated. It seem like the CI process didn't like having more than a certain number of if
layers.
I tested the code with a CMIP model data that has cftime-based time axis, and the same operation was running okay without errors, so was presuming it was okay with cftime-based axes. But I am open to any suggestion to make the code more robust.
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.
I like this... @tomvothecoder may be able to suggest some a way to pass the CI tests (or skip them for that line, e.g., here).
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.
I also like it but it does pass flake8 C901 with the error message of xcdat/bounds.py:507:5: C901 'BoundsAccessor._create_time_bounds' is too complex (11)
# type: ignore
or #noqa: C901
(following the Stackoverflow) didn't help.
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.
I updated the code to have the if statement (fb90864) but as expected it fails flake8 checks. @tomvothecoder Any help will be appreciated.
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.
…vert it, so following `.seconds` line could run without error
Co-authored-by: Stephen Po-Chedley <pochedley@gmail.com>
- Add comment about needing to convert dype=timedelta64[ns] to pandas timedelta object - Ignore C901 flake8 error
0acb51e
to
c6b314a
Compare
I pushed commit c6b314a to add test coverage for the new code. This PR should be good to go. |
@tomvothecoder thanks for the addition. I am merging this PR. |
Description
make sure time diff type is timedelta: in case it was timedelta64 convert it, so following
.seconds
line could run without erroradd_missing_bounds
not working when time type isnumpy.timedelta64
#659Checklist
If applicable: