Repository: pandas-dev/pandas
Pull request: TST: add test for indexing with single/double tuples
- [x] closes #20991
- [x] tests added / passed
- [x] passes `black pandas`
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`

Problem statement:
Using tuples as dataframe index, containing single entry makes rows impossible to select
#### Code Sample

```python

print "-- Good example --"
df=pd.DataFrame([[tuple([1,2])],[tuple([1])]], columns=['a']).set_index('a')
z=df.index[0]
df.loc[[z]]

print "-- Buggy example --"
z=df.index[1]
df.loc[[z]]
```
Results in:

-- Good example --
      a
(1, 2)

-- Buggy example --

KeyErrorTraceback (most recent call last)
<ipython-input-299-cb90473ac1f9> in <module>()
     10 print "-- Bug example --"
     11 z=df.index[1]
---> 12 df.loc[[z]]

/usr/lib64/python2.7/site-packages/pandas/core/indexing.pyc in __getitem__(self, key)
   1371 
   1372             maybe_callable = com._apply_if_callable(key, self.obj)
-> 1373             return self._getitem_axis(maybe_callable, axis=axis)
   1374 
   1375     def _is_scalar_access(self, key):

/usr/lib64/python2.7/site-packages/pandas/core/indexing.pyc in _getitem_axis(self, key, axis)
   1614                     raise ValueError('Cannot index with multidimensional key')
   1615 
-> 1616                 return self._getitem_iterable(key, axis=axis)
   1617 
   1618             # nested tuple slicing

/usr/lib64/python2.7/site-packages/pandas/core/indexing.pyc in _getitem_iterable(self, key, axis)
   1113 
   1114         if self._should_validate_iterable(axis):
-> 1115             self._has_valid_type(key, axis)
   1116 
   1117         labels = self.obj._get_axis(axis)

/usr/lib64/python2.7/site-packages/pandas/core/indexing.pyc in _has_valid_type(self, key, axis)
   1470                         raise KeyError(
   1471                             u"None of [{key}] are in the [{axis}]".format(
-> 1472                                 key=key, axis=self.obj._get_axis_name(axis)))
   1473                     else:
   1474 

KeyError: u'None of [[(1,)]] are in the [index]'



#### Problem description

I wanted to use tuples as an indexer.
However if the tuple contains only one entry, it makes lines to be impossible to select since it brakes pandas.


#### Expected Output

-- Good example --
      a
(1, 2)

-- Buggy example --
      a
(1, )


#### Output of ``pd.show_versions()``

<details>

[paste the output of ``pd.show_versions()`` here below this line]

INSTALLED VERSIONS
------------------
commit: None
python: 2.7.5.final.0
python-bits: 64
OS: Linux
OS-release: 3.10.0-693.5.2.el7.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: None.None

pandas: 0.22.0
pytest: 3.1.2
pip: 10.0.1
setuptools: None
Cython: 0.24
numpy: 1.14.0
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: 5.6.0
sphinx: 1.6.2
patsy: 0.4.1
dateutil: 2.7.2
pytz: 2017.3
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.0.0
openpyxl: 2.5.0
xlrd: 1.1.0
xlwt: 1.3.0
xlsxwriter: 1.0.2
lxml: 4.1.1
bs4: None
html5lib: 1.0.1
sqlalchemy: 1.0.13
pymysql: None
psycopg2: 2.6.1 (dt dec pq3 ext)
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

</details>

Hints from issue discussion:
If it can be avoided you probably don't want to be using tuples as indexes - it won't be as performant and isn't idiomatic - inference causes odd stuff like this to happen.

That said you are welcome to have a look and if you can fix this case without impacting existing tests, would certainly take a PR.
This works correctly now, but probably isn't tested

Commit message: remove pd from pd.DataFrame

Patch to review:
diff --git a/pandas/tests/frame/test_indexing.py b/pandas/tests/frame/test_indexing.py
index e215c90d2eb04..d21700cdcd09c 100644
--- a/pandas/tests/frame/test_indexing.py
+++ b/pandas/tests/frame/test_indexing.py
@@ -2626,6 +2626,20 @@ def test_index_namedtuple(self):
         result = df.loc[IndexType("foo", "bar")]["A"]
         assert result == 1
 
+    def test_index_single_double_tuples(self):
+        # GH 20991
+        tuple_1 = tuple([1, 2])
+        tuple_2 = tuple([1])
+        df = DataFrame([[tuple_1], [tuple_2]], columns=["A"]).set_index("A")
+
+        result = df.loc[[df.index[0]]]
+        expected = DataFrame([[tuple_1]], columns=["A"]).set_index("A")
+        tm.assert_frame_equal(result, expected)
+
+        result = df.loc[[df.index[1]]]
+        expected = DataFrame([[tuple_2]], columns=["A"]).set_index("A")
+        tm.assert_frame_equal(result, expected)
+
     def test_boolean_indexing(self):
         idx = list(range(3))
         cols = ["A", "B", "C"]


Code review comments:
diff_hunk: @@ -2626,6 +2626,20 @@ def test_index_namedtuple(self):
         result = df.loc[IndexType("foo", "bar")]["A"]
         assert result == 1
 
+    def test_index_single_double_tuples(self):
+        # GH 20991
+        tuple_1 = tuple([1, 2])
+        tuple_2 = tuple([1])
+        df = DataFrame([[tuple_1], [tuple_2]], columns=["A"]).set_index("A")
path: pandas/tests/frame/test_indexing.py
text: @user1:
Can you just construct `idx = pd.Index(...)` before this and use `index=idx` as part of the DataFrame constructors? Would be more concise than the current construction / set_index ops

@author:
Thanks @user1 - removed `.set_index()`

diff_hunk: @@ -2626,6 +2626,20 @@ def test_index_namedtuple(self):
         result = df.loc[IndexType("foo", "bar")]["A"]
         assert result == 1
 
+    def test_index_single_double_tuples(self):
+        # GH 20991
+        tuple_1 = tuple([1, 2])
+        tuple_2 = tuple([1])
+        df = DataFrame([[tuple_1], [tuple_2]], columns=["A"]).set_index("A")
+
+        result = df.loc[[df.index[0]]]
path: pandas/tests/frame/test_indexing.py
text: @user1:
`df.loc[tuple_1]`; if you can avoid `df.index[...]` calls here will make the test more concise, as accessing index values like that isn't related to the test

@author:
Thanks @user1  - removed `.index[...]`