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

encode_cf_variable triggers AttributeError: 'DataArray' object has no attribute '_data' #7645

Closed
4 tasks done
snowman2 opened this issue Mar 19, 2023 · 3 comments
Closed
4 tasks done
Labels
bug needs triage Issue that has not been reviewed by xarray team member

Comments

@snowman2
Copy link
Contributor

What happened?

AttributeError: 'DataArray' object has no attribute '_data'

What did you expect to happen?

No error.

Minimal Complete Verifiable Example

# https://github.com/corteva/rioxarray/blob/21284f67db536d9c104aa872ab0bbc261259e59e/test/integration/test_integration_rioxarray.py#L1818-L1844

import numpy
import xarray
import rioxarray

test_da = xarray.DataArray(
    numpy.zeros((5, 5)),
    dims=("y", "x"),
    coords={"y": numpy.arange(1, 6), "x": numpy.arange(2, 7)},
)
test_da.values[1, 1] = -1.1
test_nd = test_da.rio.write_nodata(-1.1)
test_nd.rio.write_transform(
    Affine.from_gdal(425047, 3.0, 0.0, 4615780, 0.0, -3.0), inplace=True
)
test_nd.rio.write_crs("EPSG:4326", inplace=True)
test_nd.rio.to_raster("dtype.tif", dtype=numpy.uint8)

MVCE confirmation

  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.

Relevant log output

rioxarray/raster_writer.py:288: in to_raster
    data = encode_cf_variable(out_data).values.astype(numpy_dtype)
/usr/share/miniconda/envs/test/lib/python3.9/site-packages/xarray/conventions.py:296: in encode_cf_variable
    var = coder.encode(var, name=name)
/usr/share/miniconda/envs/test/lib/python3.9/site-packages/xarray/coding/times.py:690: in encode
    ) or contains_cftime_datetimes(variable):
/usr/share/miniconda/envs/test/lib/python3.9/site-packages/xarray/core/common.py:1818: in contains_cftime_datetimes
    return _contains_cftime_datetimes(var._data)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <xarray.DataArray (y: 5, x: 5)>
array([[ 0. ,  0. ,  0. ,  0. ,  0. ],
       [ 0. , -1.1,  0. ,  0. ,  0. ],
       [...    (y) int64 1 2 3 4 5
  * x            (x) int64 2 3 4 5 6
    spatial_ref  int64 0
Attributes:
    _FillValue:  -1.1
name = '_data'

    def __getattr__(self, name: str) -> Any:
        if name not in {"__dict__", "__setstate__"}:
            # this avoids an infinite loop when pickle looks for the
            # __setstate__ attribute before the xarray object is initialized
            for source in self._attr_sources:
                with suppress(KeyError):
                    return source[name]
>       raise AttributeError(
            f"{type(self).__name__!r} object has no attribute {name!r}"
        )
E       AttributeError: 'DataArray' object has no attribute '_data'

/usr/share/miniconda/envs/test/lib/python3.9/site-packages/xarray/core/common.py:276: AttributeError

Anything else we need to know?

No response

Environment

This is the latest version of xarray/numpy (pre-release versions).

https://github.com/corteva/rioxarray/actions/runs/4458288974/jobs/7829970587

@snowman2 snowman2 added bug needs triage Issue that has not been reviewed by xarray team member labels Mar 19, 2023
@Illviljan
Copy link
Contributor

Probably from #7494. encode_cf_variable only accepts Variables. Replace

data = encode_cf_variable(out_data).values.astype(numpy_dtype)

with

data = encode_cf_variable(out_data.variable).values.astype(numpy_dtype)

should fix the error.

mypy should have caught this a while ago when #7374 went in, does out_data have defined type hints?

@dcherian
Copy link
Contributor

^ 👍

Passing .variable should fix it but the real issue here is that you're using private API because there is no public API: #4412

That would be a nice PR!

cc @JessicaS11 @scottyhq

@snowman2
Copy link
Contributor Author

Thanks @Illviljan 👍

mypy should have caught this a while ago

Type hints are likely incomplete/inexact in some places in rioxarray code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug needs triage Issue that has not been reviewed by xarray team member
Projects
None yet
Development

No branches or pull requests

3 participants