Skip to content

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

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
SleepingPills opened this issue Sep 17, 2013 · 2 comments · Fixed by #23776
Closed
Labels
Bug good first issue Indexing Related to indexing on series/frames, not to indexes themselves Needs Tests Unit test(s) needed to prevent regressions Period Period data type
Milestone

Comments

@SleepingPills
Copy link
Contributor

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
@SleepingPills
Copy link
Contributor Author

A possibly related issue (happens when using a MultiIndex containing periods), is that when querying with a label that is not in the index, a ValueError("Too many indexers") will be raised instead of a KeyError.

@ghost ghost assigned jtratner Oct 22, 2013
@jreback jreback modified the milestones: 0.15.0, 0.14.0 Mar 30, 2014
@jreback jreback modified the milestones: 0.16.0, Next Major Release Mar 3, 2015
@jbrockmendel
Copy link
Member

Works in 0.23.2, needs test.

@jbrockmendel jbrockmendel added the Needs Tests Unit test(s) needed to prevent regressions label Jul 6, 2018
@datapythonista datapythonista modified the milestones: Contributions Welcome, Someday Jul 8, 2018
@jreback jreback modified the milestones: Someday, 0.24.0 Nov 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug good first issue Indexing Related to indexing on series/frames, not to indexes themselves Needs Tests Unit test(s) needed to prevent regressions Period Period data type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants