Skip to content

Commit

Permalink
Update tests to check sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
reidy-p committed Feb 16, 2019
1 parent 9d9a595 commit 4cd70eb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
4 changes: 3 additions & 1 deletion pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,8 @@ def union(self, other, sort=None):
* False : do not sort the result
.. versionadded:: 0.25.0
Returns
-------
y : Index or DatetimeIndex
Expand Down Expand Up @@ -588,7 +590,7 @@ def _fast_union(self, other, sort=None):
left, right = self, other
# DTIs are not in the "correct" order and we don't want
# to sort but want to remove overlaps
elif sort is not None:
elif sort is False:
left, right = self, other
left_start = left[0]
loc = right.searchsorted(left_start, side='left')
Expand Down
26 changes: 17 additions & 9 deletions pandas/tests/indexes/datetimes/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,33 @@ def test_union(self, tz, sort):
rng1 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz)
other1 = pd.date_range('1/6/2000', freq='D', periods=5, tz=tz)
expected1 = pd.date_range('1/1/2000', freq='D', periods=10, tz=tz)
expected1_notsorted = pd.DatetimeIndex(list(other1) + list(rng1))

rng2 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz)
other2 = pd.date_range('1/4/2000', freq='D', periods=5, tz=tz)
expected2 = pd.date_range('1/1/2000', freq='D', periods=8, tz=tz)
expected2_notsorted = pd.DatetimeIndex(list(other2) + list(rng2[:3]))

rng3 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz)
other3 = pd.DatetimeIndex([], tz=tz)
expected3 = pd.date_range('1/1/2000', freq='D', periods=5, tz=tz)
expected3_notsorted = rng3

for rng, other, expected in [(rng1, other1, expected1),
(rng2, other2, expected2),
(rng3, other3, expected3)]:
for rng, other, exp, exp_notsorted in [(rng1, other1, expected1,
expected1_notsorted),
(rng2, other2, expected2,
expected2_notsorted),
(rng3, other3, expected3,
expected3_notsorted)]:

result_union = rng.union(other, sort=sort)
tm.assert_index_equal(result_union, expected)
tm.assert_index_equal(result_union, exp)

result_union = other.union(rng, sort=sort)
if sort is None:
tm.assert_index_equal(result_union, expected)
tm.assert_index_equal(result_union, exp)
else:
assert tm.equalContents(result_union, expected)
tm.assert_index_equal(result_union, exp_notsorted)

@pytest.mark.parametrize("sort", [None, False])
def test_union_coverage(self, sort):
Expand Down Expand Up @@ -96,7 +102,7 @@ def test_union_bug_1745(self, sort):
'2012-05-11 15:27:24.873000',
'2012-05-11 15:31:05.350000'])
if sort is None:
exp = DatetimeIndex(sorted(set(list(left)) | set(list(right))))
exp = exp.sort_values()
tm.assert_index_equal(result, exp)

@pytest.mark.parametrize("sort", [None, False])
Expand Down Expand Up @@ -336,7 +342,8 @@ def test_union(self, sort):
if sort is None:
tm.assert_index_equal(right.union(left, sort=sort), the_union)
else:
assert tm.equalContents(right.union(left, sort=sort), the_union)
expected = pd.DatetimeIndex(list(right) + list(left))
tm.assert_index_equal(right.union(left, sort=sort), expected)

# overlapping, but different offset
rng = date_range(START, END, freq=BMonthEnd())
Expand Down Expand Up @@ -385,7 +392,8 @@ def test_union_not_cacheable(self, sort):
if sort is None:
tm.assert_index_equal(the_union, rng)
else:
assert tm.equalContents(the_union, rng)
expected = pd.DatetimeIndex(list(rng[10:]) + list(rng[:10]))
tm.assert_index_equal(the_union, expected)

rng1 = rng[10:]
rng2 = rng[15:35]
Expand Down

0 comments on commit 4cd70eb

Please sign in to comment.