Skip to content

List indexer on PeriodIndex doesn't coerce strings #11278

Closed
@max-sixty

Description

@max-sixty
In [2]: index = pd.period_range(start='2000', periods=20, freq='B')
In [5]: series = pd.Series(range(20), index=index)

In [6]: series
Out[6]: 
2000-01-03     0
2000-01-04     1
2000-01-05     2
...
2000-01-26    17
2000-01-27    18
2000-01-28    19
Freq: B, dtype: int64

In [7]: series.loc['2000-01-14']
Out[7]: 9

Supplying a list of Periods as the indexer works as expected:

In [15]: series[[pd.Period(d, freq='B') for d in ['2000-01-14', '2000-01-18']]]
Out[15]: 
2000-01-14     9
2000-01-18    11
Freq: B, dtype: int64

But not with strings:

In [10]: series.loc[['2000-01-14', '2000-01-15']]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-10-e922e8a9ed74> in <module>()
----> 1 series.loc[['2000-01-14', '2000-01-15']]

/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/core/indexing.py in __getitem__(self, key)
   1187             return self._getitem_tuple(key)
   1188         else:
-> 1189             return self._getitem_axis(key, axis=0)
   1190 
   1191     def _getitem_axis(self, key, axis=0):

/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/core/indexing.py in _getitem_axis(self, key, axis)
   1321                     raise ValueError('Cannot index with multidimensional key')
   1322 
-> 1323                 return self._getitem_iterable(key, axis=axis)
   1324 
   1325             # nested tuple slicing

/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/core/indexing.py in _getitem_iterable(self, key, axis)
    931     def _getitem_iterable(self, key, axis=0):
    932         if self._should_validate_iterable(axis):
--> 933             self._has_valid_type(key, axis)
    934 
    935         labels = self.obj._get_axis(axis)

/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/core/indexing.py in _has_valid_type(self, key, axis)
   1269 
   1270                 raise KeyError("None of [%s] are in the [%s]" %
-> 1271                                (key, self.obj._get_axis_name(axis)))
   1272 
   1273             return True

KeyError: "None of [['2000-01-14', '2000-01-15']] are in the [index]"

It also gives NaN without the loc indexer:

In [16]: series[['2000-01-14', '2000-01-18']]
Out[16]: 
2000-01-14   NaN
2000-01-18   NaN
dtype: float64

When supplied with the ints behind the PeriodIndex, it doesn't resolve a single int, but will resolve a list:

In [4]: index.values
Out[4]: 
array([7827, 7828, 7829, 7830, 7831, 7832, 7833, 7834, 7835, 7836, 7837,
       7838, 7839, 7840, 7841, 7842, 7843, 7844, 7845, 7846])

In [9]: series.loc[[7829, 7830]]
Out[9]: 
7829    2
7830    3
dtype: int64


In [8]: series.loc[7829]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-b6f5fe23a769> in <module>()
----> 1 series.loc[7829]

/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/core/indexing.py in __getitem__(self, key)
   1187             return self._getitem_tuple(key)
   1188         else:
-> 1189             return self._getitem_axis(key, axis=0)
   1190 
   1191     def _getitem_axis(self, key, axis=0):

/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/core/indexing.py in _getitem_axis(self, key, axis)
   1331 
   1332         # fall thru to straight lookup
-> 1333         self._has_valid_type(key, axis)
   1334         return self._get_label(key, axis=axis)
   1335 

/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/core/indexing.py in _has_valid_type(self, key, axis)
   1283 
   1284             try:
-> 1285                 key = self._convert_scalar_indexer(key, axis)
   1286                 if not key in ax:
   1287                     error()

/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/core/indexing.py in _convert_scalar_indexer(self, key, axis)
    161         ax = self.obj._get_axis(min(axis, self.ndim - 1))
    162         # a scalar
--> 163         return ax._convert_scalar_indexer(key, kind=self.name)
    164 
    165     def _convert_slice_indexer(self, key, axis):

/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/tseries/base.py in _convert_scalar_indexer(self, key, kind)
    333 
    334         if kind in ['loc'] and lib.isscalar(key) and (is_integer(key) or is_float(key)):
--> 335             self._invalid_indexer('index',key)
    336 
    337         return super(DatetimeIndexOpsMixin, self)._convert_scalar_indexer(key, kind=kind)

/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pandas/core/index.py in _invalid_indexer(self, form, key)
    942                                                            klass=type(self),
    943                                                            key=key,
--> 944                                                            kind=type(key)))
    945 
    946     def get_duplicates(self):

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselvesPeriodPeriod data type

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions