|
1 | 1 | Frequently Asked Questions
|
2 | 2 | ==========================
|
3 | 3 |
|
| 4 | +.. ipython:: python |
| 5 | + :suppress: |
| 6 | +
|
| 7 | + import numpy as np |
| 8 | + import pandas as pd |
| 9 | + import xarray as xr |
| 10 | + np.random.seed(123456) |
| 11 | +
|
4 | 12 | Why is pandas not enough?
|
5 | 13 | -------------------------
|
6 | 14 |
|
@@ -58,6 +66,38 @@ fundamentally multi-dimensional. If your data is unstructured or
|
58 | 66 | one-dimensional, stick with pandas.
|
59 | 67 |
|
60 | 68 |
|
| 69 | +Why don't aggregations return Python scalars? |
| 70 | +--------------------------------------------- |
| 71 | + |
| 72 | +xarray tries hard to be self-consistent: operations on a ``DataArray`` (resp. |
| 73 | +``Dataset``) return another ``DataArray`` (resp. ``Dataset``) object. In |
| 74 | +particular, operations returning scalar values (e.g. indexing or aggregations |
| 75 | +like ``mean`` or ``sum`` applied to all axes) will also return xarray objects. |
| 76 | + |
| 77 | +Unfortunately, this means we sometimes have to explicitly cast our results from |
| 78 | +xarray when using them in other libraries. As an illustration, the following |
| 79 | +code fragment |
| 80 | + |
| 81 | +.. ipython:: python |
| 82 | +
|
| 83 | + arr = xr.DataArray([1, 2, 3]) |
| 84 | + pd.Series({'x': arr[0], 'mean': arr.mean(), 'std': arr.std()}) |
| 85 | +
|
| 86 | +does not yield the pandas DataFrame we expected. We need to specify the type |
| 87 | +conversion ourselves: |
| 88 | + |
| 89 | +.. ipython:: python |
| 90 | +
|
| 91 | + pd.Series({'x': arr[0], 'mean': arr.mean(), 'std': arr.std()}, dtype=float) |
| 92 | +
|
| 93 | +Alternatively, we could use the ``item`` method or the ``float`` constructor to |
| 94 | +convert values one at a time |
| 95 | + |
| 96 | +.. ipython:: python |
| 97 | +
|
| 98 | + pd.Series({'x': arr[0].item(), 'mean': float(arr.mean())}) |
| 99 | +
|
| 100 | +
|
61 | 101 | .. _approach to metadata:
|
62 | 102 |
|
63 | 103 | What is your approach to metadata?
|
|
0 commit comments