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

another cftime failure #3427

Closed
dcherian opened this issue Oct 22, 2019 · 2 comments · Fixed by #3430
Closed

another cftime failure #3427

dcherian opened this issue Oct 22, 2019 · 2 comments · Fixed by #3430

Comments

@dcherian
Copy link
Contributor

________ TestNetCDF3ViaNetCDF4Data.test_roundtrip_cftime_datetime_data _________

dates = array([cftime.DatetimeProlepticGregorian(0001-01-01 00:00:00),
       cftime.DatetimeProlepticGregorian(0001-01-02 00:00:00)],
      dtype=object)
units = 'days since 0001-01-01 00:00:00.000000'
calendar = 'proleptic_gregorian'

    def encode_cf_datetime(dates, units=None, calendar=None):
        """Given an array of datetime objects, returns the tuple `(num, units,
        calendar)` suitable for a CF compliant time variable.
    
        Unlike `date2num`, this function can handle datetime64 arrays.
    
        See also
        --------
        cftime.date2num
        """
        dates = np.asarray(dates)
    
        if units is None:
            units = infer_datetime_units(dates)
        else:
            units = _cleanup_netcdf_time_units(units)
    
        if calendar is None:
            calendar = infer_calendar_name(dates)
    
        delta, ref_date = _unpack_netcdf_time_units(units)
        try:
            if calendar not in _STANDARD_CALENDARS or dates.dtype.kind == "O":
                # parse with cftime instead
>               raise OutOfBoundsDatetime
E               pandas._libs.tslibs.np_datetime.OutOfBoundsDatetime

xarray/coding/times.py:400: OutOfBoundsDatetime

During handling of the above exception, another exception occurred:

self = <xarray.tests.test_backends.TestNetCDF3ViaNetCDF4Data object at 0x7fd54f4cb780>

    @requires_cftime
    def test_roundtrip_cftime_datetime_data(self):
        from .test_coding_times import _all_cftime_date_types
    
        date_types = _all_cftime_date_types()
        for date_type in date_types.values():
            times = [date_type(1, 1, 1), date_type(1, 1, 2)]
            expected = Dataset({"t": ("t", times), "t0": times[0]})
            kwargs = {"encoding": {"t0": {"units": "days since 0001-01-01"}}}
            expected_decoded_t = np.array(times)
            expected_decoded_t0 = np.array([date_type(1, 1, 1)])
            expected_calendar = times[0].calendar
    
            with warnings.catch_warnings():
                if expected_calendar in {"proleptic_gregorian", "gregorian"}:
                    warnings.filterwarnings("ignore", "Unable to decode time axis")
    
>               with self.roundtrip(expected, save_kwargs=kwargs) as actual:

xarray/tests/test_backends.py:483: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/usr/share/miniconda/envs/xarray-tests/lib/python3.6/contextlib.py:81: in __enter__
    return next(self.gen)
xarray/tests/test_backends.py:243: in roundtrip
    self.save(data, path, **save_kwargs)
xarray/tests/test_backends.py:265: in save
    path, engine=self.engine, format=self.file_format, **kwargs
xarray/core/dataset.py:1536: in to_netcdf
    invalid_netcdf=invalid_netcdf,
xarray/backends/api.py:1071: in to_netcdf
    dataset, store, writer, encoding=encoding, unlimited_dims=unlimited_dims
xarray/backends/api.py:1117: in dump_to_store
    store.store(variables, attrs, check_encoding, writer, unlimited_dims=unlimited_dims)
xarray/backends/common.py:293: in store
    variables, attributes = self.encode(variables, attributes)
xarray/backends/common.py:382: in encode
    variables, attributes = cf_encoder(variables, attributes)
xarray/conventions.py:748: in cf_encoder
    new_vars = {k: encode_cf_variable(v, name=k) for k, v in variables.items()}
xarray/conventions.py:748: in <dictcomp>
    new_vars = {k: encode_cf_variable(v, name=k) for k, v in variables.items()}
xarray/conventions.py:250: in encode_cf_variable
    var = coder.encode(var, name=name)
xarray/coding/times.py:446: in encode
    data, encoding.pop("units", None), encoding.pop("calendar", None)
xarray/coding/times.py:419: in encode_cf_datetime
    num = _encode_datetime_with_cftime(dates, units, calendar)
xarray/coding/times.py:366: in _encode_datetime_with_cftime
    return np.vectorize(encode_datetime)(dates)
/usr/share/miniconda/envs/xarray-tests/lib/python3.6/site-packages/numpy/lib/function_base.py:2755: in __call__
    return self._vectorize_call(func=func, args=vargs)
/usr/share/miniconda/envs/xarray-tests/lib/python3.6/site-packages/numpy/lib/function_base.py:2825: in _vectorize_call
    ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args)
/usr/share/miniconda/envs/xarray-tests/lib/python3.6/site-packages/numpy/lib/function_base.py:2785: in _get_ufunc_and_otypes
    outputs = func(*inputs)
xarray/coding/times.py:364: in encode_datetime
    return np.nan if d is None else cftime.date2num(d, units, calendar)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

>   ???
E   AttributeError: 'cftime._cftime.DatetimeProlepticGregorian' object has no attribute 'tzinfo'

cftime/_cftime.pyx:186: AttributeError
@spencerkclark
Copy link
Member

Hmm...this feels like it may be a cftime bug. In version 1.0.3.4 the following succeeds without an error:

In [1]: import cftime

In [2]: d = cftime.DatetimeProlepticGregorian(1, 1, 1)

In [3]: cftime.date2num(d, 'days since 0001-01-01', 'proleptic_gregorian')
Out[3]: 0.0

However, with 1.0.4, the following occurs:

In [3]: cftime.date2num(d, 'days since 0001-01-01', 'proleptic_gregorian')
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-5b0bfcafe058> in <module>
----> 1 cftime.date2num(d, 'days since 0001-01-01', 'proleptic_gregorian')

cftime/_cftime.pyx in cftime._cftime.date2num()

AttributeError: 'cftime._cftime.DatetimeProlepticGregorian' object has no attribute 'tzinfo'

I filed an issue upstream: Unidata/cftime#126.

@spencerkclark
Copy link
Member

@jswhit has tracked down the source of the problem, added a fix in Unidata/cftime#127, and kindly agreed to cut a new release. I can confirm that with this, and @andersy005's fix in #3430, the xarray test suite passes.

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 a pull request may close this issue.

2 participants