A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pandas-dev/pandas/issues/6504 below:

KeyError when indexing both axes with .loc[] with duplicate indices · Issue #6504 · pandas-dev/pandas · GitHub

Skip to content Navigation Menu

Saved searches Use saved searches to filter your results more quickly

Sign up You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert Additional navigation options

KeyError when indexing both axes with .loc[] with duplicate indices #6504

Description

The last line of the following code snippet does not work, while it seems it should. Am I overlooking something?

df = pd.DataFrame(np.random.randn(9,2), index=[1,1,1,2,2,2,3,3,3], columns=['a', 'b'])
df
df.loc[[1, 2]]    # works
df.loc[:,['a', 'b']]    # works
df.loc[[1, 2], ['a', 'b']]    # does not work

You don't have this with an index without duplicates.

In [1]: df = pd.DataFrame(np.random.randn(9,2), index=[1,1,1,2,2,2,3,3,3], colum
ns=['a', 'b'])

In [2]: df
Out[2]:
          a         b
1  0.714735 -1.125141
1  0.061066 -0.737312
1  0.269988  1.434862
2  0.336829  1.040681
2  0.929521 -1.142978
2  0.272813 -0.370184
3 -0.520348  1.126233
3  0.232261 -0.075287
3 -0.314570 -2.635400

[9 rows x 2 columns]

In [3]: df.loc[[1, 2]]
Out[3]:
          a         b
1  0.714735 -1.125141
1  0.061066 -0.737312
1  0.269988  1.434862
2  0.336829  1.040681
2  0.929521 -1.142978
2  0.272813 -0.370184

[6 rows x 2 columns]

In [4]: df.loc[:,['a', 'b']]
Out[4]:
          a         b
1  0.714735 -1.125141
1  0.061066 -0.737312
1  0.269988  1.434862
2  0.336829  1.040681
2  0.929521 -1.142978
2  0.272813 -0.370184
3 -0.520348  1.126233
3  0.232261 -0.075287
3 -0.314570 -2.635400

[9 rows x 2 columns]

In [5]: df.loc[[1, 2], ['a', 'b']]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-5-617013ef905a> in <module>()
----> 1 df.loc[[1, 2], ['a', 'b']]

c:\users\vdbosscj\scipy\pandas-joris\pandas\core\indexing.pyc in __getitem__(sel
f, key)
   1152     def __getitem__(self, key):
   1153         if type(key) is tuple:
-> 1154             return self._getitem_tuple(key)
   1155         else:
   1156             return self._getitem_axis(key, axis=0)

c:\users\vdbosscj\scipy\pandas-joris\pandas\core\indexing.pyc in _getitem_tuple(
self, tup)
    680         # ugly hack for GH #836
    681         if self._multi_take_opportunity(tup):
--> 682             return self._multi_take(tup)
    683
    684         # no shortcut needed

c:\users\vdbosscj\scipy\pandas-joris\pandas\core\indexing.pyc in _multi_take(sel
f, tup)
    726             return o.reindex(**d)
    727         except:
--> 728             raise self._exception
    729
    730     def _convert_for_reindex(self, key, axis=0):

KeyError:

You can’t perform that action at this time.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4