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

Return numpy.datetime64 arrays for non-standard calendars #126

Merged
merged 8 commits into from
May 16, 2014
Merged

Return numpy.datetime64 arrays for non-standard calendars #126

merged 8 commits into from
May 16, 2014

Conversation

jhamman
Copy link
Member

@jhamman jhamman commented May 13, 2014

Fixes issues in #118 and #121

Joe Hamman added 2 commits May 12, 2014 14:25
def nctime_to_nptime(times):
"""Given an array of netCDF4.datetime objects, return an array of
numpy.datetime64 objects"""
new = np.empty(len(times), dtype='M8[ns]')
Copy link
Member

Choose a reason for hiding this comment

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

Could you please make this work for arrays with arbitrary shape?

You would need to replace len(times) with times.size and then do a reshape at the end to times.shape.

…e ability to fallback to netCDF4.datetime objects since this solution does not fix the problem for all calendars in all situations.
'numpy.datetime64 objects, continuing using '
'dummy netCDF4.datetime objects instead',
RuntimeWarning, stacklevel=2)
pass
Copy link
Member

Choose a reason for hiding this comment

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

pass is no-op here -- you can safely remove it.

@shoyer
Copy link
Member

shoyer commented May 13, 2014

The issue with the 360_day calendar is sort of what I was afraid of with the non-standard calendars. I think issuing warnings is a reasonable solution, but perhaps it would be better to not even try to convert dates with calendars that can't be safely converted? That would be slightly more predictable (I would still probably issue a warning in those cases). Looking at the standard NetCDF calendars it appears that the only problematic ones would be "all_leap"/"366_day" and "uniform30day"/"360_day".

It would also be nice if you could also issue a warning when we fall back to netCDF4 datetime objects because there are dates outside the valid range. The DecodedCFDatetimeArray object pretends to have dtype=np.datetime64, and in this case the values turn out not to be.

In terms of tests:

  1. Please also verify that converting a single element returns what you expect. This should be a np.datetime64 object, not a 0-dimensional datetime64 array, since numpy's support for these is semi-broken.
  2. Please use a context manager to verify that the warning you expect is issued. This also keeps expected warnings from polluting our test output. See: https://docs.python.org/2/library/warnings.html#testing-warnings

warnings.warn('Unable to decode time axis into full '
'numpy.datetime64 objects, continuing using '
'dummy netCDF4.datetime objects instead, reason:'
'{0}'.format(e), RuntimeWarning, stacklevel=2)
Copy link
Member

Choose a reason for hiding this comment

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

It is indeed better to issue a single warning -- that way it can be filtered more easily.

Joe Hamman added 3 commits May 14, 2014 17:33
Expanded warnings for when non numpy.datetime64 arrays are returned.
Use context manager for warnings in test_conventions.py.
@jhamman
Copy link
Member Author

jhamman commented May 15, 2014

I think this is all ready to go.

In the future and maybe as a separate project, I'd like to see more complete non-Gregorian calendar support in Python. I know the climate modeling community would utilize the feature. For now however, I think this is a good step for xray.

@requires_netCDF4
def test_decode_non_standard_calendar_single_element(self):

for calendar in ['noleap', '365_day', '360_day', 'julian', 'all_leap',
Copy link
Member

Choose a reason for hiding this comment

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

My preference would be to the test the different calendars separately, based on whether they can be decoded into datetime64 or not. That way you can verify expected results exactly (e.g., the array dtype and whether they raise a warning or not). The current test leaves some room for ambiguity:).

Copy link
Member Author

Choose a reason for hiding this comment

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

I've written a separate test to make sure we get what we're expecting for single element fallbacks.

shoyer added a commit that referenced this pull request May 16, 2014
Return numpy.datetime64 arrays for non-standard calendars
@shoyer shoyer merged commit 556db83 into pydata:master May 16, 2014
@shoyer
Copy link
Member

shoyer commented May 16, 2014

Thanks you, Joe!

@jhamman
Copy link
Member Author

jhamman commented May 16, 2014

Sure thing. Do you want to close the associated issues (#118 and #121) or should I?

@shoyer
Copy link
Member

shoyer commented May 16, 2014

I just closed them. There is actually a cool feature of GitHub where if you write your commit message in the form "Fixes #118" the issue gets closed automatically upon merging: https://help.github.com/articles/closing-issues-via-commit-messages

keewis pushed a commit to keewis/xarray that referenced this pull request Jan 17, 2024
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