Repository: pandas-dev/pandas

Issue:
Transform with np.any / np.all and NA in Group Raises
```python
In [17]: df = pd.DataFrame([['foo', True],                                                          
    ...:                    [np.nan, True],
    ...:                    ['foo', True]], columns=['key', 'val'])

In [13]: df.groupby('key')['val'].transform('any') 
Out[13]: 
0    True
1     NaN
2    True
Name: val, dtype: object

In [14]: df.groupby('key')['val'].transform(np.any)
...
~/clones/pandas/pandas/core/internals.py in set_axis(self, axis, new_labels)
   3280             raise ValueError(
   3281                 'Length mismatch: Expected axis has {old} elements, new '
-> 3282                 'values have {new} elements'.format(old=old_len, new=new_len))
   3283 
   3284         self.axes[axis] = new_labels

ValueError: Length mismatch: Expected axis has 2 elements, new values have 3 elements
```

I've only seen this value error when using np.any and np.all. Other aggs (at least np.sum, np.min, np.max) seem to work fine

<details>


INSTALLED VERSIONS
------------------
commit: fa24af91a156587e7f8d1aab27a45644b59c7e49
python: 3.6.4.final.0
python-bits: 64
OS: Darwin
OS-release: 17.5.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.23.0.dev0+738.gfa24af91a.dirty
pytest: 3.4.1
pip: 9.0.1
setuptools: 38.5.1
Cython: 0.27.3
numpy: 1.14.1
scipy: 1.0.0
pyarrow: 0.8.0
xarray: 0.10.0
IPython: 6.2.1
sphinx: 1.7.0
patsy: 0.5.0
dateutil: 2.6.1
pytz: 2018.3
blosc: None
bottleneck: 1.2.1
tables: 3.4.2
numexpr: 2.6.4
feather: 0.4.0
matplotlib: 2.1.2
openpyxl: 2.5.0
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.2
lxml: 4.1.1
bs4: 4.6.0
html5lib: 1.0.1
sqlalchemy: 1.2.5
pymysql: 0.8.0
psycopg2: 2.7.4 (dt dec pq3 ext lo64)
jinja2: 2.10
s3fs: 0.1.3
fastparquet: 0.1.4
pandas_gbq: None
pandas_datareader: None
</details>


Discussion:
That sounds odd!  Investigation and PR are welcome!

Resolving patch:
diff --git a/pandas/core/base.py b/pandas/core/base.py
--- a/pandas/core/base.py
+++ b/pandas/core/base.py
@@ -188,6 +188,8 @@ class SelectionMixin(object):
         builtins.sum: 'sum',
         builtins.max: 'max',
         builtins.min: 'min',
+        np.all: 'all',
+        np.any: 'any',
         np.sum: 'sum',
         np.mean: 'mean',
         np.prod: 'prod',


Test patch:
diff --git a/pandas/tests/groupby/test_transform.py b/pandas/tests/groupby/test_transform.py
--- a/pandas/tests/groupby/test_transform.py
+++ b/pandas/tests/groupby/test_transform.py
@@ -723,3 +723,15 @@ def get_result(grp_obj):
             exp = DataFrame({'vals': exp_vals * 2})
             result = get_result(grp)
             tm.assert_frame_equal(result, exp)
+
+    @pytest.mark.parametrize("func", [np.any, np.all])
+    def test_any_all_np_func(self, func):
+        # GH 20653
+        df = pd.DataFrame([['foo', True],
+                           [np.nan, True],
+                           ['foo', True]], columns=['key', 'val'])
+
+        exp = pd.Series([True, np.nan, True], name='val')
+
+        res = df.groupby('key')['val'].transform(func)
+        tm.assert_series_equal(res, exp)