O.rka
O.rka

Reputation: 30757

Get values (point, vector, array, etc.) from `xr.Dataset` in Xarray ? (Python 3)

I can't figure out how to actually pull the data out of a xr.Dataset object. I can't figure out how to access individual values. How can I pull the values (point values, vectors, arrays, etc.) out of the Datasets like I can with the DataArrays?

np.random.seed(0)
DA_data = xr.DataArray(np.random.random((3,2,10,100)), dims=["targets","accuracy","metrics","attributes"], name="Data")
DA_data.coords["attributes"] = ["attr_%d"%_ for _ in range(100)]
# DA_data.coords
# Coordinates:
#   * targets     (targets) int64 0 1 2
#   * accuracy    (accuracy) int64 0 1
#   * metrics     (metrics) int64 0 1 2 3 4 5 6 7 8 9
#   * attributes  (attributes) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...

# Indexing DataArray
#DA_data.sel(targets=0, accuracy=0, metrics=0, attributes="attr_5").values
#array(0.6458941130666561)
float(DA_data.sel(targets=0, accuracy=0, metrics=0, attributes="attr_5").values)
#0.6458941130666561

# Indexing Dataset
DS_data = DA_data.to_dataset()
# DS_data
# <xarray.Dataset>
# Dimensions:     (accuracy: 2, attributes: 100, metrics: 10, targets: 3)
# Coordinates:
#   * targets     (targets) int64 0 1 2
#   * accuracy    (accuracy) int64 0 1
#   * metrics     (metrics) int64 0 1 2 3 4 5 6 7 8 9
#   * attributes  (attributes) <U7 'attr_0' 'attr_1' 'attr_2' 'attr_3' ...
# Data variables:
#     Data        (targets, accuracy, metrics, attributes) float64 0.5488 ...

DS_data.sel(targets=0, accuracy=0, metrics=0, attributes="attr_5").values
# <bound method Mapping.values of <xarray.Dataset>
# Dimensions:     ()
# Coordinates:
#     targets     int64 0
#     accuracy    int64 0
#     metrics     int64 0
#     attributes  <U7 'attr_5'
# Data variables:
#     Data        float64 0.6459>
float(DS_data.sel(targets=0, accuracy=0, metrics=0, attributes="attr_5").values)
# ---------------------------------------------------------------------------
# TypeError                                 Traceback (most recent call last)
# <ipython-input-408-e0c88e8541d8> in <module>()
#      38 # Data variables:
#      39 #     Data        float64 0.6459>
# ---> 40 float(DS_data.sel(targets=0, accuracy=0, metrics=0, attributes="attr_5").values)

# TypeError: float() argument must be a string or a number, not 'method'

Upvotes: 7

Views: 9904

Answers (1)

shoyer
shoyer

Reputation: 9623

It's a little confusing, but .values works differently on Dataset and DataArray:

  • DataArray.values returns a NumPy array. This behavior is consistent with pandas.
  • Dataset.values() returns a list (Python 2) or ValuesView (Python 3) of the DataArray objects that constitute the Dataset. This behavior is consistent with Dataset satisfying Python's Mapping interface.

To pull values out of a Dataset, you need to pull out a DataArray via the dataset's dictionary-like interface, e.g., float(DA_data['Data']) or float(DA_data.values()[0]). You can't directly convert a Dataset into a float or NumPy array, no more than you could with a Python dict.

Upvotes: 9

Related Questions