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

fix datetime issues #984

Merged
merged 4 commits into from
Aug 25, 2016
Merged

fix datetime issues #984

merged 4 commits into from
Aug 25, 2016

Conversation

mathause
Copy link
Collaborator

fixes: #975
work around: pandas-dev/pandas#14068

I finally found 3 issues:

(1) pretty print fails when date is out of bounds
(2) pandas Overflow error (pandas-dev/pandas#14068)
(3) decode_cf_datetime when first date is in bound but later dates are not

import xarray as xr
from datetime import datetime


# (1) pretty print
xr.core.formatting.format_timestamp(datetime(2800, 1, 1))

# (2) overflow error
ds = xr.Dataset(coords={'time' : [0, 266 * 365]})
units = 'days since 2000-01-01 00:00:00'
ds.time.attrs = dict(units=units)
ds_decoded = xr.conventions.decode_cf(ds)
ds_decoded.time

import netCDF4 as nc
import numpy as np
import xarray as xr

# (3) create netCDF file
ncf = nc.Dataset('test_future.nc', 'w')
ncf.createDimension('time')
ncf.createVariable('time', np.int, dimensions=('time'))
ncf.variables['time'].units = 'days since 1850-01-01 00:00:00'
ncf.variables['time'].calendar = 'standard'
ncf.variables['time'][:] = np.arange(850) * 365
ncf.close()

# open with xr
ds = xr.open_dataset('test_future.nc')
# this works
ds
# ds.time is a datetime64[ns] object
# this fails
ds.time

try:
datetime_str = unicode_type(pd.Timestamp(t))
except OutOfBoundsDatetime:
datetime_str = unicode_type(t.__str__())
Copy link
Member

Choose a reason for hiding this comment

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

just use unicode_type(t)

@mathause
Copy link
Collaborator Author

thanks for your feedback! I updated the pr

@shoyer
Copy link
Member

shoyer commented Aug 25, 2016

This looks great to me -- thanks!

Once you add a bug-fix note to "What's New" this will be good go in.

- pretty print when date is out of bounds
- decode_cf_datetime when first date is in bound but later dates are not
- work around pandas Overflow error (pandas-dev/pandas#14068)
@shoyer shoyer merged commit 6cf5d0c into pydata:master Aug 25, 2016
@shoyer
Copy link
Member

shoyer commented Aug 25, 2016

Thanks @mathause !

@mathause mathause deleted the fix_datetime branch August 26, 2016 06:57
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.

invalid timestamps in the future
2 participants