Skip to content

Commit 502a988

Browse files
0x0LJoe Hamman
authored and
Joe Hamman
committed
pandas casting issues (#1734)
* pandas casting issues * update * single value * whats-new * add credit for 0x0L doc updates
1 parent 5a6dea4 commit 502a988

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

doc/faq.rst

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
Frequently Asked Questions
22
==========================
33

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+
412
Why is pandas not enough?
513
-------------------------
614

@@ -58,6 +66,38 @@ fundamentally multi-dimensional. If your data is unstructured or
5866
one-dimensional, stick with pandas.
5967

6068

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+
61101
.. _approach to metadata:
62102

63103
What is your approach to metadata?

doc/whats-new.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ What's New
1818
v0.10.1 (unreleased)
1919
--------------------
2020

21+
Documentation
22+
~~~~~~~~~~~~~
23+
24+
- New entry `Why don’t aggregations return Python scalars?` in the
25+
:doc:`faq` (:issue:`1726`).
26+
By `0x0L <https://github.com/0x0L>`_.
27+
2128
Enhancements
2229
~~~~~~~~~~~~
2330

0 commit comments

Comments
 (0)