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

Changes to raise exceptions when comparing bounded datetime cells #1016

Merged
merged 2 commits into from
Feb 13, 2014

Conversation

esc24
Copy link
Member

@esc24 esc24 commented Feb 10, 2014

This PR is in response to unexpected and highly undesirable behaviour identified by #984. The effect of this PR is to raise an exception when attempting to constrain bounded time coords with the new date time cell comparison feature. This is disappointing, but without calendar information it is not possible to determine whether a PartialDateTime (or any other calendar-agnostic datetime-like object) lies within a bounded region. As Iris currently stands you'll get the wrong answer when performing such constraints and absolutely no indication that something has gone awry. Note that point based datetime constraints (i.e. those in the docs) are fine.

Please also notice that I've put in some special behaviour around netcdftime.datetime objects as I can do:

>>> import netcdftime
>>> a = netcdftime.datetime(2000, 1, 1)
>>> b = netcdftime.datetime(1990, 1, 1)
>>> a > b
False
>>> a < b
True

This fallback to id-based comparison (I think) makes comparing netcdftime.datetime objects to anything other than PartialDateTimes unreliable. I'd rather not put this time-specific/netcdftime-specific logic in coords.py but I can't think of a better place without a lot of work.

@@ -184,14 +186,22 @@ def __common_cmp__(self, other, operator_method):
Non-Cell vs Cell comparison is used to define Constraint matching.

"""
if not (isinstance(other, (int, float, np.number, Cell)) or
hasattr(other, 'timetuple')):
Copy link
Member

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?

Copy link
Member Author

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.datetime to 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.

Copy link
Member

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....

@esc24
Copy link
Member Author

esc24 commented Feb 12, 2014

Arrgh

@esc24 esc24 closed this Feb 12, 2014
@esc24 esc24 reopened this Feb 12, 2014
@esc24
Copy link
Member Author

esc24 commented Feb 12, 2014

@pelson - I had a git hiccup which meant I accidentally rebased against upstream/master. I've cherry-picked my way out so hopefully that won't be a problem. travis is failing due to the netcdf issue that's been fixed on upstream/master but not in this branch.

@@ -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 '
Copy link
Member

Choose a reason for hiding this comment

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

This is where having a PartialDateTime.within(lower_dt, upper_dt) would be valuable, right...

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes.

@pelson
Copy link
Member

pelson commented Feb 13, 2014

👍 - I think this is good to go. Any reason not to merge @esc24?

@esc24
Copy link
Member Author

esc24 commented Feb 13, 2014

Nope. I think it's good to go @pelson. How about I rebase just to let travis do its thing?

pelson added a commit that referenced this pull request Feb 13, 2014
Changes to raise exceptions when comparing bounded datetime cells
@pelson pelson merged commit 66b4fb1 into SciTools:v1.6.x Feb 13, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants