DEPR: Series.compress
State: closed

xref #18262

Labels:
name: Deprecate
description: Functionality to remove in pandas

Comments:
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/21930?src=pr&el=h1) Report
> Merging [#21930](https://codecov.io/gh/pandas-dev/pandas/pull/21930?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/3e4839301fc2927646889b194c9eb41c62b76bda?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/21930/graphs/tree.svg?height=150&width=650&token=eZ4WkYLtcO&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/21930?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #21930      +/-   ##
==========================================
+ Coverage      92%      92%   +<.01%     
==========================================
  Files         170      170              
  Lines       50609    50611       +2     
==========================================
+ Hits        46561    46563       +2     
  Misses       4048     4048
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.4% <100%> (ø)` | :arrow_up: |
| #single | `42.19% <0%> (-0.01%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/21930?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/series.py](https://codecov.io/gh/pandas-dev/pandas/pull/21930/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvc2VyaWVzLnB5) | `94.11% <100%> (+0.01%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/21930?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/21930?src=pr&el=footer). Last update [3e48393...c228d49](https://codecov.io/gh/pandas-dev/pandas/pull/21930?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

comment by @gfyoung but otherwise, merge away.
thanks @TomAugspurger 
One problem with this PR is that now `np.compress` also raises a warning:

```
In [9]: cond = [True, False, True, False, False]
   ...:   s = Series([1, -1, 5, 8, 7], index=list('abcde'))

In [10]: np.compress(cond, s)
/home/joris/miniconda3/envs/dev/lib/python3.5/site-packages/numpy/core/fromnumeric.py:57: FutureWarning: Series.compress(condition) is deprecated. Use Series[condition] instead.
  return getattr(obj, method)(*args, **kwds)
Out[10]: 
a    1
c    5
Name: foo, dtype: int64
```

so apparently `np.compress` will simply call the method on the passed object if that exists. 

Is there something to do about that? (I suppose not?)

Additional problem is that once we remove the method, `np.compress` will also not work anymore. 

You get something like (with the method commented out):

```
In [2]: np.compress(cond, s)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~/miniconda3/envs/dev/lib/python3.5/site-packages/numpy/core/fromnumeric.py in _wrapfunc(obj, method, *args, **kwds)
     56     try:
---> 57         return getattr(obj, method)(*args, **kwds)
     58 

~/scipy/pandas/pandas/core/generic.py in __getattr__(self, name)
   4557                 return self[name]
-> 4558             return object.__getattribute__(self, name)
   4559 

AttributeError: 'Series' object has no attribute 'compress'

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-2-cb6f12fdb9e8> in <module>()
----> 1 np.compress(cond, s)

~/miniconda3/envs/dev/lib/python3.5/site-packages/numpy/core/fromnumeric.py in compress(condition, a, axis, out)
   1668 
   1669     """
-> 1670     return _wrapfunc(a, 'compress', condition, axis=axis, out=out)
   1671 
   1672 

~/miniconda3/envs/dev/lib/python3.5/site-packages/numpy/core/fromnumeric.py in _wrapfunc(obj, method, *args, **kwds)
     65     # a downstream library like 'pandas'.
     66     except (AttributeError, TypeError):
---> 67         return _wrapit(obj, method, *args, **kwds)
     68 
     69 

~/miniconda3/envs/dev/lib/python3.5/site-packages/numpy/core/fromnumeric.py in _wrapit(obj, method, *args, **kwds)
     49         if not isinstance(result, mu.ndarray):
     50             result = asarray(result)
---> 51         result = wrap(result)
     52     return result
     53 

~/scipy/pandas/pandas/core/series.py in __array_wrap__(self, result, context)
    655         """
    656         return self._constructor(result, index=self.index,
--> 657                                  copy=False).__finalize__(self)
    658 
    659     def __array_prepare__(self, result, context=None):

~/scipy/pandas/pandas/core/series.py in __init__(self, data, index, dtype, name, copy, fastpath)
    264                             'Length of passed values is {val}, '
    265                             'index implies {ind}'
--> 266                             .format(val=len(data), ind=len(index)))
    267                 except TypeError:
    268                     pass

ValueError: Length of passed values is 2, index implies 5
```

which is not a very user friendly error message ..

Of course, if we are fine with `np.compress(series)` not working anymore, then the fact that it raises a deprecation message (my previous comment) is of course fine ..
How about we update the warning message to be

```python
Use `series[condition] or np.asarray(series).compress(condition)`
```