Skip to content

Commit b115a6b

Browse files
soilstackTomAugspurger
authored andcommitted
BUG: Partial slicing an datetime MultiIndex (#27127)
Fixes GH26944 AttributeError on partial multiindex timestamp slice
1 parent 355e322 commit b115a6b

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

doc/source/whatsnew/v0.25.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ Indexing
773773
- 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`).
774774
- Fixed bug where assigning a :class:`arrays.PandasArray` to a :class:`pandas.core.frame.DataFrame` would raise error (:issue:`26390`)
775775
- Allow keyword arguments for callable local reference used in the :meth:`DataFrame.query` string (:issue:`26426`)
776-
776+
- Bug which produced ``AttributeError`` on partial matching :class:`Timestamp` in a :class:`MultiIndex` (:issue:`26944`)
777777

778778
Missing
779779
^^^^^^^

pandas/core/indexes/multi.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -2755,7 +2755,9 @@ def convert_indexer(start, stop, step, indexer=indexer,
27552755
# a partial date slicer on a DatetimeIndex generates a slice
27562756
# note that the stop ALREADY includes the stopped point (if
27572757
# it was a string sliced)
2758-
return convert_indexer(start.start, stop.stop, step)
2758+
start = getattr(start, 'start', start)
2759+
stop = getattr(stop, 'stop', stop)
2760+
return convert_indexer(start, stop, step)
27592761

27602762
elif level > 0 or self.lexsort_depth == 0 or step is not None:
27612763
# need to have like semantics here to right

pandas/tests/indexes/multi/test_indexing.py

+21
Original file line numberDiff line numberDiff line change
@@ -397,3 +397,24 @@ def test_get_indexer_categorical_time():
397397
Categorical(date_range("2012-01-01", periods=3, freq='H'))])
398398
result = midx.get_indexer(midx)
399399
tm.assert_numpy_array_equal(result, np.arange(9, dtype=np.intp))
400+
401+
402+
def test_timestamp_multiindex_indexer():
403+
# https://github.com/pandas-dev/pandas/issues/26944
404+
idx = pd.MultiIndex.from_product([
405+
pd.date_range("2019-01-01T00:15:33", periods=100, freq="H",
406+
name="date"),
407+
['x'],
408+
[3]
409+
])
410+
df = pd.DataFrame({'foo': np.arange(len(idx))}, idx)
411+
result = df.loc[pd.IndexSlice['2019-1-2':, "x", :], 'foo']
412+
qidx = pd.MultiIndex.from_product([
413+
pd.date_range(start="2019-01-02T00:15:33", end='2019-01-05T02:15:33',
414+
freq="H", name="date"),
415+
['x'],
416+
[3]
417+
])
418+
should_be = pd.Series(data=np.arange(24, len(qidx) + 24), index=qidx,
419+
name="foo")
420+
tm.assert_series_equal(result, should_be)

0 commit comments

Comments
 (0)