Skip to content

Commit f01ba02

Browse files
erbianTomAugspurger
authored andcommitted
BUG: fix missing sort keyword for PeriodIndex.join (#16586)
(cherry picked from commit e9e434d)
1 parent 0a249e2 commit f01ba02

File tree

7 files changed

+51
-8
lines changed

7 files changed

+51
-8
lines changed

doc/source/whatsnew/v0.20.3.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Sparse
8282

8383
Reshaping
8484
^^^^^^^^^
85-
85+
- ``PeriodIndex`` / ``TimedeltaIndex.join`` was missing the ``sort=`` kwarg (:issue:`16541`)
8686
- Bug in joining on a ``MultiIndex`` with a ``category`` dtype for a level (:issue:`16627`).
8787
- Bug in :func:`merge` when merging/joining with multiple categorical columns (:issue:`16767`)
8888

pandas/core/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3129,7 +3129,7 @@ def _join_non_unique(self, other, how='left', return_indexers=False):
31293129
left_idx = _ensure_platform_int(left_idx)
31303130
right_idx = _ensure_platform_int(right_idx)
31313131

3132-
join_index = self.values.take(left_idx)
3132+
join_index = np.asarray(self.values.take(left_idx))
31333133
mask = left_idx == -1
31343134
np.putmask(join_index, mask, other._values.take(right_idx))
31353135

pandas/core/indexes/period.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -919,14 +919,16 @@ def insert(self, loc, item):
919919
self[loc:].asi8))
920920
return self._shallow_copy(idx)
921921

922-
def join(self, other, how='left', level=None, return_indexers=False):
922+
def join(self, other, how='left', level=None, return_indexers=False,
923+
sort=False):
923924
"""
924925
See Index.join
925926
"""
926927
self._assert_can_do_setop(other)
927928

928929
result = Int64Index.join(self, other, how=how, level=level,
929-
return_indexers=return_indexers)
930+
return_indexers=return_indexers,
931+
sort=sort)
930932

931933
if return_indexers:
932934
result, lidx, ridx = result

pandas/core/indexes/timedeltas.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,8 @@ def union(self, other):
516516
result.freq = to_offset(result.inferred_freq)
517517
return result
518518

519-
def join(self, other, how='left', level=None, return_indexers=False):
519+
def join(self, other, how='left', level=None, return_indexers=False,
520+
sort=False):
520521
"""
521522
See Index.join
522523
"""
@@ -527,7 +528,8 @@ def join(self, other, how='left', level=None, return_indexers=False):
527528
pass
528529

529530
return Index.join(self, other, how=how, level=level,
530-
return_indexers=return_indexers)
531+
return_indexers=return_indexers,
532+
sort=sort)
531533

532534
def _wrap_joined_index(self, joined, other):
533535
name = self.name if self.name == other.name else None

pandas/tests/frame/test_join.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@
33
import pytest
44
import numpy as np
55

6-
from pandas import DataFrame, Index
6+
from pandas import DataFrame, Index, PeriodIndex
77
from pandas.tests.frame.common import TestData
88
import pandas.util.testing as tm
99

1010

11+
@pytest.fixture
12+
def frame_with_period_index():
13+
return DataFrame(
14+
data=np.arange(20).reshape(4, 5),
15+
columns=list('abcde'),
16+
index=PeriodIndex(start='2000', freq='A', periods=4))
17+
18+
1119
@pytest.fixture
1220
def frame():
1321
return TestData().frame
@@ -139,3 +147,21 @@ def test_join_overlap(frame):
139147

140148
# column order not necessarily sorted
141149
tm.assert_frame_equal(joined, expected.loc[:, joined.columns])
150+
151+
152+
def test_join_period_index(frame_with_period_index):
153+
other = frame_with_period_index.rename(
154+
columns=lambda x: '{key}{key}'.format(key=x))
155+
156+
joined_values = np.concatenate(
157+
[frame_with_period_index.values] * 2, axis=1)
158+
159+
joined_cols = frame_with_period_index.columns.append(other.columns)
160+
161+
joined = frame_with_period_index.join(other)
162+
expected = DataFrame(
163+
data=joined_values,
164+
columns=joined_cols,
165+
index=frame_with_period_index.index)
166+
167+
tm.assert_frame_equal(joined, expected)

pandas/tests/indexes/common.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ def test_fillna(self):
909909

910910
def test_nulls(self):
911911
# this is really a smoke test for the methods
912-
# as these are adequantely tested for function elsewhere
912+
# as these are adequately tested for function elsewhere
913913

914914
for name, index in self.indices.items():
915915
if len(index) == 0:
@@ -937,3 +937,10 @@ def test_empty(self):
937937
index = self.create_index()
938938
assert not index.empty
939939
assert index[:0].empty
940+
941+
@pytest.mark.parametrize('how', ['outer', 'inner', 'left', 'right'])
942+
def test_join_self_unique(self, how):
943+
index = self.create_index()
944+
if index.is_unique:
945+
joined = index.join(index, how=how)
946+
assert (index == joined).all()

pandas/tests/indexes/period/test_period.py

+6
Original file line numberDiff line numberDiff line change
@@ -773,3 +773,9 @@ def test_map(self):
773773
result = index.map(lambda x: x.ordinal)
774774
exp = Index([x.ordinal for x in index])
775775
tm.assert_index_equal(result, exp)
776+
777+
@pytest.mark.parametrize('how', ['outer', 'inner', 'left', 'right'])
778+
def test_join_self(self, how):
779+
index = period_range('1/1/2000', periods=10)
780+
joined = index.join(index, how=how)
781+
assert index is joined

0 commit comments

Comments
 (0)