-
Notifications
You must be signed in to change notification settings - Fork 16
Fix ZeroDivisonError in add T bounds #761
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
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #761 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 16 16
Lines 1702 1702
=========================================
Hits 1702 1702 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@tomvothecoder this fix works okay for the e3sm_diags PR:E3SM-Project/e3sm_diags#982 |
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.
Pull Request Overview
This PR fixes a ZeroDivisionError when computing hourly intervals by using total_seconds() instead of the seconds attribute on timedelta objects.
- Uses
diff.total_seconds()for accurate hour calculation - Maintains downstream daily sub-frequency logic
Comments suppressed due to low confidence (1)
xcdat/bounds.py:652
- Add unit tests for edge cases where
diffis zero, less than one hour, exactly 24 hours, and greater than 24 hours to verifydaily_subfreqis computed correctly and no division-by-zero or invalid frequency occurs.
daily_subfreq = int(24 / hrs) # type: ignore
| diff = pd.to_timedelta(diff) | ||
|
|
||
| hrs = diff.seconds / 3600 | ||
| hrs = diff.total_seconds() / 3600 |
Copilot
AI
May 20, 2025
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.
If diff exceeds 24 hours, hrs will be >=24 and daily_subfreq = int(24 / hrs) becomes zero, leading to downstream errors. Consider adding a guard to ensure hrs is within (0, 24] or handle diff > 24h explicitly before computing daily_subfreq.
| hrs = diff.total_seconds() / 3600 | |
| hrs = diff.total_seconds() / 3600 | |
| if not (0 < hrs <= 24): | |
| raise ValueError( | |
| f"Invalid time difference: {hrs} hours. Expected a value in the range (0, 24]." | |
| ) |
|
@chengzhuzhang – is there a compact explanation for why I can see how It might be helpful to add a little bit of context if we ever revisit this section of code. |
|
@pochedls yes, I should have written better description... I'm just copy over the caution statement you linked: It is a somewhat common bug for code to unintentionally use this attribute when it is actually intended to get a total_seconds() value instead: In my case, I'm processing daily data, for example, I have a timedelta object like following: |
|
I realize it is daily data, so I would have thought the inferred frequency is daily and it would have skipped the problematic lines of code altogether. |
Interesting! I only looked around lines of code that caused trouble and didn't go through the logic. Yes, it is interesting this was caught by freq = hours condition. |
xCDAT's The call stack looks like:
Is there a bug in |
|
yep, probably the inferring mechanism (bullet # 3) is not very reliable. Following based on data from the MVCE. Change |
|
Having a close look of the data. I think it might be data problem. The min time difference that is shorter than one day is actually caused by an odd time values around Feb of a leap year: |
Description
Checklist
If applicable: