test_numeric.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. # coding=utf-8
  2. # pylint: disable-msg=E1101,W0612
  3. import numpy as np
  4. import pytest
  5. from pandas.compat import lrange, range
  6. import pandas as pd
  7. from pandas import DataFrame, Index, Series
  8. import pandas.util.testing as tm
  9. from pandas.util.testing import assert_series_equal
  10. def test_get():
  11. # GH 6383
  12. s = Series(np.array([43, 48, 60, 48, 50, 51, 50, 45, 57, 48, 56, 45,
  13. 51, 39, 55, 43, 54, 52, 51, 54]))
  14. result = s.get(25, 0)
  15. expected = 0
  16. assert result == expected
  17. s = Series(np.array([43, 48, 60, 48, 50, 51, 50, 45, 57, 48, 56,
  18. 45, 51, 39, 55, 43, 54, 52, 51, 54]),
  19. index=pd.Float64Index(
  20. [25.0, 36.0, 49.0, 64.0, 81.0, 100.0,
  21. 121.0, 144.0, 169.0, 196.0, 1225.0,
  22. 1296.0, 1369.0, 1444.0, 1521.0, 1600.0,
  23. 1681.0, 1764.0, 1849.0, 1936.0],
  24. dtype='object'))
  25. result = s.get(25, 0)
  26. expected = 43
  27. assert result == expected
  28. # GH 7407
  29. # with a boolean accessor
  30. df = pd.DataFrame({'i': [0] * 3, 'b': [False] * 3})
  31. vc = df.i.value_counts()
  32. result = vc.get(99, default='Missing')
  33. assert result == 'Missing'
  34. vc = df.b.value_counts()
  35. result = vc.get(False, default='Missing')
  36. assert result == 3
  37. result = vc.get(True, default='Missing')
  38. assert result == 'Missing'
  39. def test_get_nan():
  40. # GH 8569
  41. s = pd.Float64Index(range(10)).to_series()
  42. assert s.get(np.nan) is None
  43. assert s.get(np.nan, default='Missing') == 'Missing'
  44. def test_get_nan_multiple():
  45. # GH 8569
  46. # ensure that fixing "test_get_nan" above hasn't broken get
  47. # with multiple elements
  48. s = pd.Float64Index(range(10)).to_series()
  49. idx = [2, 30]
  50. with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
  51. assert_series_equal(s.get(idx),
  52. Series([2, np.nan], index=idx))
  53. idx = [2, np.nan]
  54. with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
  55. assert_series_equal(s.get(idx),
  56. Series([2, np.nan], index=idx))
  57. # GH 17295 - all missing keys
  58. idx = [20, 30]
  59. assert(s.get(idx) is None)
  60. idx = [np.nan, np.nan]
  61. assert(s.get(idx) is None)
  62. def test_delitem():
  63. # GH 5542
  64. # should delete the item inplace
  65. s = Series(lrange(5))
  66. del s[0]
  67. expected = Series(lrange(1, 5), index=lrange(1, 5))
  68. assert_series_equal(s, expected)
  69. del s[1]
  70. expected = Series(lrange(2, 5), index=lrange(2, 5))
  71. assert_series_equal(s, expected)
  72. # empty
  73. s = Series()
  74. with pytest.raises(KeyError, match=r"^0$"):
  75. del s[0]
  76. # only 1 left, del, add, del
  77. s = Series(1)
  78. del s[0]
  79. assert_series_equal(s, Series(dtype='int64', index=Index(
  80. [], dtype='int64')))
  81. s[0] = 1
  82. assert_series_equal(s, Series(1))
  83. del s[0]
  84. assert_series_equal(s, Series(dtype='int64', index=Index(
  85. [], dtype='int64')))
  86. # Index(dtype=object)
  87. s = Series(1, index=['a'])
  88. del s['a']
  89. assert_series_equal(s, Series(dtype='int64', index=Index(
  90. [], dtype='object')))
  91. s['a'] = 1
  92. assert_series_equal(s, Series(1, index=['a']))
  93. del s['a']
  94. assert_series_equal(s, Series(dtype='int64', index=Index(
  95. [], dtype='object')))
  96. def test_slice_float64():
  97. values = np.arange(10., 50., 2)
  98. index = Index(values)
  99. start, end = values[[5, 15]]
  100. s = Series(np.random.randn(20), index=index)
  101. result = s[start:end]
  102. expected = s.iloc[5:16]
  103. assert_series_equal(result, expected)
  104. result = s.loc[start:end]
  105. assert_series_equal(result, expected)
  106. df = DataFrame(np.random.randn(20, 3), index=index)
  107. result = df[start:end]
  108. expected = df.iloc[5:16]
  109. tm.assert_frame_equal(result, expected)
  110. result = df.loc[start:end]
  111. tm.assert_frame_equal(result, expected)
  112. def test_getitem_negative_out_of_bounds():
  113. s = Series(tm.rands_array(5, 10), index=tm.rands_array(10, 10))
  114. msg = "index out of bounds"
  115. with pytest.raises(IndexError, match=msg):
  116. s[-11]
  117. msg = "index -11 is out of bounds for axis 0 with size 10"
  118. with pytest.raises(IndexError, match=msg):
  119. s[-11] = 'foo'
  120. def test_getitem_regression():
  121. s = Series(lrange(5), index=lrange(5))
  122. result = s[lrange(5)]
  123. assert_series_equal(result, s)
  124. def test_getitem_setitem_slice_bug():
  125. s = Series(lrange(10), lrange(10))
  126. result = s[-12:]
  127. assert_series_equal(result, s)
  128. result = s[-7:]
  129. assert_series_equal(result, s[3:])
  130. result = s[:-12]
  131. assert_series_equal(result, s[:0])
  132. s = Series(lrange(10), lrange(10))
  133. s[-12:] = 0
  134. assert (s == 0).all()
  135. s[:-12] = 5
  136. assert (s == 0).all()
  137. def test_getitem_setitem_slice_integers():
  138. s = Series(np.random.randn(8), index=[2, 4, 6, 8, 10, 12, 14, 16])
  139. result = s[:4]
  140. expected = s.reindex([2, 4, 6, 8])
  141. assert_series_equal(result, expected)
  142. s[:4] = 0
  143. assert (s[:4] == 0).all()
  144. assert not (s[4:] == 0).any()
  145. def test_setitem_float_labels():
  146. # note labels are floats
  147. s = Series(['a', 'b', 'c'], index=[0, 0.5, 1])
  148. tmp = s.copy()
  149. s.loc[1] = 'zoo'
  150. tmp.iloc[2] = 'zoo'
  151. assert_series_equal(s, tmp)
  152. def test_slice_float_get_set(test_data):
  153. msg = (r"cannot do slice indexing on <class 'pandas\.core\.indexes"
  154. r"\.datetimes\.DatetimeIndex'> with these indexers \[{key}\]"
  155. r" of <(class|type) 'float'>")
  156. with pytest.raises(TypeError, match=msg.format(key=r"4\.0")):
  157. test_data.ts[4.0:10.0]
  158. with pytest.raises(TypeError, match=msg.format(key=r"4\.0")):
  159. test_data.ts[4.0:10.0] = 0
  160. with pytest.raises(TypeError, match=msg.format(key=r"4\.5")):
  161. test_data.ts[4.5:10.0]
  162. with pytest.raises(TypeError, match=msg.format(key=r"4\.5")):
  163. test_data.ts[4.5:10.0] = 0
  164. def test_slice_floats2():
  165. s = Series(np.random.rand(10), index=np.arange(10, 20, dtype=float))
  166. assert len(s.loc[12.0:]) == 8
  167. assert len(s.loc[12.5:]) == 7
  168. i = np.arange(10, 20, dtype=float)
  169. i[2] = 12.2
  170. s.index = i
  171. assert len(s.loc[12.0:]) == 8
  172. assert len(s.loc[12.5:]) == 7
  173. def test_int_indexing():
  174. s = Series(np.random.randn(6), index=[0, 0, 1, 1, 2, 2])
  175. with pytest.raises(KeyError, match=r"^5$"):
  176. s[5]
  177. with pytest.raises(KeyError, match=r"^'c'$"):
  178. s['c']
  179. # not monotonic
  180. s = Series(np.random.randn(6), index=[2, 2, 0, 0, 1, 1])
  181. with pytest.raises(KeyError, match=r"^5$"):
  182. s[5]
  183. with pytest.raises(KeyError, match=r"^'c'$"):
  184. s['c']
  185. def test_getitem_int64(test_data):
  186. idx = np.int64(5)
  187. assert test_data.ts[idx] == test_data.ts[5]