Skip to content

Commit

Permalink
BUG: Partial slicing an datetime MultiIndex (#27127)
Browse files Browse the repository at this point in the history
Fixes GH26944 AttributeError on partial multiindex timestamp slice
  • Loading branch information
soilstack authored and TomAugspurger committed Jul 1, 2019
1 parent 355e322 commit b115a6b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ Indexing
- Bug in which :meth:`DataFrame.to_csv` caused a segfault for a reindexed data frame, when the indices were single-level :class:`MultiIndex` (:issue:`26303`).
- Fixed bug where assigning a :class:`arrays.PandasArray` to a :class:`pandas.core.frame.DataFrame` would raise error (:issue:`26390`)
- Allow keyword arguments for callable local reference used in the :meth:`DataFrame.query` string (:issue:`26426`)

- Bug which produced ``AttributeError`` on partial matching :class:`Timestamp` in a :class:`MultiIndex` (:issue:`26944`)

Missing
^^^^^^^
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2755,7 +2755,9 @@ def convert_indexer(start, stop, step, indexer=indexer,
# a partial date slicer on a DatetimeIndex generates a slice
# note that the stop ALREADY includes the stopped point (if
# it was a string sliced)
return convert_indexer(start.start, stop.stop, step)
start = getattr(start, 'start', start)
stop = getattr(stop, 'stop', stop)
return convert_indexer(start, stop, step)

elif level > 0 or self.lexsort_depth == 0 or step is not None:
# need to have like semantics here to right
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/indexes/multi/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,24 @@ def test_get_indexer_categorical_time():
Categorical(date_range("2012-01-01", periods=3, freq='H'))])
result = midx.get_indexer(midx)
tm.assert_numpy_array_equal(result, np.arange(9, dtype=np.intp))


def test_timestamp_multiindex_indexer():
# https://github.com/pandas-dev/pandas/issues/26944
idx = pd.MultiIndex.from_product([
pd.date_range("2019-01-01T00:15:33", periods=100, freq="H",
name="date"),
['x'],
[3]
])
df = pd.DataFrame({'foo': np.arange(len(idx))}, idx)
result = df.loc[pd.IndexSlice['2019-1-2':, "x", :], 'foo']
qidx = pd.MultiIndex.from_product([
pd.date_range(start="2019-01-02T00:15:33", end='2019-01-05T02:15:33',
freq="H", name="date"),
['x'],
[3]
])
should_be = pd.Series(data=np.arange(24, len(qidx) + 24), index=qidx,
name="foo")
tm.assert_series_equal(result, should_be)

0 comments on commit b115a6b

Please sign in to comment.