Skip to content

Commit c9b9eec

Browse files
blsqrdcheriankeewis
authored
Use explicit type check in as_compatible_data instead of blanket access to values attribute (#2905)
* In as_compatible_data, check explicitly for nested self-described types This change was prompted by the fact that `getattr(data, 'values', data)` affected any kind of data with a `values` attribute, which is not the desired behaviour at that point. This also extends tests to assert that custom objects with such an attribute are not attempted to be converted * Add whats-new entry * Remove trailing whitespace * In as_compatible_data, check explicitly for nested self-described types This change was prompted by the fact that `getattr(data, 'values', data)` affected any kind of data with a `values` attribute, which is not the desired behaviour at that point. This also extends tests to assert that custom objects with such an attribute are not attempted to be converted * whats-new * Fix test. * Update @blsqr github URL in whats-new * actually check that values is not extracted Co-authored-by: dcherian <[email protected]> Co-authored-by: Keewis <[email protected]>
1 parent 0a309e0 commit c9b9eec

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

doc/whats-new.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ New Features
9090

9191
Bug fixes
9292
~~~~~~~~~
93+
- Use specific type checks in
94+
:py:func:`~xarray.core.variable.as_compatible_data` instead of blanket
95+
access to ``values`` attribute (:issue:`2097`)
96+
By `Yunus Sevinchan <https://github.com/blsqr>`_.
9397
- :py:meth:`DataArray.resample` and :py:meth:`Dataset.resample` do not trigger computations anymore if :py:meth:`Dataset.weighted` or :py:meth:`DataArray.weighted` are applied (:issue:`4625`, :pull:`4668`). By `Julius Busecke <https://github.com/jbusecke>`_.
9498
- :py:func:`merge` with ``combine_attrs='override'`` makes a copy of the attrs (:issue:`4627`).
9599
- By default, when possible, xarray will now always use values of type ``int64`` when encoding

xarray/core/variable.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ def as_compatible_data(data, fastpath=False):
218218
data = np.timedelta64(getattr(data, "value", data), "ns")
219219

220220
# we don't want nested self-described arrays
221-
data = getattr(data, "values", data)
221+
if isinstance(data, (pd.Series, pd.Index, pd.DataFrame)):
222+
data = data.values
222223

223224
if isinstance(data, np.ma.MaskedArray):
224225
mask = np.ma.getmaskarray(data)

xarray/tests/test_variable.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2300,6 +2300,11 @@ def __init__(self, array):
23002300
class CustomIndexable(CustomArray, indexing.ExplicitlyIndexed):
23012301
pass
23022302

2303+
# Type with data stored in values attribute
2304+
class CustomWithValuesAttr:
2305+
def __init__(self, array):
2306+
self.values = array
2307+
23032308
array = CustomArray(np.arange(3))
23042309
orig = Variable(dims=("x"), data=array, attrs={"foo": "bar"})
23052310
assert isinstance(orig._data, np.ndarray) # should not be CustomArray
@@ -2308,6 +2313,10 @@ class CustomIndexable(CustomArray, indexing.ExplicitlyIndexed):
23082313
orig = Variable(dims=("x"), data=array, attrs={"foo": "bar"})
23092314
assert isinstance(orig._data, CustomIndexable)
23102315

2316+
array = CustomWithValuesAttr(np.arange(3))
2317+
orig = Variable(dims=(), data=array)
2318+
assert isinstance(orig._data.item(), CustomWithValuesAttr)
2319+
23112320

23122321
def test_raise_no_warning_for_nan_in_binary_ops():
23132322
with pytest.warns(None) as record:

0 commit comments

Comments
 (0)