Skip to content

Commit 2164af5

Browse files
christopherzimmermanjreback
authored andcommitted
GH28337: Period index doesn't handle reindexing with a non-period index (pandas-dev#28354)
1 parent 07c7510 commit 2164af5

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

doc/source/whatsnew/v1.0.0.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ Indexing
163163
^^^^^^^^
164164

165165
- Bug in assignment using a reverse slicer (:issue:`26939`)
166-
-
166+
- Bug in reindexing a :meth:`PeriodIndex` with another type of index that contained a `Period` (:issue:`28323`) (:issue:`28337`)
167167

168168
Missing
169169
^^^^^^^
@@ -232,7 +232,7 @@ Other
232232
- Trying to set the ``display.precision``, ``display.max_rows`` or ``display.max_columns`` using :meth:`set_option` to anything but a ``None`` or a positive int will raise a ``ValueError`` (:issue:`23348`)
233233
- Using :meth:`DataFrame.replace` with overlapping keys in a nested dictionary will no longer raise, now matching the behavior of a flat dictionary (:issue:`27660`)
234234
- :meth:`DataFrame.to_csv` and :meth:`Series.to_csv` now support dicts as ``compression`` argument with key ``'method'`` being the compression method and others as additional compression options when the compression method is ``'zip'``. (:issue:`26023`)
235-
235+
-
236236

237237
.. _whatsnew_1000.contributors:
238238

pandas/core/indexes/period.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -651,10 +651,13 @@ def get_indexer(self, target, method=None, limit=None, tolerance=None):
651651

652652
if isinstance(target, PeriodIndex):
653653
target = target.asi8
654+
self_index = self._int64index
655+
else:
656+
self_index = self
654657

655658
if tolerance is not None:
656659
tolerance = self._convert_tolerance(tolerance, target)
657-
return Index.get_indexer(self._int64index, target, method, limit, tolerance)
660+
return Index.get_indexer(self_index, target, method, limit, tolerance)
658661

659662
@Appender(_index_shared_docs["get_indexer_non_unique"] % _index_doc_kwargs)
660663
def get_indexer_non_unique(self, target):

pandas/tests/indexes/period/test_period.py

+29
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,35 @@ def test_period_set_index_reindex(self):
354354
df = df.set_index(idx2)
355355
tm.assert_index_equal(df.index, idx2)
356356

357+
@pytest.mark.parametrize(
358+
"p_values, o_values, values, expected_values",
359+
[
360+
(
361+
[Period("2019Q1", "Q-DEC"), Period("2019Q2", "Q-DEC")],
362+
[Period("2019Q1", "Q-DEC"), Period("2019Q2", "Q-DEC"), "All"],
363+
[1.0, 1.0],
364+
[1.0, 1.0, np.nan],
365+
),
366+
(
367+
[Period("2019Q1", "Q-DEC"), Period("2019Q2", "Q-DEC")],
368+
[Period("2019Q1", "Q-DEC"), Period("2019Q2", "Q-DEC")],
369+
[1.0, 1.0],
370+
[1.0, 1.0],
371+
),
372+
],
373+
)
374+
def test_period_reindex_with_object(
375+
self, p_values, o_values, values, expected_values
376+
):
377+
# GH 28337
378+
period_index = PeriodIndex(p_values)
379+
object_index = Index(o_values)
380+
381+
s = pd.Series(values, index=period_index)
382+
result = s.reindex(object_index)
383+
expected = pd.Series(expected_values, index=object_index)
384+
tm.assert_series_equal(result, expected)
385+
357386
def test_factorize(self):
358387
idx1 = PeriodIndex(
359388
["2014-01", "2014-01", "2014-02", "2014-02", "2014-03", "2014-03"], freq="M"

pandas/tests/reshape/test_pivot.py

+26
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,32 @@ def test_pivot_periods(self, method):
677677
pv = pd.pivot(df, index="p1", columns="p2", values="data1")
678678
tm.assert_frame_equal(pv, expected)
679679

680+
def test_pivot_periods_with_margins(self):
681+
# GH 28323
682+
df = DataFrame(
683+
{
684+
"a": [1, 1, 2, 2],
685+
"b": [
686+
pd.Period("2019Q1"),
687+
pd.Period("2019Q2"),
688+
pd.Period("2019Q1"),
689+
pd.Period("2019Q2"),
690+
],
691+
"x": 1.0,
692+
}
693+
)
694+
695+
expected = DataFrame(
696+
data=1.0,
697+
index=pd.Index([1, 2, "All"], name="a"),
698+
columns=pd.Index(
699+
[pd.Period("2019Q1"), pd.Period("2019Q2"), "All"], name="b"
700+
),
701+
)
702+
703+
result = df.pivot_table(index="a", columns="b", values="x", margins=True)
704+
tm.assert_frame_equal(expected, result)
705+
680706
@pytest.mark.parametrize(
681707
"values",
682708
[

0 commit comments

Comments
 (0)