Skip to content

List indexer on PeriodIndex doesn't coerce strings #11278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
max-sixty opened this issue Oct 9, 2015 · 1 comment · Fixed by #30515
Closed

List indexer on PeriodIndex doesn't coerce strings #11278

max-sixty opened this issue Oct 9, 2015 · 1 comment · Fixed by #30515
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Period Period data type
Milestone

Comments

@max-sixty
Copy link
Contributor

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):
@jreback
Copy link
Contributor

jreback commented Oct 9, 2015

dupe of #3462
but will keep this issue

@jreback jreback added Closed PR Bug Indexing Related to indexing on series/frames, not to indexes themselves Period Period data type and removed Closed PR labels Oct 9, 2015
@jreback jreback added this to the 0.17.1 milestone Oct 9, 2015
@jreback jreback modified the milestones: Next Major Release, 0.17.1 Nov 13, 2015
@jreback jreback modified the milestones: Contributions Welcome, 1.1 Apr 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves Period Period data type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants