8000 ENH: Add in sort keyword to DatetimeIndex.union by reidy-p · Pull Request #25110 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

ENH: Add in sort keyword to DatetimeIndex.union #25110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 23, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
8000
Next Next commit
Update tests to check sorting
  • Loading branch information
reidy-p committed Feb 23, 2019
commit 5b09e716c86d30ce62a7b9f806f05f49716599a4
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a versionaded tag


.. 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