test_indexing.py 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  1. # pylint: disable-msg=E1101,W0612
  2. import numpy as np
  3. import pytest
  4. import pandas as pd
  5. from pandas.core.sparse.api import SparseDtype
  6. import pandas.util.testing as tm
  7. class TestSparseSeriesIndexing(object):
  8. def setup_method(self, method):
  9. self.orig = pd.Series([1, np.nan, np.nan, 3, np.nan])
  10. self.sparse = self.orig.to_sparse()
  11. def test_getitem(self):
  12. orig = self.orig
  13. sparse = self.sparse
  14. assert sparse[0] == 1
  15. assert np.isnan(sparse[1])
  16. assert sparse[3] == 3
  17. result = sparse[[1, 3, 4]]
  18. exp = orig[[1, 3, 4]].to_sparse()
  19. tm.assert_sp_series_equal(result, exp)
  20. # dense array
  21. result = sparse[orig % 2 == 1]
  22. exp = orig[orig % 2 == 1].to_sparse()
  23. tm.assert_sp_series_equal(result, exp)
  24. # sparse array (actuary it coerces to normal Series)
  25. result = sparse[sparse % 2 == 1]
  26. exp = orig[orig % 2 == 1].to_sparse()
  27. tm.assert_sp_series_equal(result, exp)
  28. # sparse array
  29. result = sparse[pd.SparseArray(sparse % 2 == 1, dtype=bool)]
  30. tm.assert_sp_series_equal(result, exp)
  31. def test_getitem_slice(self):
  32. orig = self.orig
  33. sparse = self.sparse
  34. tm.assert_sp_series_equal(sparse[:2], orig[:2].to_sparse())
  35. tm.assert_sp_series_equal(sparse[4:2], orig[4:2].to_sparse())
  36. tm.assert_sp_series_equal(sparse[::2], orig[::2].to_sparse())
  37. tm.assert_sp_series_equal(sparse[-5:], orig[-5:].to_sparse())
  38. def test_getitem_int_dtype(self):
  39. # GH 8292
  40. s = pd.SparseSeries([0, 1, 2, 3, 4, 5, 6], name='xxx')
  41. res = s[::2]
  42. exp = pd.SparseSeries([0, 2, 4, 6], index=[0, 2, 4, 6], name='xxx')
  43. tm.assert_sp_series_equal(res, exp)
  44. assert res.dtype == SparseDtype(np.int64)
  45. s = pd.SparseSeries([0, 1, 2, 3, 4, 5, 6], fill_value=0, name='xxx')
  46. res = s[::2]
  47. exp = pd.SparseSeries([0, 2, 4, 6], index=[0, 2, 4, 6],
  48. fill_value=0, name='xxx')
  49. tm.assert_sp_series_equal(res, exp)
  50. assert res.dtype == SparseDtype(np.int64)
  51. def test_getitem_fill_value(self):
  52. orig = pd.Series([1, np.nan, 0, 3, 0])
  53. sparse = orig.to_sparse(fill_value=0)
  54. assert sparse[0] == 1
  55. assert np.isnan(sparse[1])
  56. assert sparse[2] == 0
  57. assert sparse[3] == 3
  58. result = sparse[[1, 3, 4]]
  59. exp = orig[[1, 3, 4]].to_sparse(fill_value=0)
  60. tm.assert_sp_series_equal(result, exp)
  61. # dense array
  62. result = sparse[orig % 2 == 1]
  63. exp = orig[orig % 2 == 1].to_sparse(fill_value=0)
  64. tm.assert_sp_series_equal(result, exp)
  65. # sparse array (actuary it coerces to normal Series)
  66. result = sparse[sparse % 2 == 1]
  67. exp = orig[orig % 2 == 1].to_sparse(fill_value=0)
  68. tm.assert_sp_series_equal(result, exp)
  69. # sparse array
  70. result = sparse[pd.SparseArray(sparse % 2 == 1, dtype=bool)]
  71. tm.assert_sp_series_equal(result, exp)
  72. def test_getitem_ellipsis(self):
  73. # GH 9467
  74. s = pd.SparseSeries([1, np.nan, 2, 0, np.nan])
  75. tm.assert_sp_series_equal(s[...], s)
  76. s = pd.SparseSeries([1, np.nan, 2, 0, np.nan], fill_value=0)
  77. tm.assert_sp_series_equal(s[...], s)
  78. def test_getitem_slice_fill_value(self):
  79. orig = pd.Series([1, np.nan, 0, 3, 0])
  80. sparse = orig.to_sparse(fill_value=0)
  81. tm.assert_sp_series_equal(sparse[:2],
  82. orig[:2].to_sparse(fill_value=0))
  83. tm.assert_sp_series_equal(sparse[4:2],
  84. orig[4:2].to_sparse(fill_value=0))
  85. tm.assert_sp_series_equal(sparse[::2],
  86. orig[::2].to_sparse(fill_value=0))
  87. tm.assert_sp_series_equal(sparse[-5:],
  88. orig[-5:].to_sparse(fill_value=0))
  89. def test_loc(self):
  90. orig = self.orig
  91. sparse = self.sparse
  92. assert sparse.loc[0] == 1
  93. assert np.isnan(sparse.loc[1])
  94. result = sparse.loc[[1, 3, 4]]
  95. exp = orig.loc[[1, 3, 4]].to_sparse()
  96. tm.assert_sp_series_equal(result, exp)
  97. # exceeds the bounds
  98. result = sparse.reindex([1, 3, 4, 5])
  99. exp = orig.reindex([1, 3, 4, 5]).to_sparse()
  100. tm.assert_sp_series_equal(result, exp)
  101. # padded with NaN
  102. assert np.isnan(result[-1])
  103. # dense array
  104. result = sparse.loc[orig % 2 == 1]
  105. exp = orig.loc[orig % 2 == 1].to_sparse()
  106. tm.assert_sp_series_equal(result, exp)
  107. # sparse array (actuary it coerces to normal Series)
  108. result = sparse.loc[sparse % 2 == 1]
  109. exp = orig.loc[orig % 2 == 1].to_sparse()
  110. tm.assert_sp_series_equal(result, exp)
  111. # sparse array
  112. result = sparse.loc[pd.SparseArray(sparse % 2 == 1, dtype=bool)]
  113. tm.assert_sp_series_equal(result, exp)
  114. def test_loc_index(self):
  115. orig = pd.Series([1, np.nan, np.nan, 3, np.nan], index=list('ABCDE'))
  116. sparse = orig.to_sparse()
  117. assert sparse.loc['A'] == 1
  118. assert np.isnan(sparse.loc['B'])
  119. result = sparse.loc[['A', 'C', 'D']]
  120. exp = orig.loc[['A', 'C', 'D']].to_sparse()
  121. tm.assert_sp_series_equal(result, exp)
  122. # dense array
  123. result = sparse.loc[orig % 2 == 1]
  124. exp = orig.loc[orig % 2 == 1].to_sparse()
  125. tm.assert_sp_series_equal(result, exp)
  126. # sparse array (actuary it coerces to normal Series)
  127. result = sparse.loc[sparse % 2 == 1]
  128. exp = orig.loc[orig % 2 == 1].to_sparse()
  129. tm.assert_sp_series_equal(result, exp)
  130. # sparse array
  131. result = sparse[pd.SparseArray(sparse % 2 == 1, dtype=bool)]
  132. tm.assert_sp_series_equal(result, exp)
  133. def test_loc_index_fill_value(self):
  134. orig = pd.Series([1, np.nan, 0, 3, 0], index=list('ABCDE'))
  135. sparse = orig.to_sparse(fill_value=0)
  136. assert sparse.loc['A'] == 1
  137. assert np.isnan(sparse.loc['B'])
  138. result = sparse.loc[['A', 'C', 'D']]
  139. exp = orig.loc[['A', 'C', 'D']].to_sparse(fill_value=0)
  140. tm.assert_sp_series_equal(result, exp)
  141. # dense array
  142. result = sparse.loc[orig % 2 == 1]
  143. exp = orig.loc[orig % 2 == 1].to_sparse(fill_value=0)
  144. tm.assert_sp_series_equal(result, exp)
  145. # sparse array (actuary it coerces to normal Series)
  146. result = sparse.loc[sparse % 2 == 1]
  147. exp = orig.loc[orig % 2 == 1].to_sparse(fill_value=0)
  148. tm.assert_sp_series_equal(result, exp)
  149. def test_loc_slice(self):
  150. orig = self.orig
  151. sparse = self.sparse
  152. tm.assert_sp_series_equal(sparse.loc[2:], orig.loc[2:].to_sparse())
  153. def test_loc_slice_index_fill_value(self):
  154. orig = pd.Series([1, np.nan, 0, 3, 0], index=list('ABCDE'))
  155. sparse = orig.to_sparse(fill_value=0)
  156. tm.assert_sp_series_equal(sparse.loc['C':],
  157. orig.loc['C':].to_sparse(fill_value=0))
  158. def test_loc_slice_fill_value(self):
  159. orig = pd.Series([1, np.nan, 0, 3, 0])
  160. sparse = orig.to_sparse(fill_value=0)
  161. tm.assert_sp_series_equal(sparse.loc[2:],
  162. orig.loc[2:].to_sparse(fill_value=0))
  163. def test_iloc(self):
  164. orig = self.orig
  165. sparse = self.sparse
  166. assert sparse.iloc[3] == 3
  167. assert np.isnan(sparse.iloc[2])
  168. result = sparse.iloc[[1, 3, 4]]
  169. exp = orig.iloc[[1, 3, 4]].to_sparse()
  170. tm.assert_sp_series_equal(result, exp)
  171. result = sparse.iloc[[1, -2, -4]]
  172. exp = orig.iloc[[1, -2, -4]].to_sparse()
  173. tm.assert_sp_series_equal(result, exp)
  174. with pytest.raises(IndexError):
  175. sparse.iloc[[1, 3, 5]]
  176. def test_iloc_fill_value(self):
  177. orig = pd.Series([1, np.nan, 0, 3, 0])
  178. sparse = orig.to_sparse(fill_value=0)
  179. assert sparse.iloc[3] == 3
  180. assert np.isnan(sparse.iloc[1])
  181. assert sparse.iloc[4] == 0
  182. result = sparse.iloc[[1, 3, 4]]
  183. exp = orig.iloc[[1, 3, 4]].to_sparse(fill_value=0)
  184. tm.assert_sp_series_equal(result, exp)
  185. def test_iloc_slice(self):
  186. orig = pd.Series([1, np.nan, np.nan, 3, np.nan])
  187. sparse = orig.to_sparse()
  188. tm.assert_sp_series_equal(sparse.iloc[2:], orig.iloc[2:].to_sparse())
  189. def test_iloc_slice_fill_value(self):
  190. orig = pd.Series([1, np.nan, 0, 3, 0])
  191. sparse = orig.to_sparse(fill_value=0)
  192. tm.assert_sp_series_equal(sparse.iloc[2:],
  193. orig.iloc[2:].to_sparse(fill_value=0))
  194. def test_at(self):
  195. orig = pd.Series([1, np.nan, np.nan, 3, np.nan])
  196. sparse = orig.to_sparse()
  197. assert sparse.at[0] == orig.at[0]
  198. assert np.isnan(sparse.at[1])
  199. assert np.isnan(sparse.at[2])
  200. assert sparse.at[3] == orig.at[3]
  201. assert np.isnan(sparse.at[4])
  202. orig = pd.Series([1, np.nan, np.nan, 3, np.nan],
  203. index=list('abcde'))
  204. sparse = orig.to_sparse()
  205. assert sparse.at['a'] == orig.at['a']
  206. assert np.isnan(sparse.at['b'])
  207. assert np.isnan(sparse.at['c'])
  208. assert sparse.at['d'] == orig.at['d']
  209. assert np.isnan(sparse.at['e'])
  210. def test_at_fill_value(self):
  211. orig = pd.Series([1, np.nan, 0, 3, 0],
  212. index=list('abcde'))
  213. sparse = orig.to_sparse(fill_value=0)
  214. assert sparse.at['a'] == orig.at['a']
  215. assert np.isnan(sparse.at['b'])
  216. assert sparse.at['c'] == orig.at['c']
  217. assert sparse.at['d'] == orig.at['d']
  218. assert sparse.at['e'] == orig.at['e']
  219. def test_iat(self):
  220. orig = self.orig
  221. sparse = self.sparse
  222. assert sparse.iat[0] == orig.iat[0]
  223. assert np.isnan(sparse.iat[1])
  224. assert np.isnan(sparse.iat[2])
  225. assert sparse.iat[3] == orig.iat[3]
  226. assert np.isnan(sparse.iat[4])
  227. assert np.isnan(sparse.iat[-1])
  228. assert sparse.iat[-5] == orig.iat[-5]
  229. def test_iat_fill_value(self):
  230. orig = pd.Series([1, np.nan, 0, 3, 0])
  231. sparse = orig.to_sparse()
  232. assert sparse.iat[0] == orig.iat[0]
  233. assert np.isnan(sparse.iat[1])
  234. assert sparse.iat[2] == orig.iat[2]
  235. assert sparse.iat[3] == orig.iat[3]
  236. assert sparse.iat[4] == orig.iat[4]
  237. assert sparse.iat[-1] == orig.iat[-1]
  238. assert sparse.iat[-5] == orig.iat[-5]
  239. def test_get(self):
  240. s = pd.SparseSeries([1, np.nan, np.nan, 3, np.nan])
  241. assert s.get(0) == 1
  242. assert np.isnan(s.get(1))
  243. assert s.get(5) is None
  244. s = pd.SparseSeries([1, np.nan, 0, 3, 0], index=list('ABCDE'))
  245. assert s.get('A') == 1
  246. assert np.isnan(s.get('B'))
  247. assert s.get('C') == 0
  248. assert s.get('XX') is None
  249. s = pd.SparseSeries([1, np.nan, 0, 3, 0], index=list('ABCDE'),
  250. fill_value=0)
  251. assert s.get('A') == 1
  252. assert np.isnan(s.get('B'))
  253. assert s.get('C') == 0
  254. assert s.get('XX') is None
  255. def test_take(self):
  256. orig = pd.Series([1, np.nan, np.nan, 3, np.nan],
  257. index=list('ABCDE'))
  258. sparse = orig.to_sparse()
  259. tm.assert_sp_series_equal(sparse.take([0]),
  260. orig.take([0]).to_sparse())
  261. tm.assert_sp_series_equal(sparse.take([0, 1, 3]),
  262. orig.take([0, 1, 3]).to_sparse())
  263. tm.assert_sp_series_equal(sparse.take([-1, -2]),
  264. orig.take([-1, -2]).to_sparse())
  265. def test_take_fill_value(self):
  266. orig = pd.Series([1, np.nan, 0, 3, 0],
  267. index=list('ABCDE'))
  268. sparse = orig.to_sparse(fill_value=0)
  269. tm.assert_sp_series_equal(sparse.take([0]),
  270. orig.take([0]).to_sparse(fill_value=0))
  271. exp = orig.take([0, 1, 3]).to_sparse(fill_value=0)
  272. tm.assert_sp_series_equal(sparse.take([0, 1, 3]), exp)
  273. exp = orig.take([-1, -2]).to_sparse(fill_value=0)
  274. tm.assert_sp_series_equal(sparse.take([-1, -2]), exp)
  275. def test_reindex(self):
  276. orig = pd.Series([1, np.nan, np.nan, 3, np.nan],
  277. index=list('ABCDE'))
  278. sparse = orig.to_sparse()
  279. res = sparse.reindex(['A', 'E', 'C', 'D'])
  280. exp = orig.reindex(['A', 'E', 'C', 'D']).to_sparse()
  281. tm.assert_sp_series_equal(res, exp)
  282. # all missing & fill_value
  283. res = sparse.reindex(['B', 'E', 'C'])
  284. exp = orig.reindex(['B', 'E', 'C']).to_sparse()
  285. tm.assert_sp_series_equal(res, exp)
  286. orig = pd.Series([np.nan, np.nan, np.nan, np.nan, np.nan],
  287. index=list('ABCDE'))
  288. sparse = orig.to_sparse()
  289. res = sparse.reindex(['A', 'E', 'C', 'D'])
  290. exp = orig.reindex(['A', 'E', 'C', 'D']).to_sparse()
  291. tm.assert_sp_series_equal(res, exp)
  292. def test_fill_value_reindex(self):
  293. orig = pd.Series([1, np.nan, 0, 3, 0], index=list('ABCDE'))
  294. sparse = orig.to_sparse(fill_value=0)
  295. res = sparse.reindex(['A', 'E', 'C', 'D'])
  296. exp = orig.reindex(['A', 'E', 'C', 'D']).to_sparse(fill_value=0)
  297. tm.assert_sp_series_equal(res, exp)
  298. # includes missing and fill_value
  299. res = sparse.reindex(['A', 'B', 'C'])
  300. exp = orig.reindex(['A', 'B', 'C']).to_sparse(fill_value=0)
  301. tm.assert_sp_series_equal(res, exp)
  302. # all missing
  303. orig = pd.Series([np.nan, np.nan, np.nan, np.nan, np.nan],
  304. index=list('ABCDE'))
  305. sparse = orig.to_sparse(fill_value=0)
  306. res = sparse.reindex(['A', 'E', 'C', 'D'])
  307. exp = orig.reindex(['A', 'E', 'C', 'D']).to_sparse(fill_value=0)
  308. tm.assert_sp_series_equal(res, exp)
  309. # all fill_value
  310. orig = pd.Series([0., 0., 0., 0., 0.],
  311. index=list('ABCDE'))
  312. sparse = orig.to_sparse(fill_value=0)
  313. def test_fill_value_reindex_coerces_float_int(self):
  314. orig = pd.Series([1, np.nan, 0, 3, 0], index=list('ABCDE'))
  315. sparse = orig.to_sparse(fill_value=0)
  316. res = sparse.reindex(['A', 'E', 'C', 'D'])
  317. exp = orig.reindex(['A', 'E', 'C', 'D']).to_sparse(fill_value=0)
  318. tm.assert_sp_series_equal(res, exp)
  319. def test_reindex_fill_value(self):
  320. floats = pd.Series([1., 2., 3.]).to_sparse()
  321. result = floats.reindex([1, 2, 3], fill_value=0)
  322. expected = pd.Series([2., 3., 0], index=[1, 2, 3]).to_sparse()
  323. tm.assert_sp_series_equal(result, expected)
  324. def test_reindex_nearest(self):
  325. s = pd.Series(np.arange(10, dtype='float64')).to_sparse()
  326. target = [0.1, 0.9, 1.5, 2.0]
  327. actual = s.reindex(target, method='nearest')
  328. expected = pd.Series(np.around(target), target).to_sparse()
  329. tm.assert_sp_series_equal(expected, actual)
  330. actual = s.reindex(target, method='nearest', tolerance=0.2)
  331. expected = pd.Series([0, 1, np.nan, 2], target).to_sparse()
  332. tm.assert_sp_series_equal(expected, actual)
  333. actual = s.reindex(target, method='nearest',
  334. tolerance=[0.3, 0.01, 0.4, 3])
  335. expected = pd.Series([0, np.nan, np.nan, 2], target).to_sparse()
  336. tm.assert_sp_series_equal(expected, actual)
  337. @pytest.mark.parametrize("kind", ["integer", "block"])
  338. @pytest.mark.parametrize("fill", [True, False, np.nan])
  339. def tests_indexing_with_sparse(self, kind, fill):
  340. # see gh-13985
  341. arr = pd.SparseArray([1, 2, 3], kind=kind)
  342. indexer = pd.SparseArray([True, False, True],
  343. fill_value=fill,
  344. dtype=bool)
  345. expected = arr[indexer]
  346. result = pd.SparseArray([1, 3], kind=kind)
  347. tm.assert_sp_array_equal(result, expected)
  348. s = pd.SparseSeries(arr, index=["a", "b", "c"], dtype=np.float64)
  349. expected = pd.SparseSeries([1, 3], index=["a", "c"], kind=kind,
  350. dtype=SparseDtype(np.float64, s.fill_value))
  351. tm.assert_sp_series_equal(s[indexer], expected)
  352. tm.assert_sp_series_equal(s.loc[indexer], expected)
  353. tm.assert_sp_series_equal(s.iloc[indexer], expected)
  354. indexer = pd.SparseSeries(indexer, index=["a", "b", "c"])
  355. tm.assert_sp_series_equal(s[indexer], expected)
  356. tm.assert_sp_series_equal(s.loc[indexer], expected)
  357. msg = ("iLocation based boolean indexing cannot "
  358. "use an indexable as a mask")
  359. with pytest.raises(ValueError, match=msg):
  360. s.iloc[indexer]
  361. class TestSparseSeriesMultiIndexing(TestSparseSeriesIndexing):
  362. def setup_method(self, method):
  363. # Mi with duplicated values
  364. idx = pd.MultiIndex.from_tuples([('A', 0), ('A', 1), ('B', 0),
  365. ('C', 0), ('C', 1)])
  366. self.orig = pd.Series([1, np.nan, np.nan, 3, np.nan], index=idx)
  367. self.sparse = self.orig.to_sparse()
  368. def test_getitem_multi(self):
  369. orig = self.orig
  370. sparse = self.sparse
  371. assert sparse[0] == orig[0]
  372. assert np.isnan(sparse[1])
  373. assert sparse[3] == orig[3]
  374. tm.assert_sp_series_equal(sparse['A'], orig['A'].to_sparse())
  375. tm.assert_sp_series_equal(sparse['B'], orig['B'].to_sparse())
  376. result = sparse[[1, 3, 4]]
  377. exp = orig[[1, 3, 4]].to_sparse()
  378. tm.assert_sp_series_equal(result, exp)
  379. # dense array
  380. result = sparse[orig % 2 == 1]
  381. exp = orig[orig % 2 == 1].to_sparse()
  382. tm.assert_sp_series_equal(result, exp)
  383. # sparse array (actuary it coerces to normal Series)
  384. result = sparse[sparse % 2 == 1]
  385. exp = orig[orig % 2 == 1].to_sparse()
  386. tm.assert_sp_series_equal(result, exp)
  387. # sparse array
  388. result = sparse[pd.SparseArray(sparse % 2 == 1, dtype=bool)]
  389. tm.assert_sp_series_equal(result, exp)
  390. def test_getitem_multi_tuple(self):
  391. orig = self.orig
  392. sparse = self.sparse
  393. assert sparse['C', 0] == orig['C', 0]
  394. assert np.isnan(sparse['A', 1])
  395. assert np.isnan(sparse['B', 0])
  396. def test_getitems_slice_multi(self):
  397. orig = self.orig
  398. sparse = self.sparse
  399. tm.assert_sp_series_equal(sparse[2:], orig[2:].to_sparse())
  400. tm.assert_sp_series_equal(sparse.loc['B':], orig.loc['B':].to_sparse())
  401. tm.assert_sp_series_equal(sparse.loc['C':], orig.loc['C':].to_sparse())
  402. tm.assert_sp_series_equal(sparse.loc['A':'B'],
  403. orig.loc['A':'B'].to_sparse())
  404. tm.assert_sp_series_equal(sparse.loc[:'B'], orig.loc[:'B'].to_sparse())
  405. def test_loc(self):
  406. # need to be override to use different label
  407. orig = self.orig
  408. sparse = self.sparse
  409. tm.assert_sp_series_equal(sparse.loc['A'],
  410. orig.loc['A'].to_sparse())
  411. tm.assert_sp_series_equal(sparse.loc['B'],
  412. orig.loc['B'].to_sparse())
  413. result = sparse.loc[[1, 3, 4]]
  414. exp = orig.loc[[1, 3, 4]].to_sparse()
  415. tm.assert_sp_series_equal(result, exp)
  416. # exceeds the bounds
  417. result = sparse.loc[[1, 3, 4, 5]]
  418. exp = orig.loc[[1, 3, 4, 5]].to_sparse()
  419. tm.assert_sp_series_equal(result, exp)
  420. # single element list (GH 15447)
  421. result = sparse.loc[['A']]
  422. exp = orig.loc[['A']].to_sparse()
  423. tm.assert_sp_series_equal(result, exp)
  424. # dense array
  425. result = sparse.loc[orig % 2 == 1]
  426. exp = orig.loc[orig % 2 == 1].to_sparse()
  427. tm.assert_sp_series_equal(result, exp)
  428. # sparse array (actuary it coerces to normal Series)
  429. result = sparse.loc[sparse % 2 == 1]
  430. exp = orig.loc[orig % 2 == 1].to_sparse()
  431. tm.assert_sp_series_equal(result, exp)
  432. # sparse array
  433. result = sparse.loc[pd.SparseArray(sparse % 2 == 1, dtype=bool)]
  434. tm.assert_sp_series_equal(result, exp)
  435. def test_loc_multi_tuple(self):
  436. orig = self.orig
  437. sparse = self.sparse
  438. assert sparse.loc['C', 0] == orig.loc['C', 0]
  439. assert np.isnan(sparse.loc['A', 1])
  440. assert np.isnan(sparse.loc['B', 0])
  441. def test_loc_slice(self):
  442. orig = self.orig
  443. sparse = self.sparse
  444. tm.assert_sp_series_equal(sparse.loc['A':], orig.loc['A':].to_sparse())
  445. tm.assert_sp_series_equal(sparse.loc['B':], orig.loc['B':].to_sparse())
  446. tm.assert_sp_series_equal(sparse.loc['C':], orig.loc['C':].to_sparse())
  447. tm.assert_sp_series_equal(sparse.loc['A':'B'],
  448. orig.loc['A':'B'].to_sparse())
  449. tm.assert_sp_series_equal(sparse.loc[:'B'], orig.loc[:'B'].to_sparse())
  450. def test_reindex(self):
  451. # GH 15447
  452. orig = self.orig
  453. sparse = self.sparse
  454. res = sparse.reindex([('A', 0), ('C', 1)])
  455. exp = orig.reindex([('A', 0), ('C', 1)]).to_sparse()
  456. tm.assert_sp_series_equal(res, exp)
  457. # On specific level:
  458. res = sparse.reindex(['A', 'C', 'B'], level=0)
  459. exp = orig.reindex(['A', 'C', 'B'], level=0).to_sparse()
  460. tm.assert_sp_series_equal(res, exp)
  461. # single element list (GH 15447)
  462. res = sparse.reindex(['A'], level=0)
  463. exp = orig.reindex(['A'], level=0).to_sparse()
  464. tm.assert_sp_series_equal(res, exp)
  465. with pytest.raises(TypeError):
  466. # Incomplete keys are not accepted for reindexing:
  467. sparse.reindex(['A', 'C'])
  468. # "copy" argument:
  469. res = sparse.reindex(sparse.index, copy=True)
  470. exp = orig.reindex(orig.index, copy=True).to_sparse()
  471. tm.assert_sp_series_equal(res, exp)
  472. assert sparse is not res
  473. class TestSparseDataFrameIndexing(object):
  474. def test_getitem(self):
  475. orig = pd.DataFrame([[1, np.nan, np.nan],
  476. [2, 3, np.nan],
  477. [np.nan, np.nan, 4],
  478. [0, np.nan, 5]],
  479. columns=list('xyz'))
  480. sparse = orig.to_sparse()
  481. tm.assert_sp_series_equal(sparse['x'], orig['x'].to_sparse())
  482. tm.assert_sp_frame_equal(sparse[['x']], orig[['x']].to_sparse())
  483. tm.assert_sp_frame_equal(sparse[['z', 'x']],
  484. orig[['z', 'x']].to_sparse())
  485. tm.assert_sp_frame_equal(sparse[[True, False, True, True]],
  486. orig[[True, False, True, True]].to_sparse())
  487. tm.assert_sp_frame_equal(sparse.iloc[[1, 2]],
  488. orig.iloc[[1, 2]].to_sparse())
  489. def test_getitem_fill_value(self):
  490. orig = pd.DataFrame([[1, np.nan, 0],
  491. [2, 3, np.nan],
  492. [0, np.nan, 4],
  493. [0, np.nan, 5]],
  494. columns=list('xyz'))
  495. sparse = orig.to_sparse(fill_value=0)
  496. result = sparse[['z']]
  497. expected = orig[['z']].to_sparse(fill_value=0)
  498. tm.assert_sp_frame_equal(result, expected, check_fill_value=False)
  499. tm.assert_sp_series_equal(sparse['y'],
  500. orig['y'].to_sparse(fill_value=0))
  501. exp = orig[['x']].to_sparse(fill_value=0)
  502. exp._default_fill_value = np.nan
  503. tm.assert_sp_frame_equal(sparse[['x']], exp)
  504. exp = orig[['z', 'x']].to_sparse(fill_value=0)
  505. exp._default_fill_value = np.nan
  506. tm.assert_sp_frame_equal(sparse[['z', 'x']], exp)
  507. indexer = [True, False, True, True]
  508. exp = orig[indexer].to_sparse(fill_value=0)
  509. exp._default_fill_value = np.nan
  510. tm.assert_sp_frame_equal(sparse[indexer], exp)
  511. exp = orig.iloc[[1, 2]].to_sparse(fill_value=0)
  512. exp._default_fill_value = np.nan
  513. tm.assert_sp_frame_equal(sparse.iloc[[1, 2]], exp)
  514. def test_loc(self):
  515. orig = pd.DataFrame([[1, np.nan, np.nan],
  516. [2, 3, np.nan],
  517. [np.nan, np.nan, 4]],
  518. columns=list('xyz'))
  519. sparse = orig.to_sparse()
  520. assert sparse.loc[0, 'x'] == 1
  521. assert np.isnan(sparse.loc[1, 'z'])
  522. assert sparse.loc[2, 'z'] == 4
  523. # have to specify `kind='integer'`, since we construct a
  524. # new SparseArray here, and the default sparse type is
  525. # integer there, but block in SparseSeries
  526. tm.assert_sp_series_equal(sparse.loc[0],
  527. orig.loc[0].to_sparse(kind='integer'))
  528. tm.assert_sp_series_equal(sparse.loc[1],
  529. orig.loc[1].to_sparse(kind='integer'))
  530. tm.assert_sp_series_equal(sparse.loc[2, :],
  531. orig.loc[2, :].to_sparse(kind='integer'))
  532. tm.assert_sp_series_equal(sparse.loc[2, :],
  533. orig.loc[2, :].to_sparse(kind='integer'))
  534. tm.assert_sp_series_equal(sparse.loc[:, 'y'],
  535. orig.loc[:, 'y'].to_sparse())
  536. tm.assert_sp_series_equal(sparse.loc[:, 'y'],
  537. orig.loc[:, 'y'].to_sparse())
  538. result = sparse.loc[[1, 2]]
  539. exp = orig.loc[[1, 2]].to_sparse()
  540. tm.assert_sp_frame_equal(result, exp)
  541. result = sparse.loc[[1, 2], :]
  542. exp = orig.loc[[1, 2], :].to_sparse()
  543. tm.assert_sp_frame_equal(result, exp)
  544. result = sparse.loc[:, ['x', 'z']]
  545. exp = orig.loc[:, ['x', 'z']].to_sparse()
  546. tm.assert_sp_frame_equal(result, exp)
  547. result = sparse.loc[[0, 2], ['x', 'z']]
  548. exp = orig.loc[[0, 2], ['x', 'z']].to_sparse()
  549. tm.assert_sp_frame_equal(result, exp)
  550. # exceeds the bounds
  551. result = sparse.reindex([1, 3, 4, 5])
  552. exp = orig.reindex([1, 3, 4, 5]).to_sparse()
  553. tm.assert_sp_frame_equal(result, exp)
  554. # dense array
  555. result = sparse.loc[orig.x % 2 == 1]
  556. exp = orig.loc[orig.x % 2 == 1].to_sparse()
  557. tm.assert_sp_frame_equal(result, exp)
  558. # sparse array (actuary it coerces to normal Series)
  559. result = sparse.loc[sparse.x % 2 == 1]
  560. exp = orig.loc[orig.x % 2 == 1].to_sparse()
  561. tm.assert_sp_frame_equal(result, exp)
  562. # sparse array
  563. result = sparse.loc[pd.SparseArray(sparse.x % 2 == 1, dtype=bool)]
  564. tm.assert_sp_frame_equal(result, exp)
  565. def test_loc_index(self):
  566. orig = pd.DataFrame([[1, np.nan, np.nan],
  567. [2, 3, np.nan],
  568. [np.nan, np.nan, 4]],
  569. index=list('abc'), columns=list('xyz'))
  570. sparse = orig.to_sparse()
  571. assert sparse.loc['a', 'x'] == 1
  572. assert np.isnan(sparse.loc['b', 'z'])
  573. assert sparse.loc['c', 'z'] == 4
  574. tm.assert_sp_series_equal(sparse.loc['a'],
  575. orig.loc['a'].to_sparse(kind='integer'))
  576. tm.assert_sp_series_equal(sparse.loc['b'],
  577. orig.loc['b'].to_sparse(kind='integer'))
  578. tm.assert_sp_series_equal(sparse.loc['b', :],
  579. orig.loc['b', :].to_sparse(kind='integer'))
  580. tm.assert_sp_series_equal(sparse.loc['b', :],
  581. orig.loc['b', :].to_sparse(kind='integer'))
  582. tm.assert_sp_series_equal(sparse.loc[:, 'z'],
  583. orig.loc[:, 'z'].to_sparse())
  584. tm.assert_sp_series_equal(sparse.loc[:, 'z'],
  585. orig.loc[:, 'z'].to_sparse())
  586. result = sparse.loc[['a', 'b']]
  587. exp = orig.loc[['a', 'b']].to_sparse()
  588. tm.assert_sp_frame_equal(result, exp)
  589. result = sparse.loc[['a', 'b'], :]
  590. exp = orig.loc[['a', 'b'], :].to_sparse()
  591. tm.assert_sp_frame_equal(result, exp)
  592. result = sparse.loc[:, ['x', 'z']]
  593. exp = orig.loc[:, ['x', 'z']].to_sparse()
  594. tm.assert_sp_frame_equal(result, exp)
  595. result = sparse.loc[['c', 'a'], ['x', 'z']]
  596. exp = orig.loc[['c', 'a'], ['x', 'z']].to_sparse()
  597. tm.assert_sp_frame_equal(result, exp)
  598. # dense array
  599. result = sparse.loc[orig.x % 2 == 1]
  600. exp = orig.loc[orig.x % 2 == 1].to_sparse()
  601. tm.assert_sp_frame_equal(result, exp)
  602. # sparse array (actuary it coerces to normal Series)
  603. result = sparse.loc[sparse.x % 2 == 1]
  604. exp = orig.loc[orig.x % 2 == 1].to_sparse()
  605. tm.assert_sp_frame_equal(result, exp)
  606. # sparse array
  607. result = sparse.loc[pd.SparseArray(sparse.x % 2 == 1, dtype=bool)]
  608. tm.assert_sp_frame_equal(result, exp)
  609. def test_loc_slice(self):
  610. orig = pd.DataFrame([[1, np.nan, np.nan],
  611. [2, 3, np.nan],
  612. [np.nan, np.nan, 4]],
  613. columns=list('xyz'))
  614. sparse = orig.to_sparse()
  615. tm.assert_sp_frame_equal(sparse.loc[2:], orig.loc[2:].to_sparse())
  616. def test_iloc(self):
  617. orig = pd.DataFrame([[1, np.nan, np.nan],
  618. [2, 3, np.nan],
  619. [np.nan, np.nan, 4]])
  620. sparse = orig.to_sparse()
  621. assert sparse.iloc[1, 1] == 3
  622. assert np.isnan(sparse.iloc[2, 0])
  623. tm.assert_sp_series_equal(sparse.iloc[0],
  624. orig.loc[0].to_sparse(kind='integer'))
  625. tm.assert_sp_series_equal(sparse.iloc[1],
  626. orig.loc[1].to_sparse(kind='integer'))
  627. tm.assert_sp_series_equal(sparse.iloc[2, :],
  628. orig.iloc[2, :].to_sparse(kind='integer'))
  629. tm.assert_sp_series_equal(sparse.iloc[2, :],
  630. orig.iloc[2, :].to_sparse(kind='integer'))
  631. tm.assert_sp_series_equal(sparse.iloc[:, 1],
  632. orig.iloc[:, 1].to_sparse())
  633. tm.assert_sp_series_equal(sparse.iloc[:, 1],
  634. orig.iloc[:, 1].to_sparse())
  635. result = sparse.iloc[[1, 2]]
  636. exp = orig.iloc[[1, 2]].to_sparse()
  637. tm.assert_sp_frame_equal(result, exp)
  638. result = sparse.iloc[[1, 2], :]
  639. exp = orig.iloc[[1, 2], :].to_sparse()
  640. tm.assert_sp_frame_equal(result, exp)
  641. result = sparse.iloc[:, [1, 0]]
  642. exp = orig.iloc[:, [1, 0]].to_sparse()
  643. tm.assert_sp_frame_equal(result, exp)
  644. result = sparse.iloc[[2], [1, 0]]
  645. exp = orig.iloc[[2], [1, 0]].to_sparse()
  646. tm.assert_sp_frame_equal(result, exp)
  647. with pytest.raises(IndexError):
  648. sparse.iloc[[1, 3, 5]]
  649. def test_iloc_slice(self):
  650. orig = pd.DataFrame([[1, np.nan, np.nan],
  651. [2, 3, np.nan],
  652. [np.nan, np.nan, 4]],
  653. columns=list('xyz'))
  654. sparse = orig.to_sparse()
  655. tm.assert_sp_frame_equal(sparse.iloc[2:], orig.iloc[2:].to_sparse())
  656. def test_at(self):
  657. orig = pd.DataFrame([[1, np.nan, 0],
  658. [2, 3, np.nan],
  659. [0, np.nan, 4],
  660. [0, np.nan, 5]],
  661. index=list('ABCD'), columns=list('xyz'))
  662. sparse = orig.to_sparse()
  663. assert sparse.at['A', 'x'] == orig.at['A', 'x']
  664. assert np.isnan(sparse.at['B', 'z'])
  665. assert np.isnan(sparse.at['C', 'y'])
  666. assert sparse.at['D', 'x'] == orig.at['D', 'x']
  667. def test_at_fill_value(self):
  668. orig = pd.DataFrame([[1, np.nan, 0],
  669. [2, 3, np.nan],
  670. [0, np.nan, 4],
  671. [0, np.nan, 5]],
  672. index=list('ABCD'), columns=list('xyz'))
  673. sparse = orig.to_sparse(fill_value=0)
  674. assert sparse.at['A', 'x'] == orig.at['A', 'x']
  675. assert np.isnan(sparse.at['B', 'z'])
  676. assert np.isnan(sparse.at['C', 'y'])
  677. assert sparse.at['D', 'x'] == orig.at['D', 'x']
  678. def test_iat(self):
  679. orig = pd.DataFrame([[1, np.nan, 0],
  680. [2, 3, np.nan],
  681. [0, np.nan, 4],
  682. [0, np.nan, 5]],
  683. index=list('ABCD'), columns=list('xyz'))
  684. sparse = orig.to_sparse()
  685. assert sparse.iat[0, 0] == orig.iat[0, 0]
  686. assert np.isnan(sparse.iat[1, 2])
  687. assert np.isnan(sparse.iat[2, 1])
  688. assert sparse.iat[2, 0] == orig.iat[2, 0]
  689. assert np.isnan(sparse.iat[-1, -2])
  690. assert sparse.iat[-1, -1] == orig.iat[-1, -1]
  691. def test_iat_fill_value(self):
  692. orig = pd.DataFrame([[1, np.nan, 0],
  693. [2, 3, np.nan],
  694. [0, np.nan, 4],
  695. [0, np.nan, 5]],
  696. index=list('ABCD'), columns=list('xyz'))
  697. sparse = orig.to_sparse(fill_value=0)
  698. assert sparse.iat[0, 0] == orig.iat[0, 0]
  699. assert np.isnan(sparse.iat[1, 2])
  700. assert np.isnan(sparse.iat[2, 1])
  701. assert sparse.iat[2, 0] == orig.iat[2, 0]
  702. assert np.isnan(sparse.iat[-1, -2])
  703. assert sparse.iat[-1, -1] == orig.iat[-1, -1]
  704. def test_take(self):
  705. orig = pd.DataFrame([[1, np.nan, 0],
  706. [2, 3, np.nan],
  707. [0, np.nan, 4],
  708. [0, np.nan, 5]],
  709. columns=list('xyz'))
  710. sparse = orig.to_sparse()
  711. tm.assert_sp_frame_equal(sparse.take([0]),
  712. orig.take([0]).to_sparse())
  713. tm.assert_sp_frame_equal(sparse.take([0, 1]),
  714. orig.take([0, 1]).to_sparse())
  715. tm.assert_sp_frame_equal(sparse.take([-1, -2]),
  716. orig.take([-1, -2]).to_sparse())
  717. def test_take_fill_value(self):
  718. orig = pd.DataFrame([[1, np.nan, 0],
  719. [2, 3, np.nan],
  720. [0, np.nan, 4],
  721. [0, np.nan, 5]],
  722. columns=list('xyz'))
  723. sparse = orig.to_sparse(fill_value=0)
  724. exp = orig.take([0]).to_sparse(fill_value=0)
  725. exp._default_fill_value = np.nan
  726. tm.assert_sp_frame_equal(sparse.take([0]), exp)
  727. exp = orig.take([0, 1]).to_sparse(fill_value=0)
  728. exp._default_fill_value = np.nan
  729. tm.assert_sp_frame_equal(sparse.take([0, 1]), exp)
  730. exp = orig.take([-1, -2]).to_sparse(fill_value=0)
  731. exp._default_fill_value = np.nan
  732. tm.assert_sp_frame_equal(sparse.take([-1, -2]), exp)
  733. def test_reindex(self):
  734. orig = pd.DataFrame([[1, np.nan, 0],
  735. [2, 3, np.nan],
  736. [0, np.nan, 4],
  737. [0, np.nan, 5]],
  738. index=list('ABCD'), columns=list('xyz'))
  739. sparse = orig.to_sparse()
  740. res = sparse.reindex(['A', 'C', 'B'])
  741. exp = orig.reindex(['A', 'C', 'B']).to_sparse()
  742. tm.assert_sp_frame_equal(res, exp)
  743. orig = pd.DataFrame([[np.nan, np.nan, np.nan],
  744. [np.nan, np.nan, np.nan],
  745. [np.nan, np.nan, np.nan],
  746. [np.nan, np.nan, np.nan]],
  747. index=list('ABCD'), columns=list('xyz'))
  748. sparse = orig.to_sparse()
  749. res = sparse.reindex(['A', 'C', 'B'])
  750. exp = orig.reindex(['A', 'C', 'B']).to_sparse()
  751. tm.assert_sp_frame_equal(res, exp)
  752. def test_reindex_fill_value(self):
  753. orig = pd.DataFrame([[1, np.nan, 0],
  754. [2, 3, np.nan],
  755. [0, np.nan, 4],
  756. [0, np.nan, 5]],
  757. index=list('ABCD'), columns=list('xyz'))
  758. sparse = orig.to_sparse(fill_value=0)
  759. res = sparse.reindex(['A', 'C', 'B'])
  760. exp = orig.reindex(['A', 'C', 'B']).to_sparse(fill_value=0)
  761. tm.assert_sp_frame_equal(res, exp)
  762. # all missing
  763. orig = pd.DataFrame([[np.nan, np.nan, np.nan],
  764. [np.nan, np.nan, np.nan],
  765. [np.nan, np.nan, np.nan],
  766. [np.nan, np.nan, np.nan]],
  767. index=list('ABCD'), columns=list('xyz'))
  768. sparse = orig.to_sparse(fill_value=0)
  769. res = sparse.reindex(['A', 'C', 'B'])
  770. exp = orig.reindex(['A', 'C', 'B']).to_sparse(fill_value=0)
  771. tm.assert_sp_frame_equal(res, exp)
  772. # all fill_value
  773. orig = pd.DataFrame([[0, 0, 0],
  774. [0, 0, 0],
  775. [0, 0, 0],
  776. [0, 0, 0]],
  777. index=list('ABCD'), columns=list('xyz'),
  778. dtype=np.int)
  779. sparse = orig.to_sparse(fill_value=0)
  780. res = sparse.reindex(['A', 'C', 'B'])
  781. exp = orig.reindex(['A', 'C', 'B']).to_sparse(fill_value=0)
  782. tm.assert_sp_frame_equal(res, exp)
  783. class TestMultitype(object):
  784. def setup_method(self, method):
  785. self.cols = ['string', 'int', 'float', 'object']
  786. self.string_series = pd.SparseSeries(['a', 'b', 'c'])
  787. self.int_series = pd.SparseSeries([1, 2, 3])
  788. self.float_series = pd.SparseSeries([1.1, 1.2, 1.3])
  789. self.object_series = pd.SparseSeries([[], {}, set()])
  790. self.sdf = pd.SparseDataFrame({
  791. 'string': self.string_series,
  792. 'int': self.int_series,
  793. 'float': self.float_series,
  794. 'object': self.object_series,
  795. })
  796. self.sdf = self.sdf[self.cols]
  797. self.ss = pd.SparseSeries(['a', 1, 1.1, []], index=self.cols)
  798. def test_frame_basic_dtypes(self):
  799. for _, row in self.sdf.iterrows():
  800. assert row.dtype == SparseDtype(object)
  801. tm.assert_sp_series_equal(self.sdf['string'], self.string_series,
  802. check_names=False)
  803. tm.assert_sp_series_equal(self.sdf['int'], self.int_series,
  804. check_names=False)
  805. tm.assert_sp_series_equal(self.sdf['float'], self.float_series,
  806. check_names=False)
  807. tm.assert_sp_series_equal(self.sdf['object'], self.object_series,
  808. check_names=False)
  809. def test_frame_indexing_single(self):
  810. tm.assert_sp_series_equal(self.sdf.iloc[0],
  811. pd.SparseSeries(['a', 1, 1.1, []],
  812. index=self.cols),
  813. check_names=False)
  814. tm.assert_sp_series_equal(self.sdf.iloc[1],
  815. pd.SparseSeries(['b', 2, 1.2, {}],
  816. index=self.cols),
  817. check_names=False)
  818. tm.assert_sp_series_equal(self.sdf.iloc[2],
  819. pd.SparseSeries(['c', 3, 1.3, set()],
  820. index=self.cols),
  821. check_names=False)
  822. def test_frame_indexing_multiple(self):
  823. tm.assert_sp_frame_equal(self.sdf, self.sdf[:])
  824. tm.assert_sp_frame_equal(self.sdf, self.sdf.loc[:])
  825. tm.assert_sp_frame_equal(self.sdf.iloc[[1, 2]],
  826. pd.SparseDataFrame({
  827. 'string': self.string_series.iloc[[1, 2]],
  828. 'int': self.int_series.iloc[[1, 2]],
  829. 'float': self.float_series.iloc[[1, 2]],
  830. 'object': self.object_series.iloc[[1, 2]]
  831. }, index=[1, 2])[self.cols])
  832. tm.assert_sp_frame_equal(self.sdf[['int', 'string']],
  833. pd.SparseDataFrame({
  834. 'int': self.int_series,
  835. 'string': self.string_series,
  836. }))
  837. def test_series_indexing_single(self):
  838. for i, idx in enumerate(self.cols):
  839. assert self.ss.iloc[i] == self.ss[idx]
  840. tm.assert_class_equal(self.ss.iloc[i], self.ss[idx],
  841. obj="series index")
  842. assert self.ss['string'] == 'a'
  843. assert self.ss['int'] == 1
  844. assert self.ss['float'] == 1.1
  845. assert self.ss['object'] == []
  846. def test_series_indexing_multiple(self):
  847. tm.assert_sp_series_equal(self.ss.loc[['string', 'int']],
  848. pd.SparseSeries(['a', 1],
  849. index=['string', 'int']))
  850. tm.assert_sp_series_equal(self.ss.loc[['string', 'object']],
  851. pd.SparseSeries(['a', []],
  852. index=['string', 'object']))