|
3 | 3 | v0.22.0
|
4 | 4 | -------
|
5 | 5 |
|
6 |
| -This is a major release from 0.21.1 and includes a number of API changes, |
7 |
| -deprecations, new features, enhancements, and performance improvements along |
8 |
| -with a large number of bug fixes. We recommend that all users upgrade to this |
9 |
| -version. |
| 6 | +This is a major release from 0.21.1 and includes a single, API breaking change. |
| 7 | +We recommend that all users upgrade to this version after carefully reading the |
| 8 | +release note (singular!). |
10 | 9 |
|
11 | 10 | .. _whatsnew_0220.api_breaking:
|
12 | 11 |
|
13 | 12 | Backwards incompatible API changes
|
14 | 13 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 14 | + |
| 15 | +Pandas 0.22.0 changes the handling of empty and all-NA sums and products. The |
| 16 | +summary is that |
| 17 | + |
| 18 | +* The sum of an all-NA or empty series is now 0 |
| 19 | +* The product of an all-NA or empty series is now 1 |
| 20 | +* We've added a ``min_count`` parameter to ``.sum`` and ``.prod`` to control |
| 21 | + the minimum number of valid values for the result to be valid. If fewer than |
| 22 | + ``min_count`` valid values are present, the result is NA. The default is |
| 23 | + ``0``. To restore the 0.21 behavior, use ``min_count=1``. |
| 24 | + |
| 25 | +Some background: In pandas 0.21.1, we fixed a long-standing inconsistency |
| 26 | +in the return value of all-NA series depending on whether or not bottleneck |
| 27 | +was installed. See :ref:`whatsnew_0210.api_breaking.bottleneck`_. At the same |
| 28 | +time, we changed the sum and prod of an empty Series to also be ``NaN``. |
| 29 | + |
| 30 | +Based on feedback, we've partially reverted those changes. The default sum |
| 31 | +for all-NA and empty series is now 0 (1 for ``prod``). |
| 32 | + |
| 33 | +*pandas 0.21* |
| 34 | + |
| 35 | +.. code-block:: ipython |
| 36 | + |
| 37 | + In [1]: import pandas as pd |
| 38 | + |
| 39 | + In [2]: import numpy as np |
| 40 | + |
| 41 | + In [3]: pd.Series([]).sum() |
| 42 | + Out[3]: nan |
| 43 | + |
| 44 | + In [4]: pd.Series([np.nan]).sum() |
| 45 | + Out[4]: nan |
| 46 | + |
| 47 | +*pandas 0.22.0* |
| 48 | + |
| 49 | +.. ipython:: python |
| 50 | + |
| 51 | + pd.Series([]).sum() |
| 52 | + pd.Series([np.nan]).sum() |
| 53 | + |
| 54 | +To have the sum of an empty series return ``NaN``, use the ``min_count`` |
| 55 | +keyword. Thanks to the ``skipna`` parameter, the ``.sum`` on an all-NA |
| 56 | +series is conceptually the same as on an empty. The ``min_count`` parameter |
| 57 | +refers to the minimum number of *valid* values required for a non-NA sum |
| 58 | +or product. |
| 59 | + |
| 60 | +.. ipython:: python |
| 61 | + |
| 62 | + pd.Series([]).sum(min_count=1) |
| 63 | + pd.Series([np.nan]).sum(min_count=1) |
| 64 | + |
| 65 | +Note that this affects some other places in the library: |
| 66 | + |
| 67 | +1. Grouping by a Categorical with some unobserved categories |
| 68 | + |
| 69 | +*pandas 0.21* |
| 70 | + |
| 71 | +.. code-block:: ipython |
| 72 | + |
| 73 | + In [3]: grouper = pd.Categorical(['a', 'a'], categories=['a', 'b']) |
| 74 | + |
| 75 | + In [4]: pd.Series([1, 2]).groupby(grouper).sum() |
| 76 | + Out[4]: |
| 77 | + a 3.0 |
| 78 | + b NaN |
| 79 | + dtype: float64 |
| 80 | + |
| 81 | +*pandas 0.22* |
| 82 | + |
| 83 | +.. ipython:: python |
| 84 | + |
| 85 | + grouper = pd.Categorical(['a', 'a'], categories=['a', 'b']) |
| 86 | + pd.Series([1, 2]).groupby(grouper).sum() |
| 87 | + |
| 88 | + pd.Series([1, 2]).groupby(groupuer).sum(min_count=1) |
| 89 | + |
| 90 | +2. Upsampling |
| 91 | + |
| 92 | +*pandas 0.21.0* |
| 93 | + |
| 94 | +.. code-block:: ipython |
| 95 | + |
| 96 | + In [5]: idx = pd.DatetimeIndex(['2017-01-01', '2017-01-02']) |
| 97 | + |
| 98 | + In [6]: pd.Series([1, 2], index=idx).resample('12H').sum() |
| 99 | + Out[6]: |
| 100 | + 2017-01-01 00:00:00 1.0 |
| 101 | + 2017-01-01 12:00:00 NaN |
| 102 | + 2017-01-02 00:00:00 2.0 |
| 103 | + Freq: 12H, dtype: float64 |
| 104 | + |
| 105 | +*pandas 0.22.0* |
| 106 | + |
| 107 | +.. ipython:: python |
| 108 | + |
| 109 | + idx = pd.DatetimeIndex(['2017-01-01', '2017-01-02']) |
| 110 | + pd.Series([1, 2], index=idx).resample("12H").sum() |
| 111 | + |
| 112 | + pd.Series([1, 2], index=idx).resample("12H").sum(min_count=1) |
0 commit comments