|
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 return ``NaN``, the 0.21 behavior, use ``min_count=1``. |
| 24 | + |
| 25 | +Some background: In pandas 0.21, 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 for |
| 31 | +all-NA and empty series is now 0 (1 for ``prod``). |
| 32 | + |
| 33 | +*pandas 0.21* |
| 34 | + |
| 35 | +.. code-block:: ipython |
| 36 | + |
| 37 | + In [3]: pd.Series([]).sum() |
| 38 | + Out[3]: nan |
| 39 | + |
| 40 | + In [4]: pd.Series([np.nan]).sum() |
| 41 | + Out[4]: nan |
| 42 | + |
| 43 | +*pandas 0.22.0* |
| 44 | + |
| 45 | +.. ipython:: python |
| 46 | + |
| 47 | + pd.Series([]).sum() |
| 48 | + pd.Series([np.nan]).sum() |
| 49 | + |
| 50 | +The default behavior is the same as pandas 0.20.3 with bottleneck installed. It |
| 51 | +also matches the behavior of ``np.nansum`` and ``np.nanprod`` on empty and |
| 52 | +all-NA arrays. |
| 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 | +Returning ``NaN`` was the default behavior for pandas 0.20.3 without bottleneck |
| 66 | +installed. |
| 67 | + |
| 68 | +Note that this affects some other places in the library: |
| 69 | + |
| 70 | +1. Grouping by a Categorical with some unobserved categories and computing the |
| 71 | +``sum`` / ``prod``. |
| 72 | + |
| 73 | +*pandas 0.21* |
| 74 | + |
| 75 | +.. code-block:: ipython |
| 76 | + |
| 77 | + In [5]: grouper = pd.Categorical(['a', 'a'], categories=['a', 'b']) |
| 78 | + |
| 79 | + In [6]: pd.Series([1, 2]).groupby(grouper).sum() |
| 80 | + Out[6]: |
| 81 | + a 3.0 |
| 82 | + b NaN |
| 83 | + dtype: float64 |
| 84 | + |
| 85 | +*pandas 0.22* |
| 86 | + |
| 87 | +.. ipython:: python |
| 88 | + |
| 89 | + grouper = pd.Categorical(['a', 'a'], categories=['a', 'b']) |
| 90 | + pd.Series([1, 2]).groupby(grouper).sum() |
| 91 | + |
| 92 | + pd.Series([1, 2]).groupby(grouper).sum(min_count=1) |
| 93 | + |
| 94 | +2. Resampling and taking the ``sum`` / ``prod``. |
| 95 | + |
| 96 | +The output for an all-NaN bin will change: |
| 97 | + |
| 98 | +*pandas 0.21.0* |
| 99 | + |
| 100 | +.. code-block:: ipython |
| 101 | + |
| 102 | + In [7]: s = pd.Series([1, 1, np.nan, np.nan], |
| 103 | + ...: index=pd.date_range('2017', periods=4)) |
| 104 | + ...: |
| 105 | + |
| 106 | + In [8]: s |
| 107 | + Out[8]: |
| 108 | + 2017-01-01 1.0 |
| 109 | + 2017-01-02 1.0 |
| 110 | + 2017-01-03 NaN |
| 111 | + 2017-01-04 NaN |
| 112 | + Freq: D, dtype: float64 |
| 113 | + |
| 114 | + In [9]: s.resample('2d').sum() |
| 115 | + Out[9]: |
| 116 | + 2017-01-01 2.0 |
| 117 | + 2017-01-03 NaN |
| 118 | + Freq: 2D, dtype: float64 |
| 119 | + |
| 120 | +*pandas 0.22.0* |
| 121 | + |
| 122 | +.. ipython:: python |
| 123 | + |
| 124 | + s = pd.Series([1, 1, np.nan, np.nan], |
| 125 | + index=pd.date_range('2017', periods=4)) |
| 126 | + s.resample('2d').sum() |
| 127 | + |
| 128 | +To restore the 0.21 behavior of returning ``NaN``, use ``min_count>=1``. |
| 129 | + |
| 130 | +.. ipython:: python |
| 131 | + |
| 132 | + s.resample('2d').sum(min_count=1) |
| 133 | + |
| 134 | +In particular, upsampling and taking the sum or product is affected, as |
| 135 | +upsampling introduces all-NaN series even if your original series was |
| 136 | +entirely valid. |
| 137 | + |
| 138 | +*pandas 0.21.0* |
| 139 | + |
| 140 | +.. code-block:: ipython |
| 141 | + |
| 142 | + In [10]: idx = pd.DatetimeIndex(['2017-01-01', '2017-01-02']) |
| 143 | + |
| 144 | + In [10]: pd.Series([1, 2], index=idx).resample('12H').sum() |
| 145 | + Out[10]: |
| 146 | + 2017-01-01 00:00:00 1.0 |
| 147 | + 2017-01-01 12:00:00 NaN |
| 148 | + 2017-01-02 00:00:00 2.0 |
| 149 | + Freq: 12H, dtype: float64 |
| 150 | + |
| 151 | +*pandas 0.22.0* |
| 152 | + |
| 153 | +.. ipython:: python |
| 154 | + |
| 155 | + idx = pd.DatetimeIndex(['2017-01-01', '2017-01-02']) |
| 156 | + pd.Series([1, 2], index=idx).resample("12H").sum() |
| 157 | + |
| 158 | +Once again, the ``min_count`` keyword is available to restore the 0.21 behavior. |
| 159 | + |
| 160 | + pd.Series([1, 2], index=idx).resample("12H").sum(min_count=1) |
| 161 | + |
| 162 | +3. Rolling / Expanding window operations and taking the ``sum`` / ``prod``. |
| 163 | + |
| 164 | +Rolling and expanding already have a ``min_periods`` keyword that behaves |
| 165 | +similarly to ``min_count``. The only case that changes is when doing a rolling |
| 166 | +or expanding sum on an all-NaN series with ``min_periods=0``. Previously this |
| 167 | +returned ``NaN``, now it will return ``0`` (or ``1`` for ``prod``). |
| 168 | + |
| 169 | +*pandas 0.21.1* |
| 170 | + |
| 171 | +.. ipython:: python |
| 172 | + |
| 173 | + In [11]: s = pd.Series([np.nan, np.nan]) |
| 174 | + |
| 175 | + In [12]: s.rolling(2, min_periods=0).sum() |
| 176 | + Out[12]: |
| 177 | + 0 NaN |
| 178 | + 1 NaN |
| 179 | + dtype: float64 |
| 180 | + |
| 181 | +*pandas 0.22.0* |
| 182 | + |
| 183 | +.. ipython:: python |
| 184 | + |
| 185 | + In [2]: s = pd.Series([np.nan, np.nan]) |
| 186 | + |
| 187 | + In [3]: s.rolling(2, min_periods=0).sum() |
0 commit comments