Skip to content

Empty TypeError message with bad column selection #12218

Closed
@nbonnotte

Description

@nbonnotte

I was trying to guess, in a dataframe with multi-indexed columns, how to select all columns with a given second-level value.

And then I hit an empty TypeError message

In [8]: df = pd.DataFrame(columns=['i', 'c', 'x', 'y'], data=[[0, 0, 1, 2], [1, 0, 3, 4], [0, 1, 1, 2], [1, 1, 3, 4]])

In [9]: dg
Out[9]: 
   x     y   
c  0  1  0  1
i            
0  1  1  2  2
1  3  3  4  4

In [12]: dg = df.pivot_table(index='i', columns='c', values=['x', 'y'])

n [19]: dg['x', 0]
Out[19]: 
i
0    1
1    3
Name: (x, 0), dtype: int64

In [20]: dg[:, 0]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-20-7eb22191791d> in <module>()
----> 1 dg[:, 0]

/home/nicolas/Git/pandas/pandas/core/frame.py in __getitem__(self, key)
   1977             return self._getitem_frame(key)
   1978         elif is_mi_columns:
-> 1979             return self._getitem_multilevel(key)
   1980         else:
   1981             return self._getitem_column(key)

/home/nicolas/Git/pandas/pandas/core/frame.py in _getitem_multilevel(self, key)
   2021 
   2022     def _getitem_multilevel(self, key):
-> 2023         loc = self.columns.get_loc(key)
   2024         if isinstance(loc, (slice, Series, np.ndarray, Index)):
   2025             new_columns = self.columns[loc]

/home/nicolas/Git/pandas/pandas/indexes/multi.pyc in get_loc(self, key, method)
   1526             key = _values_from_object(key)
   1527             key = tuple(map(_maybe_str_to_time_stamp, key, self.levels))
-> 1528             return self._engine.get_loc(key)
   1529 
   1530         # -- partial selection or non-unique index

/home/nicolas/Git/pandas/pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4005)()
    135             util.set_value_at(arr, loc, value)
    136 
--> 137     cpdef get_loc(self, object val):
    138         if is_definitely_invalid_key(val):
    139             raise TypeError

/home/nicolas/Git/pandas/pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:3617)()
    137     cpdef get_loc(self, object val):
    138         if is_definitely_invalid_key(val):
--> 139             raise TypeError
    140 
    141         if self.over_size_threshold and self.is_monotonic_increasing:

TypeError: 

Metadata

Metadata

Assignees

No one assigned

    Labels

    Error ReportingIncorrect or improved errors from pandasIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions