Skip to content

Commit f62e279

Browse files
committed
ENH: Series.sort_index perf enhancement
1 parent 56e0c9f commit f62e279

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

bench/zoo_bench.R

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
library(zoo)
2+
library(xts)
23

34
indices = rep(NA, 100000)
45
for (i in 1:100000)
56
indices[i] <- paste(sample(letters, 10), collapse="")
67

78
timings <- numeric()
89

9-
x <- zoo(rnorm(100000), indices)
10-
y <- zoo(rnorm(90000), indices[sample(1:100000, 90000)])
10+
## x <- zoo(rnorm(100000), indices)
11+
## y <- zoo(rnorm(90000), indices[sample(1:100000, 90000)])
12+
13+
## indices <- as.POSIXct(1:100000)
14+
15+
indices <- as.POSIXct(Sys.Date()) + 1:100000
16+
17+
x <- xts(rnorm(100000), indices)
18+
y <- xts(rnorm(90000), indices[sample(1:100000, 90000)])
1119

1220
for (i in 1:10) {
1321
gc()

pandas/core/index.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -257,15 +257,14 @@ def union(self, other):
257257
if len(indexer) > 0:
258258
other_diff = other.values.take(indexer)
259259
result = list(self) + list(other_diff)
260+
# timsort wins
261+
try:
262+
result.sort()
263+
except Exception:
264+
pass
260265
else:
261266
# contained in
262-
result = list(self)
263-
264-
# timsort wins
265-
try:
266-
result.sort()
267-
except Exception:
268-
pass
267+
result = sorted(self)
269268

270269
# for subclasses
271270
return self._wrap_union_result(other, result)

pandas/core/series.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,8 @@ def sort_index(self, ascending=True):
10571057
if not ascending:
10581058
sort_index = sort_index[::-1]
10591059
new_labels = labels.take(sort_index)
1060-
return self.reindex(new_labels)
1060+
new_values = self.values.take(sort_index)
1061+
return Series(new_values, new_labels)
10611062

10621063
def argsort(self, axis=0, kind='quicksort', order=None):
10631064
"""

0 commit comments

Comments
 (0)