Skip to content

Lookup using datetimes does not work with hierarchical indices containing periods #4861

Closed
@SleepingPills

Description

@SleepingPills

Lookup in a PeriodIndex using a datetime works as expected (the period in which the timestamp falls will be returned). However, when the PeriodIndex is part of a hierarchy, this functionality fails in a non-obvious way:

>>> s = pd.Series([1,2,3,4,5], pd.MultiIndex.from_arrays([["a", "a", "a", "b", "b"], pd.period_range("2012-01", periods=5, freq="M")]))
>>> s.loc["a", datetime(2012,1,1)]
Traceback (most recent call last):
  File "C:\VirtualEnvs\test\lib\site-packages\ipython-1.0.dev-py2.6.egg\IPython\core\interactiveshell.py", line 2837, in run_code
    exec code_obj in self.user_global_ns, self.user_ns
  File "<ipython-input-18-9e6cd34eee66>", line 1, in <module>
    a.loc["a", datetime(2012,1,1)]
  File "C:\VirtualEnvs\test\lib\site-packages\pandas-0.12.0-py2.6-win32.egg\pandas\core\indexing.py", line 697, in __getitem__
    return self._getitem_tuple(key)
  File "C:\VirtualEnvs\test\lib\site-packages\pandas-0.12.0-py2.6-win32.egg\pandas\core\indexing.py", line 258, in _getitem_tuple
    self._has_valid_tuple(tup)
  File "C:\VirtualEnvs\test\lib\site-packages\pandas-0.12.0-py2.6-win32.egg\pandas\core\indexing.py", line 691, in _has_valid_tuple
    raise ValueError('Too many indexers')
ValueError: Too many indexers

Using a period works just fine:

>>> s.loc["a", pd.Period("2012-01")]
1

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexingRelated to indexing on series/frames, not to indexes themselvesNeeds TestsUnit test(s) needed to prevent regressionsPeriodPeriod data typegood first issue

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions