-
Notifications
You must be signed in to change notification settings - Fork 300
Changes to raise exceptions when comparing bounded datetime cells #1016
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,10 +28,12 @@ | |
| import warnings | ||
| import zlib | ||
|
|
||
| import netcdftime | ||
| import numpy as np | ||
|
|
||
| import iris.aux_factory | ||
| import iris.exceptions | ||
| import iris.time | ||
| import iris.unit | ||
| import iris.util | ||
|
|
||
|
|
@@ -186,12 +188,20 @@ def __common_cmp__(self, other, operator_method): | |
| """ | ||
| if not (isinstance(other, (int, float, np.number, Cell)) or | ||
| hasattr(other, 'timetuple')): | ||
| raise ValueError("Unexpected type of other " | ||
| "{}.".format(type(other))) | ||
| raise TypeError("Unexpected type of other " | ||
| "{}.".format(type(other))) | ||
| if operator_method not in (operator.gt, operator.lt, | ||
| operator.ge, operator.le): | ||
| raise ValueError("Unexpected operator_method") | ||
|
|
||
| # Prevent silent errors resulting from missing netcdftime | ||
| # behaviour. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We really need to extend this functionality in NetCDFtime (or take the datetime under our wings). It might also be nice to state that this functionality has not been implemented yet. |
||
| if (isinstance(other, netcdftime.datetime) or | ||
| (isinstance(self.point, netcdftime.datetime) and | ||
| not isinstance(other, iris.time.PartialDateTime))): | ||
| raise TypeError('Cannot determine the order of ' | ||
| 'netcdftime.datetime objects') | ||
|
|
||
| if isinstance(other, Cell): | ||
| # Cell vs Cell comparison for providing a strict sort order | ||
| if self.bound is None: | ||
|
|
@@ -235,13 +245,17 @@ def __common_cmp__(self, other, operator_method): | |
| else: | ||
| result = operator_method(self.bound[0], other.bound[0]) | ||
| else: | ||
| # Cell vs number (or string) for providing Constraint | ||
| # behaviour. | ||
| # Cell vs number (or string, or datetime-like) for providing | ||
| # Constraint behaviour. | ||
| if self.bound is None: | ||
| # Point vs number | ||
| # - Simple matching | ||
| me = self.point | ||
| else: | ||
| if hasattr(other, 'timetuple'): | ||
| raise TypeError('Cannot determine whether a point lies ' | ||
| 'within a bounded region for ' | ||
| 'datetime-like objects.') | ||
| # Point-and-bound vs number | ||
| # - Match if "within" the Cell | ||
| if operator_method in [operator.gt, operator.le]: | ||
|
|
@@ -281,6 +295,10 @@ def contains_point(self, point): | |
| """ | ||
| if self.bound is None: | ||
| raise ValueError('Point cannot exist inside an unbounded cell.') | ||
| if hasattr(point, 'timetuple') or np.any([hasattr(val, 'timetuple') for | ||
| val in self.bound]): | ||
| raise TypeError('Cannot determine whether a point lies within ' | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is where having a
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. |
||
| 'a bounded region for datetime-like objects.') | ||
|
|
||
| return np.min(self.bound) <= point <= np.max(self.bound) | ||
|
|
||
|
|
||
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.
Is there a reason for the behaviour change? Doesn't this now prevent us comparing with normal datetimes?
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.
It does. I could revert this one or add
datetime.datetimeto the list. The danger of letting any timetuple possessing object through is that you could get datetime cells being compared to netcdftimes and getting nonsense.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.
We could just do a
not isinstance(other, netcdftime)along with the original check. I guess the question comes - do we want to be able to compare Cells with datetimes? In my head we do, but if there a reason that is a bad thing, then I wouldn't lose sleep over it....