Repository: pandas-dev/pandas

Issue:
BUG: number of keys for grouping equals axis length causing incorrect grouping
#### Code Sample, a copy-pastable example if possible



```python
df = pd.DataFrame([['foo','two', 0.371118],['foo','two', 0.765483]],columns=['first','second','one'])
df.set_index(['first','second'], inplace=True)

df.groupby(['first','second']).size()

df.reset_index().groupby(['first','second']).size()
```
#### Problem description
The output of the first groupby size gives me an output of:
```
first     1
second    1
```
#### Expected Output

rather then the expected output i get after i reset the index:
```
first  second
foo    two       2
```
The problem appears when the level of multiindex is equal the number of rows of the grouped dataframe (tested for 2 and 3 rows = levels). If the number of rows is smaller or bigger then the levels of the multiindex the problem does not occur.

#### Output of ``pd.show_versions()``

<details>
INSTALLED VERSIONS
------------------
commit: None
python: 2.7.13.final.0
python-bits: 64
OS: Darwin
OS-release: 15.4.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None

pandas: 0.20.1
pytest: 3.0.7
pip: 9.0.1
setuptools: 27.2.0
Cython: 0.25.2
numpy: 1.12.1
scipy: 0.19.0
xarray: None
IPython: 5.3.0
sphinx: 1.5.6
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: 1.2.1
tables: 3.3.0
numexpr: 2.6.2
feather: None
matplotlib: 2.0.2
openpyxl: 2.4.7
xlrd: 1.0.0
xlwt: 1.2.0
xlsxwriter: 0.9.6
lxml: 3.7.3
bs4: 4.6.0
html5lib: 0.999
sqlalchemy: 1.1.9
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
pandas_gbq: None
pandas_datareader: None

</details>



Discussion:
so you have a non-unique MultiIndex, which generally isn't as well tested; not to mention is somewhat non-performant and not very useful. That said this looks like a bug.

If you'd dig in an see if you can find the issue would be great.
actually I take this back a bit.

```
In [15]: df.groupby(level=['first', 'second']).size()
Out[15]: 
first  second
foo    two       2
dtype: int64
```

This is explicit, while specifying the names *should* be interpreted the same, I guess its not. This feature was introduced in 0.20.1.
I did some digging  and found that the bug is not related to the non unique multi-index, it rather happens with all dataframes with an equal amount of rows as levels of multi-index (and the grouper not specified as level).
Digging into the code i found that for special cases in groupby.py in line 2640 the keys for the grouping are casted to a tuplesafe array (not really sure what that is) and put into a list as the sole element. Because of that the keys are not enumerated and in_axis is set to false in line 2695 (which should be true i guess).

Removing the outer list in line 2640 fixes the here raised problem but i don't know if it is there for a good reason.

Hope that helps.
well if u can send a PR that adds your example as a test and make your change and see what fails would be great 
I think i found a solution which doesn't crash the groupby tests, but i stil have to run the full test suit and integrate my own test. Hope that i can make a PR tomorrow or the day after.

Thanks for the great work you're putting into pandas!

Resolving patch:
diff --git a/pandas/core/groupby.py b/pandas/core/groupby.py
--- a/pandas/core/groupby.py
+++ b/pandas/core/groupby.py
@@ -2629,13 +2629,14 @@ def _get_grouper(obj, key=None, axis=0, level=None, sort=True,
 
     try:
         if isinstance(obj, DataFrame):
-            all_in_columns = all(g in obj.columns for g in keys)
+            all_in_columns_index = all(g in obj.columns or g in obj.index.names
+                                       for g in keys)
         else:
-            all_in_columns = False
+            all_in_columns_index = False
     except Exception:
-        all_in_columns = False
+        all_in_columns_index = False
 
-    if not any_callable and not all_in_columns and \
+    if not any_callable and not all_in_columns_index and \
        not any_arraylike and not any_groupers and \
        match_axis_length and level is None:
         keys = [com._asarray_tuplesafe(keys)]


Test patch:
diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py
--- a/pandas/tests/groupby/test_groupby.py
+++ b/pandas/tests/groupby/test_groupby.py
@@ -3891,6 +3891,19 @@ def predictions(tool):
         result = df2.groupby('Key').apply(predictions).p1
         tm.assert_series_equal(expected, result)
 
+    def test_gb_key_len_equal_axis_len(self):
+            # GH16843
+            # test ensures that index and column keys are recognized correctly
+            # when number of keys equals axis length of groupby
+            df = pd.DataFrame([['foo', 'bar', 'B', 1],
+                               ['foo', 'bar', 'B', 2],
+                               ['foo', 'baz', 'C', 3]],
+                              columns=['first', 'second', 'third', 'one'])
+            df = df.set_index(['first', 'second'])
+            df = df.groupby(['first', 'second', 'third']).size()
+            assert df.loc[('foo', 'bar', 'B')] == 2
+            assert df.loc[('foo', 'baz', 'C')] == 1
+
 
 def _check_groupby(df, result, keys, field, f=lambda x: x.sum()):
     tups = lmap(tuple, df[keys].values)