test_qcut.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. import os
  2. import numpy as np
  3. import pytest
  4. import pandas as pd
  5. from pandas import (
  6. Categorical,
  7. DatetimeIndex,
  8. Interval,
  9. IntervalIndex,
  10. NaT,
  11. Series,
  12. TimedeltaIndex,
  13. Timestamp,
  14. cut,
  15. date_range,
  16. isna,
  17. qcut,
  18. timedelta_range,
  19. )
  20. import pandas._testing as tm
  21. from pandas.api.types import CategoricalDtype as CDT
  22. from pandas.tseries.offsets import (
  23. Day,
  24. Nano,
  25. )
  26. def test_qcut():
  27. arr = np.random.randn(1000)
  28. # We store the bins as Index that have been
  29. # rounded to comparisons are a bit tricky.
  30. labels, _ = qcut(arr, 4, retbins=True)
  31. ex_bins = np.quantile(arr, [0, 0.25, 0.5, 0.75, 1.0])
  32. result = labels.categories.left.values
  33. assert np.allclose(result, ex_bins[:-1], atol=1e-2)
  34. result = labels.categories.right.values
  35. assert np.allclose(result, ex_bins[1:], atol=1e-2)
  36. ex_levels = cut(arr, ex_bins, include_lowest=True)
  37. tm.assert_categorical_equal(labels, ex_levels)
  38. def test_qcut_bounds():
  39. arr = np.random.randn(1000)
  40. factor = qcut(arr, 10, labels=False)
  41. assert len(np.unique(factor)) == 10
  42. def test_qcut_specify_quantiles():
  43. arr = np.random.randn(100)
  44. factor = qcut(arr, [0, 0.25, 0.5, 0.75, 1.0])
  45. expected = qcut(arr, 4)
  46. tm.assert_categorical_equal(factor, expected)
  47. def test_qcut_all_bins_same():
  48. with pytest.raises(ValueError, match="edges.*unique"):
  49. qcut([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 3)
  50. def test_qcut_include_lowest():
  51. values = np.arange(10)
  52. ii = qcut(values, 4)
  53. ex_levels = IntervalIndex(
  54. [
  55. Interval(-0.001, 2.25),
  56. Interval(2.25, 4.5),
  57. Interval(4.5, 6.75),
  58. Interval(6.75, 9),
  59. ]
  60. )
  61. tm.assert_index_equal(ii.categories, ex_levels)
  62. def test_qcut_nas():
  63. arr = np.random.randn(100)
  64. arr[:20] = np.nan
  65. result = qcut(arr, 4)
  66. assert isna(result[:20]).all()
  67. def test_qcut_index():
  68. result = qcut([0, 2], 2)
  69. intervals = [Interval(-0.001, 1), Interval(1, 2)]
  70. expected = Categorical(intervals, ordered=True)
  71. tm.assert_categorical_equal(result, expected)
  72. def test_qcut_binning_issues(datapath):
  73. # see gh-1978, gh-1979
  74. cut_file = datapath(os.path.join("reshape", "data", "cut_data.csv"))
  75. arr = np.loadtxt(cut_file)
  76. result = qcut(arr, 20)
  77. starts = []
  78. ends = []
  79. for lev in np.unique(result):
  80. s = lev.left
  81. e = lev.right
  82. assert s != e
  83. starts.append(float(s))
  84. ends.append(float(e))
  85. for (sp, sn), (ep, en) in zip(
  86. zip(starts[:-1], starts[1:]), zip(ends[:-1], ends[1:])
  87. ):
  88. assert sp < sn
  89. assert ep < en
  90. assert ep <= sn
  91. def test_qcut_return_intervals():
  92. ser = Series([0, 1, 2, 3, 4, 5, 6, 7, 8])
  93. res = qcut(ser, [0, 0.333, 0.666, 1])
  94. exp_levels = np.array(
  95. [Interval(-0.001, 2.664), Interval(2.664, 5.328), Interval(5.328, 8)]
  96. )
  97. exp = Series(exp_levels.take([0, 0, 0, 1, 1, 1, 2, 2, 2])).astype(CDT(ordered=True))
  98. tm.assert_series_equal(res, exp)
  99. @pytest.mark.parametrize("labels", ["foo", 1, True])
  100. def test_qcut_incorrect_labels(labels):
  101. # GH 13318
  102. values = range(5)
  103. msg = "Bin labels must either be False, None or passed in as a list-like argument"
  104. with pytest.raises(ValueError, match=msg):
  105. qcut(values, 4, labels=labels)
  106. @pytest.mark.parametrize("labels", [["a", "b", "c"], list(range(3))])
  107. def test_qcut_wrong_length_labels(labels):
  108. # GH 13318
  109. values = range(10)
  110. msg = "Bin labels must be one fewer than the number of bin edges"
  111. with pytest.raises(ValueError, match=msg):
  112. qcut(values, 4, labels=labels)
  113. @pytest.mark.parametrize(
  114. "labels, expected",
  115. [
  116. (["a", "b", "c"], Categorical(["a", "b", "c"], ordered=True)),
  117. (list(range(3)), Categorical([0, 1, 2], ordered=True)),
  118. ],
  119. )
  120. def test_qcut_list_like_labels(labels, expected):
  121. # GH 13318
  122. values = range(3)
  123. result = qcut(values, 3, labels=labels)
  124. tm.assert_categorical_equal(result, expected)
  125. @pytest.mark.parametrize(
  126. "kwargs,msg",
  127. [
  128. ({"duplicates": "drop"}, None),
  129. ({}, "Bin edges must be unique"),
  130. ({"duplicates": "raise"}, "Bin edges must be unique"),
  131. ({"duplicates": "foo"}, "invalid value for 'duplicates' parameter"),
  132. ],
  133. )
  134. def test_qcut_duplicates_bin(kwargs, msg):
  135. # see gh-7751
  136. values = [0, 0, 0, 0, 1, 2, 3]
  137. if msg is not None:
  138. with pytest.raises(ValueError, match=msg):
  139. qcut(values, 3, **kwargs)
  140. else:
  141. result = qcut(values, 3, **kwargs)
  142. expected = IntervalIndex([Interval(-0.001, 1), Interval(1, 3)])
  143. tm.assert_index_equal(result.categories, expected)
  144. @pytest.mark.parametrize(
  145. "data,start,end", [(9.0, 8.999, 9.0), (0.0, -0.001, 0.0), (-9.0, -9.001, -9.0)]
  146. )
  147. @pytest.mark.parametrize("length", [1, 2])
  148. @pytest.mark.parametrize("labels", [None, False])
  149. def test_single_quantile(data, start, end, length, labels):
  150. # see gh-15431
  151. ser = Series([data] * length)
  152. result = qcut(ser, 1, labels=labels)
  153. if labels is None:
  154. intervals = IntervalIndex([Interval(start, end)] * length, closed="right")
  155. expected = Series(intervals).astype(CDT(ordered=True))
  156. else:
  157. expected = Series([0] * length, dtype=np.intp)
  158. tm.assert_series_equal(result, expected)
  159. @pytest.mark.parametrize(
  160. "ser",
  161. [
  162. Series(DatetimeIndex(["20180101", NaT, "20180103"])),
  163. Series(TimedeltaIndex(["0 days", NaT, "2 days"])),
  164. ],
  165. ids=lambda x: str(x.dtype),
  166. )
  167. def test_qcut_nat(ser):
  168. # see gh-19768
  169. intervals = IntervalIndex.from_tuples(
  170. [(ser[0] - Nano(), ser[2] - Day()), np.nan, (ser[2] - Day(), ser[2])]
  171. )
  172. expected = Series(Categorical(intervals, ordered=True))
  173. result = qcut(ser, 2)
  174. tm.assert_series_equal(result, expected)
  175. @pytest.mark.parametrize("bins", [3, np.linspace(0, 1, 4)])
  176. def test_datetime_tz_qcut(bins):
  177. # see gh-19872
  178. tz = "US/Eastern"
  179. ser = Series(date_range("20130101", periods=3, tz=tz))
  180. result = qcut(ser, bins)
  181. expected = Series(
  182. IntervalIndex(
  183. [
  184. Interval(
  185. Timestamp("2012-12-31 23:59:59.999999999", tz=tz),
  186. Timestamp("2013-01-01 16:00:00", tz=tz),
  187. ),
  188. Interval(
  189. Timestamp("2013-01-01 16:00:00", tz=tz),
  190. Timestamp("2013-01-02 08:00:00", tz=tz),
  191. ),
  192. Interval(
  193. Timestamp("2013-01-02 08:00:00", tz=tz),
  194. Timestamp("2013-01-03 00:00:00", tz=tz),
  195. ),
  196. ]
  197. )
  198. ).astype(CDT(ordered=True))
  199. tm.assert_series_equal(result, expected)
  200. @pytest.mark.parametrize(
  201. "arg,expected_bins",
  202. [
  203. [
  204. timedelta_range("1day", periods=3),
  205. TimedeltaIndex(["1 days", "2 days", "3 days"]),
  206. ],
  207. [
  208. date_range("20180101", periods=3),
  209. DatetimeIndex(["2018-01-01", "2018-01-02", "2018-01-03"]),
  210. ],
  211. ],
  212. )
  213. def test_date_like_qcut_bins(arg, expected_bins):
  214. # see gh-19891
  215. ser = Series(arg)
  216. result, result_bins = qcut(ser, 2, retbins=True)
  217. tm.assert_index_equal(result_bins, expected_bins)
  218. @pytest.mark.parametrize("bins", [6, 7])
  219. @pytest.mark.parametrize(
  220. "box, compare",
  221. [
  222. (Series, tm.assert_series_equal),
  223. (np.array, tm.assert_categorical_equal),
  224. (list, tm.assert_equal),
  225. ],
  226. )
  227. def test_qcut_bool_coercion_to_int(bins, box, compare):
  228. # issue 20303
  229. data_expected = box([0, 1, 1, 0, 1] * 10)
  230. data_result = box([False, True, True, False, True] * 10)
  231. expected = qcut(data_expected, bins, duplicates="drop")
  232. result = qcut(data_result, bins, duplicates="drop")
  233. compare(result, expected)
  234. @pytest.mark.parametrize("q", [2, 5, 10])
  235. def test_qcut_nullable_integer(q, any_numeric_ea_dtype):
  236. arr = pd.array(np.arange(100), dtype=any_numeric_ea_dtype)
  237. arr[::2] = pd.NA
  238. result = qcut(arr, q)
  239. expected = qcut(arr.astype(float), q)
  240. tm.assert_categorical_equal(result, expected)