goedi
goedi

Reputation: 2083

Error when selecting only one column with df.iloc in pandas

Given the DataFrame below

In [5]: dates = pd.date_range('20130101', periods=6)

In [6]: dates
Out[6]: 
DatetimeIndex(['2013-01-01', '2013-01-02', '2013-01-03', '2013-01-04',
               '2013-01-05', '2013-01-06'],
              dtype='datetime64[ns]', freq='D')

In [7]: df = pd.DataFrame(np.random.randn(6, 4), index=dates, columns=list('ABCD'))

In [8]: df
Out[8]: 
                   A         B         C         D
2013-01-01  0.469112 -0.282863 -1.509059 -1.135632
2013-01-02  1.212112 -0.173215  0.119209 -1.044236
2013-01-03 -0.861849 -2.104569 -0.494929  1.071804
2013-01-04  0.721555 -0.706771 -1.039575  0.271860
2013-01-05 -0.424972  0.567020  0.276232 -1.087401
2013-01-06 -0.673690  0.113648 -1.478427  0.524988

The result of df.loc['20130101':'20130102', 'A'] is

2013-01-01   0.469112
2013-01-02   1.212112
Freq: D, Name: A, dtype: float64

But the result of df.iloc[0:1, 'A'] is the following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py in _has_valid_tuple(self, key)
    221             try:
--> 222                 self._validate_key(k, i)
    223             except ValueError:

/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py in _validate_key(self, key, axis)
   1970             raise ValueError("Can only index by location with "
-> 1971                              "a [{types}]".format(types=self._valid_types))
   1972 

ValueError: Can only index by location with a [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array]

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-73-c05fbef69c91> in <module>()
----> 1 df.iloc[0:1, 'A']

/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py in __getitem__(self, key)
   1470             except (KeyError, IndexError):
   1471                 pass
-> 1472             return self._getitem_tuple(key)
   1473         else:
   1474             # we by definition only have the 0th axis

/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py in _getitem_tuple(self, tup)
   2011     def _getitem_tuple(self, tup):
   2012 
-> 2013         self._has_valid_tuple(tup)
   2014         try:
   2015             return self._getitem_lowerdim(tup)

/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py in _has_valid_tuple(self, key)
    224                 raise ValueError("Location based indexing can only have "
    225                                  "[{types}] types"
--> 226                                  .format(types=self._valid_types))
    227 
    228     def _is_nested_tuple_indexer(self, tup):

ValueError: Location based indexing can only have [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array] types

why?

Upvotes: 1

Views: 988

Answers (1)

jezrael
jezrael

Reputation: 862511

Because if select by positions, all values has to be positions in iloc.

So use Index.get_loc for position of A column, so selected by postions by index and also by columns:

df.iloc[0:1, df.columns.get_loc('A')]

Upvotes: 1

Related Questions