test_get.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import numpy as np
  2. import pytest
  3. import pandas as pd
  4. from pandas import (
  5. Index,
  6. Series,
  7. )
  8. import pandas._testing as tm
  9. def test_get():
  10. # GH 6383
  11. s = Series(
  12. np.array(
  13. [
  14. 43,
  15. 48,
  16. 60,
  17. 48,
  18. 50,
  19. 51,
  20. 50,
  21. 45,
  22. 57,
  23. 48,
  24. 56,
  25. 45,
  26. 51,
  27. 39,
  28. 55,
  29. 43,
  30. 54,
  31. 52,
  32. 51,
  33. 54,
  34. ]
  35. )
  36. )
  37. result = s.get(25, 0)
  38. expected = 0
  39. assert result == expected
  40. s = Series(
  41. np.array(
  42. [
  43. 43,
  44. 48,
  45. 60,
  46. 48,
  47. 50,
  48. 51,
  49. 50,
  50. 45,
  51. 57,
  52. 48,
  53. 56,
  54. 45,
  55. 51,
  56. 39,
  57. 55,
  58. 43,
  59. 54,
  60. 52,
  61. 51,
  62. 54,
  63. ]
  64. ),
  65. index=Index(
  66. [
  67. 25.0,
  68. 36.0,
  69. 49.0,
  70. 64.0,
  71. 81.0,
  72. 100.0,
  73. 121.0,
  74. 144.0,
  75. 169.0,
  76. 196.0,
  77. 1225.0,
  78. 1296.0,
  79. 1369.0,
  80. 1444.0,
  81. 1521.0,
  82. 1600.0,
  83. 1681.0,
  84. 1764.0,
  85. 1849.0,
  86. 1936.0,
  87. ],
  88. dtype=np.float64,
  89. ),
  90. )
  91. result = s.get(25, 0)
  92. expected = 43
  93. assert result == expected
  94. # GH 7407
  95. # with a boolean accessor
  96. df = pd.DataFrame({"i": [0] * 3, "b": [False] * 3})
  97. vc = df.i.value_counts()
  98. result = vc.get(99, default="Missing")
  99. assert result == "Missing"
  100. vc = df.b.value_counts()
  101. result = vc.get(False, default="Missing")
  102. assert result == 3
  103. result = vc.get(True, default="Missing")
  104. assert result == "Missing"
  105. def test_get_nan(float_numpy_dtype):
  106. # GH 8569
  107. s = Index(range(10), dtype=float_numpy_dtype).to_series()
  108. assert s.get(np.nan) is None
  109. assert s.get(np.nan, default="Missing") == "Missing"
  110. def test_get_nan_multiple(float_numpy_dtype):
  111. # GH 8569
  112. # ensure that fixing "test_get_nan" above hasn't broken get
  113. # with multiple elements
  114. s = Index(range(10), dtype=float_numpy_dtype).to_series()
  115. idx = [2, 30]
  116. assert s.get(idx) is None
  117. idx = [2, np.nan]
  118. assert s.get(idx) is None
  119. # GH 17295 - all missing keys
  120. idx = [20, 30]
  121. assert s.get(idx) is None
  122. idx = [np.nan, np.nan]
  123. assert s.get(idx) is None
  124. def test_get_with_default():
  125. # GH#7725
  126. d0 = ["a", "b", "c", "d"]
  127. d1 = np.arange(4, dtype="int64")
  128. others = ["e", 10]
  129. for data, index in ((d0, d1), (d1, d0)):
  130. s = Series(data, index=index)
  131. for i, d in zip(index, data):
  132. assert s.get(i) == d
  133. assert s.get(i, d) == d
  134. assert s.get(i, "z") == d
  135. for other in others:
  136. assert s.get(other, "z") == "z"
  137. assert s.get(other, other) == other
  138. @pytest.mark.parametrize(
  139. "arr",
  140. [np.random.randn(10), tm.makeDateIndex(10, name="a").tz_localize(tz="US/Eastern")],
  141. )
  142. def test_get_with_ea(arr):
  143. # GH#21260
  144. ser = Series(arr, index=[2 * i for i in range(len(arr))])
  145. assert ser.get(4) == ser.iloc[2]
  146. result = ser.get([4, 6])
  147. expected = ser.iloc[[2, 3]]
  148. tm.assert_series_equal(result, expected)
  149. result = ser.get(slice(2))
  150. expected = ser.iloc[[0, 1]]
  151. tm.assert_series_equal(result, expected)
  152. assert ser.get(-1) is None
  153. assert ser.get(ser.index.max() + 1) is None
  154. ser = Series(arr[:6], index=list("abcdef"))
  155. assert ser.get("c") == ser.iloc[2]
  156. result = ser.get(slice("b", "d"))
  157. expected = ser.iloc[[1, 2, 3]]
  158. tm.assert_series_equal(result, expected)
  159. result = ser.get("Z")
  160. assert result is None
  161. assert ser.get(4) == ser.iloc[4]
  162. assert ser.get(-1) == ser.iloc[-1]
  163. assert ser.get(len(ser)) is None
  164. # GH#21257
  165. ser = Series(arr)
  166. ser2 = ser[::2]
  167. assert ser2.get(1) is None
  168. def test_getitem_get(string_series, object_series):
  169. for obj in [string_series, object_series]:
  170. idx = obj.index[5]
  171. assert obj[idx] == obj.get(idx)
  172. assert obj[idx] == obj[5]
  173. assert string_series.get(-1) == string_series.get(string_series.index[-1])
  174. assert string_series[5] == string_series.get(string_series.index[5])
  175. def test_get_none():
  176. # GH#5652
  177. s1 = Series(dtype=object)
  178. s2 = Series(dtype=object, index=list("abc"))
  179. for s in [s1, s2]:
  180. result = s.get(None)
  181. assert result is None