test_libsparse.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. import operator
  2. import numpy as np
  3. import pytest
  4. import pandas._libs.sparse as splib
  5. import pandas.util._test_decorators as td
  6. from pandas import Series
  7. from pandas.core.arrays.sparse import BlockIndex, IntIndex, _make_index
  8. import pandas.util.testing as tm
  9. TEST_LENGTH = 20
  10. plain_case = dict(xloc=[0, 7, 15], xlen=[3, 5, 5], yloc=[2, 9, 14],
  11. ylen=[2, 3, 5], intersect_loc=[2, 9, 15],
  12. intersect_len=[1, 3, 4])
  13. delete_blocks = dict(xloc=[0, 5], xlen=[4, 4], yloc=[1], ylen=[4],
  14. intersect_loc=[1], intersect_len=[3])
  15. split_blocks = dict(xloc=[0], xlen=[10], yloc=[0, 5], ylen=[3, 7],
  16. intersect_loc=[0, 5], intersect_len=[3, 5])
  17. skip_block = dict(xloc=[10], xlen=[5], yloc=[0, 12], ylen=[5, 3],
  18. intersect_loc=[12], intersect_len=[3])
  19. no_intersect = dict(xloc=[0, 10], xlen=[4, 6], yloc=[5, 17], ylen=[4, 2],
  20. intersect_loc=[], intersect_len=[])
  21. def check_cases(_check_case):
  22. def _check_case_dict(case):
  23. _check_case(case['xloc'], case['xlen'], case['yloc'], case['ylen'],
  24. case['intersect_loc'], case['intersect_len'])
  25. _check_case_dict(plain_case)
  26. _check_case_dict(delete_blocks)
  27. _check_case_dict(split_blocks)
  28. _check_case_dict(skip_block)
  29. _check_case_dict(no_intersect)
  30. # one or both is empty
  31. _check_case([0], [5], [], [], [], [])
  32. _check_case([], [], [], [], [], [])
  33. class TestSparseIndexUnion(object):
  34. def test_index_make_union(self):
  35. def _check_case(xloc, xlen, yloc, ylen, eloc, elen):
  36. xindex = BlockIndex(TEST_LENGTH, xloc, xlen)
  37. yindex = BlockIndex(TEST_LENGTH, yloc, ylen)
  38. bresult = xindex.make_union(yindex)
  39. assert (isinstance(bresult, BlockIndex))
  40. tm.assert_numpy_array_equal(bresult.blocs,
  41. np.array(eloc, dtype=np.int32))
  42. tm.assert_numpy_array_equal(bresult.blengths,
  43. np.array(elen, dtype=np.int32))
  44. ixindex = xindex.to_int_index()
  45. iyindex = yindex.to_int_index()
  46. iresult = ixindex.make_union(iyindex)
  47. assert (isinstance(iresult, IntIndex))
  48. tm.assert_numpy_array_equal(iresult.indices,
  49. bresult.to_int_index().indices)
  50. """
  51. x: ----
  52. y: ----
  53. r: --------
  54. """
  55. xloc = [0]
  56. xlen = [5]
  57. yloc = [5]
  58. ylen = [4]
  59. eloc = [0]
  60. elen = [9]
  61. _check_case(xloc, xlen, yloc, ylen, eloc, elen)
  62. """
  63. x: ----- -----
  64. y: ----- --
  65. """
  66. xloc = [0, 10]
  67. xlen = [5, 5]
  68. yloc = [2, 17]
  69. ylen = [5, 2]
  70. eloc = [0, 10, 17]
  71. elen = [7, 5, 2]
  72. _check_case(xloc, xlen, yloc, ylen, eloc, elen)
  73. """
  74. x: ------
  75. y: -------
  76. r: ----------
  77. """
  78. xloc = [1]
  79. xlen = [5]
  80. yloc = [3]
  81. ylen = [5]
  82. eloc = [1]
  83. elen = [7]
  84. _check_case(xloc, xlen, yloc, ylen, eloc, elen)
  85. """
  86. x: ------ -----
  87. y: -------
  88. r: -------------
  89. """
  90. xloc = [2, 10]
  91. xlen = [4, 4]
  92. yloc = [4]
  93. ylen = [8]
  94. eloc = [2]
  95. elen = [12]
  96. _check_case(xloc, xlen, yloc, ylen, eloc, elen)
  97. """
  98. x: --- -----
  99. y: -------
  100. r: -------------
  101. """
  102. xloc = [0, 5]
  103. xlen = [3, 5]
  104. yloc = [0]
  105. ylen = [7]
  106. eloc = [0]
  107. elen = [10]
  108. _check_case(xloc, xlen, yloc, ylen, eloc, elen)
  109. """
  110. x: ------ -----
  111. y: ------- ---
  112. r: -------------
  113. """
  114. xloc = [2, 10]
  115. xlen = [4, 4]
  116. yloc = [4, 13]
  117. ylen = [8, 4]
  118. eloc = [2]
  119. elen = [15]
  120. _check_case(xloc, xlen, yloc, ylen, eloc, elen)
  121. """
  122. x: ----------------------
  123. y: ---- ---- ---
  124. r: ----------------------
  125. """
  126. xloc = [2]
  127. xlen = [15]
  128. yloc = [4, 9, 14]
  129. ylen = [3, 2, 2]
  130. eloc = [2]
  131. elen = [15]
  132. _check_case(xloc, xlen, yloc, ylen, eloc, elen)
  133. """
  134. x: ---- ---
  135. y: --- ---
  136. """
  137. xloc = [0, 10]
  138. xlen = [3, 3]
  139. yloc = [5, 15]
  140. ylen = [2, 2]
  141. eloc = [0, 5, 10, 15]
  142. elen = [3, 2, 3, 2]
  143. _check_case(xloc, xlen, yloc, ylen, eloc, elen)
  144. def test_intindex_make_union(self):
  145. a = IntIndex(5, np.array([0, 3, 4], dtype=np.int32))
  146. b = IntIndex(5, np.array([0, 2], dtype=np.int32))
  147. res = a.make_union(b)
  148. exp = IntIndex(5, np.array([0, 2, 3, 4], np.int32))
  149. assert res.equals(exp)
  150. a = IntIndex(5, np.array([], dtype=np.int32))
  151. b = IntIndex(5, np.array([0, 2], dtype=np.int32))
  152. res = a.make_union(b)
  153. exp = IntIndex(5, np.array([0, 2], np.int32))
  154. assert res.equals(exp)
  155. a = IntIndex(5, np.array([], dtype=np.int32))
  156. b = IntIndex(5, np.array([], dtype=np.int32))
  157. res = a.make_union(b)
  158. exp = IntIndex(5, np.array([], np.int32))
  159. assert res.equals(exp)
  160. a = IntIndex(5, np.array([0, 1, 2, 3, 4], dtype=np.int32))
  161. b = IntIndex(5, np.array([0, 1, 2, 3, 4], dtype=np.int32))
  162. res = a.make_union(b)
  163. exp = IntIndex(5, np.array([0, 1, 2, 3, 4], np.int32))
  164. assert res.equals(exp)
  165. a = IntIndex(5, np.array([0, 1], dtype=np.int32))
  166. b = IntIndex(4, np.array([0, 1], dtype=np.int32))
  167. with pytest.raises(ValueError):
  168. a.make_union(b)
  169. class TestSparseIndexIntersect(object):
  170. @td.skip_if_windows
  171. def test_intersect(self):
  172. def _check_correct(a, b, expected):
  173. result = a.intersect(b)
  174. assert (result.equals(expected))
  175. def _check_length_exc(a, longer):
  176. pytest.raises(Exception, a.intersect, longer)
  177. def _check_case(xloc, xlen, yloc, ylen, eloc, elen):
  178. xindex = BlockIndex(TEST_LENGTH, xloc, xlen)
  179. yindex = BlockIndex(TEST_LENGTH, yloc, ylen)
  180. expected = BlockIndex(TEST_LENGTH, eloc, elen)
  181. longer_index = BlockIndex(TEST_LENGTH + 1, yloc, ylen)
  182. _check_correct(xindex, yindex, expected)
  183. _check_correct(xindex.to_int_index(), yindex.to_int_index(),
  184. expected.to_int_index())
  185. _check_length_exc(xindex, longer_index)
  186. _check_length_exc(xindex.to_int_index(),
  187. longer_index.to_int_index())
  188. check_cases(_check_case)
  189. def test_intersect_empty(self):
  190. xindex = IntIndex(4, np.array([], dtype=np.int32))
  191. yindex = IntIndex(4, np.array([2, 3], dtype=np.int32))
  192. assert xindex.intersect(yindex).equals(xindex)
  193. assert yindex.intersect(xindex).equals(xindex)
  194. xindex = xindex.to_block_index()
  195. yindex = yindex.to_block_index()
  196. assert xindex.intersect(yindex).equals(xindex)
  197. assert yindex.intersect(xindex).equals(xindex)
  198. def test_intersect_identical(self):
  199. cases = [IntIndex(5, np.array([1, 2], dtype=np.int32)),
  200. IntIndex(5, np.array([0, 2, 4], dtype=np.int32)),
  201. IntIndex(0, np.array([], dtype=np.int32)),
  202. IntIndex(5, np.array([], dtype=np.int32))]
  203. for case in cases:
  204. assert case.intersect(case).equals(case)
  205. case = case.to_block_index()
  206. assert case.intersect(case).equals(case)
  207. class TestSparseIndexCommon(object):
  208. def test_int_internal(self):
  209. idx = _make_index(4, np.array([2, 3], dtype=np.int32), kind='integer')
  210. assert isinstance(idx, IntIndex)
  211. assert idx.npoints == 2
  212. tm.assert_numpy_array_equal(idx.indices,
  213. np.array([2, 3], dtype=np.int32))
  214. idx = _make_index(4, np.array([], dtype=np.int32), kind='integer')
  215. assert isinstance(idx, IntIndex)
  216. assert idx.npoints == 0
  217. tm.assert_numpy_array_equal(idx.indices,
  218. np.array([], dtype=np.int32))
  219. idx = _make_index(4, np.array([0, 1, 2, 3], dtype=np.int32),
  220. kind='integer')
  221. assert isinstance(idx, IntIndex)
  222. assert idx.npoints == 4
  223. tm.assert_numpy_array_equal(idx.indices,
  224. np.array([0, 1, 2, 3], dtype=np.int32))
  225. def test_block_internal(self):
  226. idx = _make_index(4, np.array([2, 3], dtype=np.int32), kind='block')
  227. assert isinstance(idx, BlockIndex)
  228. assert idx.npoints == 2
  229. tm.assert_numpy_array_equal(idx.blocs,
  230. np.array([2], dtype=np.int32))
  231. tm.assert_numpy_array_equal(idx.blengths,
  232. np.array([2], dtype=np.int32))
  233. idx = _make_index(4, np.array([], dtype=np.int32), kind='block')
  234. assert isinstance(idx, BlockIndex)
  235. assert idx.npoints == 0
  236. tm.assert_numpy_array_equal(idx.blocs,
  237. np.array([], dtype=np.int32))
  238. tm.assert_numpy_array_equal(idx.blengths,
  239. np.array([], dtype=np.int32))
  240. idx = _make_index(4, np.array([0, 1, 2, 3], dtype=np.int32),
  241. kind='block')
  242. assert isinstance(idx, BlockIndex)
  243. assert idx.npoints == 4
  244. tm.assert_numpy_array_equal(idx.blocs,
  245. np.array([0], dtype=np.int32))
  246. tm.assert_numpy_array_equal(idx.blengths,
  247. np.array([4], dtype=np.int32))
  248. idx = _make_index(4, np.array([0, 2, 3], dtype=np.int32),
  249. kind='block')
  250. assert isinstance(idx, BlockIndex)
  251. assert idx.npoints == 3
  252. tm.assert_numpy_array_equal(idx.blocs,
  253. np.array([0, 2], dtype=np.int32))
  254. tm.assert_numpy_array_equal(idx.blengths,
  255. np.array([1, 2], dtype=np.int32))
  256. def test_lookup(self):
  257. for kind in ['integer', 'block']:
  258. idx = _make_index(4, np.array([2, 3], dtype=np.int32), kind=kind)
  259. assert idx.lookup(-1) == -1
  260. assert idx.lookup(0) == -1
  261. assert idx.lookup(1) == -1
  262. assert idx.lookup(2) == 0
  263. assert idx.lookup(3) == 1
  264. assert idx.lookup(4) == -1
  265. idx = _make_index(4, np.array([], dtype=np.int32), kind=kind)
  266. for i in range(-1, 5):
  267. assert idx.lookup(i) == -1
  268. idx = _make_index(4, np.array([0, 1, 2, 3], dtype=np.int32),
  269. kind=kind)
  270. assert idx.lookup(-1) == -1
  271. assert idx.lookup(0) == 0
  272. assert idx.lookup(1) == 1
  273. assert idx.lookup(2) == 2
  274. assert idx.lookup(3) == 3
  275. assert idx.lookup(4) == -1
  276. idx = _make_index(4, np.array([0, 2, 3], dtype=np.int32),
  277. kind=kind)
  278. assert idx.lookup(-1) == -1
  279. assert idx.lookup(0) == 0
  280. assert idx.lookup(1) == -1
  281. assert idx.lookup(2) == 1
  282. assert idx.lookup(3) == 2
  283. assert idx.lookup(4) == -1
  284. def test_lookup_array(self):
  285. for kind in ['integer', 'block']:
  286. idx = _make_index(4, np.array([2, 3], dtype=np.int32), kind=kind)
  287. res = idx.lookup_array(np.array([-1, 0, 2], dtype=np.int32))
  288. exp = np.array([-1, -1, 0], dtype=np.int32)
  289. tm.assert_numpy_array_equal(res, exp)
  290. res = idx.lookup_array(np.array([4, 2, 1, 3], dtype=np.int32))
  291. exp = np.array([-1, 0, -1, 1], dtype=np.int32)
  292. tm.assert_numpy_array_equal(res, exp)
  293. idx = _make_index(4, np.array([], dtype=np.int32), kind=kind)
  294. res = idx.lookup_array(np.array([-1, 0, 2, 4], dtype=np.int32))
  295. exp = np.array([-1, -1, -1, -1], dtype=np.int32)
  296. idx = _make_index(4, np.array([0, 1, 2, 3], dtype=np.int32),
  297. kind=kind)
  298. res = idx.lookup_array(np.array([-1, 0, 2], dtype=np.int32))
  299. exp = np.array([-1, 0, 2], dtype=np.int32)
  300. tm.assert_numpy_array_equal(res, exp)
  301. res = idx.lookup_array(np.array([4, 2, 1, 3], dtype=np.int32))
  302. exp = np.array([-1, 2, 1, 3], dtype=np.int32)
  303. tm.assert_numpy_array_equal(res, exp)
  304. idx = _make_index(4, np.array([0, 2, 3], dtype=np.int32),
  305. kind=kind)
  306. res = idx.lookup_array(np.array([2, 1, 3, 0], dtype=np.int32))
  307. exp = np.array([1, -1, 2, 0], dtype=np.int32)
  308. tm.assert_numpy_array_equal(res, exp)
  309. res = idx.lookup_array(np.array([1, 4, 2, 5], dtype=np.int32))
  310. exp = np.array([-1, -1, 1, -1], dtype=np.int32)
  311. tm.assert_numpy_array_equal(res, exp)
  312. def test_lookup_basics(self):
  313. def _check(index):
  314. assert (index.lookup(0) == -1)
  315. assert (index.lookup(5) == 0)
  316. assert (index.lookup(7) == 2)
  317. assert (index.lookup(8) == -1)
  318. assert (index.lookup(9) == -1)
  319. assert (index.lookup(10) == -1)
  320. assert (index.lookup(11) == -1)
  321. assert (index.lookup(12) == 3)
  322. assert (index.lookup(17) == 8)
  323. assert (index.lookup(18) == -1)
  324. bindex = BlockIndex(20, [5, 12], [3, 6])
  325. iindex = bindex.to_int_index()
  326. _check(bindex)
  327. _check(iindex)
  328. # corner cases
  329. class TestBlockIndex(object):
  330. def test_block_internal(self):
  331. idx = _make_index(4, np.array([2, 3], dtype=np.int32), kind='block')
  332. assert isinstance(idx, BlockIndex)
  333. assert idx.npoints == 2
  334. tm.assert_numpy_array_equal(idx.blocs,
  335. np.array([2], dtype=np.int32))
  336. tm.assert_numpy_array_equal(idx.blengths,
  337. np.array([2], dtype=np.int32))
  338. idx = _make_index(4, np.array([], dtype=np.int32), kind='block')
  339. assert isinstance(idx, BlockIndex)
  340. assert idx.npoints == 0
  341. tm.assert_numpy_array_equal(idx.blocs,
  342. np.array([], dtype=np.int32))
  343. tm.assert_numpy_array_equal(idx.blengths,
  344. np.array([], dtype=np.int32))
  345. idx = _make_index(4, np.array([0, 1, 2, 3], dtype=np.int32),
  346. kind='block')
  347. assert isinstance(idx, BlockIndex)
  348. assert idx.npoints == 4
  349. tm.assert_numpy_array_equal(idx.blocs,
  350. np.array([0], dtype=np.int32))
  351. tm.assert_numpy_array_equal(idx.blengths,
  352. np.array([4], dtype=np.int32))
  353. idx = _make_index(4, np.array([0, 2, 3], dtype=np.int32), kind='block')
  354. assert isinstance(idx, BlockIndex)
  355. assert idx.npoints == 3
  356. tm.assert_numpy_array_equal(idx.blocs,
  357. np.array([0, 2], dtype=np.int32))
  358. tm.assert_numpy_array_equal(idx.blengths,
  359. np.array([1, 2], dtype=np.int32))
  360. def test_make_block_boundary(self):
  361. for i in [5, 10, 100, 101]:
  362. idx = _make_index(i, np.arange(0, i, 2, dtype=np.int32),
  363. kind='block')
  364. exp = np.arange(0, i, 2, dtype=np.int32)
  365. tm.assert_numpy_array_equal(idx.blocs, exp)
  366. tm.assert_numpy_array_equal(idx.blengths,
  367. np.ones(len(exp), dtype=np.int32))
  368. def test_equals(self):
  369. index = BlockIndex(10, [0, 4], [2, 5])
  370. assert index.equals(index)
  371. assert not index.equals(BlockIndex(10, [0, 4], [2, 6]))
  372. def test_check_integrity(self):
  373. locs = []
  374. lengths = []
  375. # 0-length OK
  376. # TODO: index variables are not used...is that right?
  377. index = BlockIndex(0, locs, lengths) # noqa
  378. # also OK even though empty
  379. index = BlockIndex(1, locs, lengths) # noqa
  380. # block extend beyond end
  381. pytest.raises(Exception, BlockIndex, 10, [5], [10])
  382. # block overlap
  383. pytest.raises(Exception, BlockIndex, 10, [2, 5], [5, 3])
  384. def test_to_int_index(self):
  385. locs = [0, 10]
  386. lengths = [4, 6]
  387. exp_inds = [0, 1, 2, 3, 10, 11, 12, 13, 14, 15]
  388. block = BlockIndex(20, locs, lengths)
  389. dense = block.to_int_index()
  390. tm.assert_numpy_array_equal(dense.indices,
  391. np.array(exp_inds, dtype=np.int32))
  392. def test_to_block_index(self):
  393. index = BlockIndex(10, [0, 5], [4, 5])
  394. assert index.to_block_index() is index
  395. class TestIntIndex(object):
  396. def test_check_integrity(self):
  397. # Too many indices than specified in self.length
  398. msg = "Too many indices"
  399. with pytest.raises(ValueError, match=msg):
  400. IntIndex(length=1, indices=[1, 2, 3])
  401. # No index can be negative.
  402. msg = "No index can be less than zero"
  403. with pytest.raises(ValueError, match=msg):
  404. IntIndex(length=5, indices=[1, -2, 3])
  405. # No index can be negative.
  406. msg = "No index can be less than zero"
  407. with pytest.raises(ValueError, match=msg):
  408. IntIndex(length=5, indices=[1, -2, 3])
  409. # All indices must be less than the length.
  410. msg = "All indices must be less than the length"
  411. with pytest.raises(ValueError, match=msg):
  412. IntIndex(length=5, indices=[1, 2, 5])
  413. with pytest.raises(ValueError, match=msg):
  414. IntIndex(length=5, indices=[1, 2, 6])
  415. # Indices must be strictly ascending.
  416. msg = "Indices must be strictly increasing"
  417. with pytest.raises(ValueError, match=msg):
  418. IntIndex(length=5, indices=[1, 3, 2])
  419. with pytest.raises(ValueError, match=msg):
  420. IntIndex(length=5, indices=[1, 3, 3])
  421. def test_int_internal(self):
  422. idx = _make_index(4, np.array([2, 3], dtype=np.int32), kind='integer')
  423. assert isinstance(idx, IntIndex)
  424. assert idx.npoints == 2
  425. tm.assert_numpy_array_equal(idx.indices,
  426. np.array([2, 3], dtype=np.int32))
  427. idx = _make_index(4, np.array([], dtype=np.int32), kind='integer')
  428. assert isinstance(idx, IntIndex)
  429. assert idx.npoints == 0
  430. tm.assert_numpy_array_equal(idx.indices,
  431. np.array([], dtype=np.int32))
  432. idx = _make_index(4, np.array([0, 1, 2, 3], dtype=np.int32),
  433. kind='integer')
  434. assert isinstance(idx, IntIndex)
  435. assert idx.npoints == 4
  436. tm.assert_numpy_array_equal(idx.indices,
  437. np.array([0, 1, 2, 3], dtype=np.int32))
  438. def test_equals(self):
  439. index = IntIndex(10, [0, 1, 2, 3, 4])
  440. assert index.equals(index)
  441. assert not index.equals(IntIndex(10, [0, 1, 2, 3]))
  442. def test_to_block_index(self):
  443. def _check_case(xloc, xlen, yloc, ylen, eloc, elen):
  444. xindex = BlockIndex(TEST_LENGTH, xloc, xlen)
  445. yindex = BlockIndex(TEST_LENGTH, yloc, ylen)
  446. # see if survive the round trip
  447. xbindex = xindex.to_int_index().to_block_index()
  448. ybindex = yindex.to_int_index().to_block_index()
  449. assert isinstance(xbindex, BlockIndex)
  450. assert xbindex.equals(xindex)
  451. assert ybindex.equals(yindex)
  452. check_cases(_check_case)
  453. def test_to_int_index(self):
  454. index = IntIndex(10, [2, 3, 4, 5, 6])
  455. assert index.to_int_index() is index
  456. class TestSparseOperators(object):
  457. def _op_tests(self, sparse_op, python_op):
  458. def _check_case(xloc, xlen, yloc, ylen, eloc, elen):
  459. xindex = BlockIndex(TEST_LENGTH, xloc, xlen)
  460. yindex = BlockIndex(TEST_LENGTH, yloc, ylen)
  461. xdindex = xindex.to_int_index()
  462. ydindex = yindex.to_int_index()
  463. x = np.arange(xindex.npoints) * 10. + 1
  464. y = np.arange(yindex.npoints) * 100. + 1
  465. xfill = 0
  466. yfill = 2
  467. result_block_vals, rb_index, bfill = sparse_op(x, xindex, xfill, y,
  468. yindex, yfill)
  469. result_int_vals, ri_index, ifill = sparse_op(x, xdindex, xfill, y,
  470. ydindex, yfill)
  471. assert rb_index.to_int_index().equals(ri_index)
  472. tm.assert_numpy_array_equal(result_block_vals, result_int_vals)
  473. assert bfill == ifill
  474. # check versus Series...
  475. xseries = Series(x, xdindex.indices)
  476. xseries = xseries.reindex(np.arange(TEST_LENGTH)).fillna(xfill)
  477. yseries = Series(y, ydindex.indices)
  478. yseries = yseries.reindex(np.arange(TEST_LENGTH)).fillna(yfill)
  479. series_result = python_op(xseries, yseries)
  480. series_result = series_result.reindex(ri_index.indices)
  481. tm.assert_numpy_array_equal(result_block_vals,
  482. series_result.values)
  483. tm.assert_numpy_array_equal(result_int_vals, series_result.values)
  484. check_cases(_check_case)
  485. @pytest.mark.parametrize('opname',
  486. ['add', 'sub', 'mul', 'truediv', 'floordiv'])
  487. def test_op(self, opname):
  488. sparse_op = getattr(splib, 'sparse_%s_float64' % opname)
  489. python_op = getattr(operator, opname)
  490. self._op_tests(sparse_op, python_op)