test_loc.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # coding=utf-8
  2. # pylint: disable-msg=E1101,W0612
  3. import numpy as np
  4. import pytest
  5. from pandas.compat import lrange
  6. import pandas as pd
  7. from pandas import Series, Timestamp
  8. from pandas.util.testing import assert_series_equal
  9. @pytest.mark.parametrize("val,expected", [
  10. (2**63 - 1, 3),
  11. (2**63, 4),
  12. ])
  13. def test_loc_uint64(val, expected):
  14. # see gh-19399
  15. s = Series({2**63 - 1: 3, 2**63: 4})
  16. assert s.loc[val] == expected
  17. def test_loc_getitem(test_data):
  18. inds = test_data.series.index[[3, 4, 7]]
  19. assert_series_equal(
  20. test_data.series.loc[inds],
  21. test_data.series.reindex(inds))
  22. assert_series_equal(test_data.series.iloc[5::2], test_data.series[5::2])
  23. # slice with indices
  24. d1, d2 = test_data.ts.index[[5, 15]]
  25. result = test_data.ts.loc[d1:d2]
  26. expected = test_data.ts.truncate(d1, d2)
  27. assert_series_equal(result, expected)
  28. # boolean
  29. mask = test_data.series > test_data.series.median()
  30. assert_series_equal(test_data.series.loc[mask], test_data.series[mask])
  31. # ask for index value
  32. assert test_data.ts.loc[d1] == test_data.ts[d1]
  33. assert test_data.ts.loc[d2] == test_data.ts[d2]
  34. def test_loc_getitem_not_monotonic(test_data):
  35. d1, d2 = test_data.ts.index[[5, 15]]
  36. ts2 = test_data.ts[::2][[1, 2, 0]]
  37. msg = r"Timestamp\('2000-01-10 00:00:00'\)"
  38. with pytest.raises(KeyError, match=msg):
  39. ts2.loc[d1:d2]
  40. with pytest.raises(KeyError, match=msg):
  41. ts2.loc[d1:d2] = 0
  42. def test_loc_getitem_setitem_integer_slice_keyerrors():
  43. s = Series(np.random.randn(10), index=lrange(0, 20, 2))
  44. # this is OK
  45. cp = s.copy()
  46. cp.iloc[4:10] = 0
  47. assert (cp.iloc[4:10] == 0).all()
  48. # so is this
  49. cp = s.copy()
  50. cp.iloc[3:11] = 0
  51. assert (cp.iloc[3:11] == 0).values.all()
  52. result = s.iloc[2:6]
  53. result2 = s.loc[3:11]
  54. expected = s.reindex([4, 6, 8, 10])
  55. assert_series_equal(result, expected)
  56. assert_series_equal(result2, expected)
  57. # non-monotonic, raise KeyError
  58. s2 = s.iloc[lrange(5) + lrange(5, 10)[::-1]]
  59. with pytest.raises(KeyError, match=r"^3L?$"):
  60. s2.loc[3:11]
  61. with pytest.raises(KeyError, match=r"^3L?$"):
  62. s2.loc[3:11] = 0
  63. def test_loc_getitem_iterator(test_data):
  64. idx = iter(test_data.series.index[:10])
  65. result = test_data.series.loc[idx]
  66. assert_series_equal(result, test_data.series[:10])
  67. def test_loc_setitem_boolean(test_data):
  68. mask = test_data.series > test_data.series.median()
  69. result = test_data.series.copy()
  70. result.loc[mask] = 0
  71. expected = test_data.series
  72. expected[mask] = 0
  73. assert_series_equal(result, expected)
  74. def test_loc_setitem_corner(test_data):
  75. inds = list(test_data.series.index[[5, 8, 12]])
  76. test_data.series.loc[inds] = 5
  77. msg = r"\['foo'\] not in index"
  78. with pytest.raises(KeyError, match=msg):
  79. test_data.series.loc[inds + ['foo']] = 5
  80. def test_basic_setitem_with_labels(test_data):
  81. indices = test_data.ts.index[[5, 10, 15]]
  82. cp = test_data.ts.copy()
  83. exp = test_data.ts.copy()
  84. cp[indices] = 0
  85. exp.loc[indices] = 0
  86. assert_series_equal(cp, exp)
  87. cp = test_data.ts.copy()
  88. exp = test_data.ts.copy()
  89. cp[indices[0]:indices[2]] = 0
  90. exp.loc[indices[0]:indices[2]] = 0
  91. assert_series_equal(cp, exp)
  92. # integer indexes, be careful
  93. s = Series(np.random.randn(10), index=lrange(0, 20, 2))
  94. inds = [0, 4, 6]
  95. arr_inds = np.array([0, 4, 6])
  96. cp = s.copy()
  97. exp = s.copy()
  98. s[inds] = 0
  99. s.loc[inds] = 0
  100. assert_series_equal(cp, exp)
  101. cp = s.copy()
  102. exp = s.copy()
  103. s[arr_inds] = 0
  104. s.loc[arr_inds] = 0
  105. assert_series_equal(cp, exp)
  106. inds_notfound = [0, 4, 5, 6]
  107. arr_inds_notfound = np.array([0, 4, 5, 6])
  108. msg = r"\[5\] not contained in the index"
  109. with pytest.raises(ValueError, match=msg):
  110. s[inds_notfound] = 0
  111. with pytest.raises(Exception, match=msg):
  112. s[arr_inds_notfound] = 0
  113. # GH12089
  114. # with tz for values
  115. s = Series(pd.date_range("2011-01-01", periods=3, tz="US/Eastern"),
  116. index=['a', 'b', 'c'])
  117. s2 = s.copy()
  118. expected = Timestamp('2011-01-03', tz='US/Eastern')
  119. s2.loc['a'] = expected
  120. result = s2.loc['a']
  121. assert result == expected
  122. s2 = s.copy()
  123. s2.iloc[0] = expected
  124. result = s2.iloc[0]
  125. assert result == expected
  126. s2 = s.copy()
  127. s2['a'] = expected
  128. result = s2['a']
  129. assert result == expected