Skip to content

Commit 8e01026

Browse files
committed
TST: Add the SparseArray constructor performance test
1 parent 26cb4ca commit 8e01026

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

asv_bench/benchmarks/sparse.py

+53-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from .pandas_vb_common import *
44
import scipy.sparse
5-
from pandas import SparseSeries, SparseDataFrame
5+
from pandas import SparseSeries, SparseDataFrame, SparseArray
66

77

88
class sparse_series_to_frame(object):
@@ -23,6 +23,58 @@ def time_sparse_series_to_frame(self):
2323
SparseDataFrame(self.series)
2424

2525

26+
class sparse_array_constructor(object):
27+
goal_time = 0.2
28+
29+
def setup(self):
30+
np.random.seed(1)
31+
self.int64_10percent = self.make_numeric_array(length=1000000, dense_size=100000, fill_value=0, dtype=np.int64)
32+
self.int64_1percent = self.make_numeric_array(length=1000000, dense_size=10000, fill_value=0, dtype=np.int64)
33+
34+
self.float64_10percent = self.make_numeric_array(length=1000000, dense_size=100000, fill_value=np.nan, dtype=np.float64)
35+
self.float64_1percent = self.make_numeric_array(length=1000000, dense_size=10000, fill_value=np.nan, dtype=np.float64)
36+
37+
self.object_10percent = self.make_object_array(length=1000000, dense_size=100000, fill_value=0)
38+
self.object_1percent = self.make_object_array(length=1000000, dense_size=10000, fill_value=0)
39+
40+
def make_numeric_array(self, length, dense_size, fill_value, dtype):
41+
arr = np.array([fill_value] * length, dtype=dtype)
42+
indexer = np.unique(np.random.randint(0, length, dense_size))
43+
arr[indexer] = np.random.randint(0, 100, len(indexer))
44+
return (arr, fill_value, dtype)
45+
46+
def make_object_array(self, length, dense_size, fill_value):
47+
elems = np.array(['a', 0.0, False, 1, 2], dtype=np.object)
48+
arr = np.array([fill_value] * length, dtype=np.object)
49+
indexer = np.unique(np.random.randint(0, length, dense_size))
50+
arr[indexer] = np.random.choice(elems, len(indexer))
51+
return (arr, fill_value, np.object)
52+
53+
def time_sparse_array_constructor_int64_10percent(self):
54+
arr, fill_value, dtype = self.int64_10percent
55+
SparseArray(arr, fill_value=fill_value, dtype=dtype)
56+
57+
def time_sparse_array_constructor_int64_1percent(self):
58+
arr, fill_value, dtype = self.int64_1percent
59+
SparseArray(arr, fill_value=fill_value, dtype=dtype)
60+
61+
def time_sparse_array_constructor_float64_10percent(self):
62+
arr, fill_value, dtype = self.float64_10percent
63+
SparseArray(arr, fill_value=fill_value, dtype=dtype)
64+
65+
def time_sparse_array_constructor_float64_1percent(self):
66+
arr, fill_value, dtype = self.float64_1percent
67+
SparseArray(arr, fill_value=fill_value, dtype=dtype)
68+
69+
def time_sparse_array_constructor_object_10percent(self):
70+
arr, fill_value, dtype = self.object_10percent
71+
SparseArray(arr, fill_value=fill_value, dtype=dtype)
72+
73+
def time_sparse_array_constructor_object_1percent(self):
74+
arr, fill_value, dtype = self.object_1percent
75+
SparseArray(arr, fill_value=fill_value, dtype=dtype)
76+
77+
2678
class sparse_frame_constructor(object):
2779
goal_time = 0.2
2880

0 commit comments

Comments
 (0)