test_str.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. from itertools import chain
  2. import operator
  3. import numpy as np
  4. import pytest
  5. from pandas.core.dtypes.common import is_number
  6. from pandas import (
  7. DataFrame,
  8. Series,
  9. )
  10. import pandas._testing as tm
  11. from pandas.tests.apply.common import (
  12. frame_transform_kernels,
  13. series_transform_kernels,
  14. )
  15. @pytest.mark.parametrize("func", ["sum", "mean", "min", "max", "std"])
  16. @pytest.mark.parametrize(
  17. "args,kwds",
  18. [
  19. pytest.param([], {}, id="no_args_or_kwds"),
  20. pytest.param([1], {}, id="axis_from_args"),
  21. pytest.param([], {"axis": 1}, id="axis_from_kwds"),
  22. pytest.param([], {"numeric_only": True}, id="optional_kwds"),
  23. pytest.param([1, True], {"numeric_only": True}, id="args_and_kwds"),
  24. ],
  25. )
  26. @pytest.mark.parametrize("how", ["agg", "apply"])
  27. def test_apply_with_string_funcs(request, float_frame, func, args, kwds, how):
  28. if len(args) > 1 and how == "agg":
  29. request.node.add_marker(
  30. pytest.mark.xfail(
  31. raises=TypeError,
  32. reason="agg/apply signature mismatch - agg passes 2nd "
  33. "argument to func",
  34. )
  35. )
  36. result = getattr(float_frame, how)(func, *args, **kwds)
  37. expected = getattr(float_frame, func)(*args, **kwds)
  38. tm.assert_series_equal(result, expected)
  39. @pytest.mark.parametrize("arg", ["sum", "mean", "min", "max", "std"])
  40. def test_with_string_args(datetime_series, arg):
  41. result = datetime_series.apply(arg)
  42. expected = getattr(datetime_series, arg)()
  43. assert result == expected
  44. @pytest.mark.parametrize("op", ["mean", "median", "std", "var"])
  45. @pytest.mark.parametrize("how", ["agg", "apply"])
  46. def test_apply_np_reducer(op, how):
  47. # GH 39116
  48. float_frame = DataFrame({"a": [1, 2], "b": [3, 4]})
  49. result = getattr(float_frame, how)(op)
  50. # pandas ddof defaults to 1, numpy to 0
  51. kwargs = {"ddof": 1} if op in ("std", "var") else {}
  52. expected = Series(
  53. getattr(np, op)(float_frame, axis=0, **kwargs), index=float_frame.columns
  54. )
  55. tm.assert_series_equal(result, expected)
  56. @pytest.mark.parametrize(
  57. "op", ["abs", "ceil", "cos", "cumsum", "exp", "log", "sqrt", "square"]
  58. )
  59. @pytest.mark.parametrize("how", ["transform", "apply"])
  60. def test_apply_np_transformer(float_frame, op, how):
  61. # GH 39116
  62. # float_frame will _usually_ have negative values, which will
  63. # trigger the warning here, but let's put one in just to be sure
  64. float_frame.iloc[0, 0] = -1.0
  65. warn = None
  66. if op in ["log", "sqrt"]:
  67. warn = RuntimeWarning
  68. with tm.assert_produces_warning(warn, check_stacklevel=False):
  69. # float_frame fixture is defined in conftest.py, so we don't check the
  70. # stacklevel as otherwise the test would fail.
  71. result = getattr(float_frame, how)(op)
  72. expected = getattr(np, op)(float_frame)
  73. tm.assert_frame_equal(result, expected)
  74. @pytest.mark.parametrize(
  75. "series, func, expected",
  76. chain(
  77. tm.get_cython_table_params(
  78. Series(dtype=np.float64),
  79. [
  80. ("sum", 0),
  81. ("max", np.nan),
  82. ("min", np.nan),
  83. ("all", True),
  84. ("any", False),
  85. ("mean", np.nan),
  86. ("prod", 1),
  87. ("std", np.nan),
  88. ("var", np.nan),
  89. ("median", np.nan),
  90. ],
  91. ),
  92. tm.get_cython_table_params(
  93. Series([np.nan, 1, 2, 3]),
  94. [
  95. ("sum", 6),
  96. ("max", 3),
  97. ("min", 1),
  98. ("all", True),
  99. ("any", True),
  100. ("mean", 2),
  101. ("prod", 6),
  102. ("std", 1),
  103. ("var", 1),
  104. ("median", 2),
  105. ],
  106. ),
  107. tm.get_cython_table_params(
  108. Series("a b c".split()),
  109. [
  110. ("sum", "abc"),
  111. ("max", "c"),
  112. ("min", "a"),
  113. ("all", True),
  114. ("any", True),
  115. ],
  116. ),
  117. ),
  118. )
  119. def test_agg_cython_table_series(series, func, expected):
  120. # GH21224
  121. # test reducing functions in
  122. # pandas.core.base.SelectionMixin._cython_table
  123. result = series.agg(func)
  124. if is_number(expected):
  125. assert np.isclose(result, expected, equal_nan=True)
  126. else:
  127. assert result == expected
  128. @pytest.mark.parametrize(
  129. "series, func, expected",
  130. chain(
  131. tm.get_cython_table_params(
  132. Series(dtype=np.float64),
  133. [
  134. ("cumprod", Series([], dtype=np.float64)),
  135. ("cumsum", Series([], dtype=np.float64)),
  136. ],
  137. ),
  138. tm.get_cython_table_params(
  139. Series([np.nan, 1, 2, 3]),
  140. [
  141. ("cumprod", Series([np.nan, 1, 2, 6])),
  142. ("cumsum", Series([np.nan, 1, 3, 6])),
  143. ],
  144. ),
  145. tm.get_cython_table_params(
  146. Series("a b c".split()), [("cumsum", Series(["a", "ab", "abc"]))]
  147. ),
  148. ),
  149. )
  150. def test_agg_cython_table_transform_series(series, func, expected):
  151. # GH21224
  152. # test transforming functions in
  153. # pandas.core.base.SelectionMixin._cython_table (cumprod, cumsum)
  154. result = series.agg(func)
  155. tm.assert_series_equal(result, expected)
  156. @pytest.mark.parametrize(
  157. "df, func, expected",
  158. chain(
  159. tm.get_cython_table_params(
  160. DataFrame(),
  161. [
  162. ("sum", Series(dtype="float64")),
  163. ("max", Series(dtype="float64")),
  164. ("min", Series(dtype="float64")),
  165. ("all", Series(dtype=bool)),
  166. ("any", Series(dtype=bool)),
  167. ("mean", Series(dtype="float64")),
  168. ("prod", Series(dtype="float64")),
  169. ("std", Series(dtype="float64")),
  170. ("var", Series(dtype="float64")),
  171. ("median", Series(dtype="float64")),
  172. ],
  173. ),
  174. tm.get_cython_table_params(
  175. DataFrame([[np.nan, 1], [1, 2]]),
  176. [
  177. ("sum", Series([1.0, 3])),
  178. ("max", Series([1.0, 2])),
  179. ("min", Series([1.0, 1])),
  180. ("all", Series([True, True])),
  181. ("any", Series([True, True])),
  182. ("mean", Series([1, 1.5])),
  183. ("prod", Series([1.0, 2])),
  184. ("std", Series([np.nan, 0.707107])),
  185. ("var", Series([np.nan, 0.5])),
  186. ("median", Series([1, 1.5])),
  187. ],
  188. ),
  189. ),
  190. )
  191. def test_agg_cython_table_frame(df, func, expected, axis):
  192. # GH 21224
  193. # test reducing functions in
  194. # pandas.core.base.SelectionMixin._cython_table
  195. result = df.agg(func, axis=axis)
  196. tm.assert_series_equal(result, expected)
  197. @pytest.mark.parametrize(
  198. "df, func, expected",
  199. chain(
  200. tm.get_cython_table_params(
  201. DataFrame(), [("cumprod", DataFrame()), ("cumsum", DataFrame())]
  202. ),
  203. tm.get_cython_table_params(
  204. DataFrame([[np.nan, 1], [1, 2]]),
  205. [
  206. ("cumprod", DataFrame([[np.nan, 1], [1, 2]])),
  207. ("cumsum", DataFrame([[np.nan, 1], [1, 3]])),
  208. ],
  209. ),
  210. ),
  211. )
  212. def test_agg_cython_table_transform_frame(df, func, expected, axis):
  213. # GH 21224
  214. # test transforming functions in
  215. # pandas.core.base.SelectionMixin._cython_table (cumprod, cumsum)
  216. if axis in ("columns", 1):
  217. # operating blockwise doesn't let us preserve dtypes
  218. expected = expected.astype("float64")
  219. result = df.agg(func, axis=axis)
  220. tm.assert_frame_equal(result, expected)
  221. @pytest.mark.parametrize("op", series_transform_kernels)
  222. def test_transform_groupby_kernel_series(request, string_series, op):
  223. # GH 35964
  224. if op == "ngroup":
  225. request.node.add_marker(
  226. pytest.mark.xfail(raises=ValueError, reason="ngroup not valid for NDFrame")
  227. )
  228. args = [0.0] if op == "fillna" else []
  229. ones = np.ones(string_series.shape[0])
  230. expected = string_series.groupby(ones).transform(op, *args)
  231. result = string_series.transform(op, 0, *args)
  232. tm.assert_series_equal(result, expected)
  233. @pytest.mark.parametrize("op", frame_transform_kernels)
  234. def test_transform_groupby_kernel_frame(request, axis, float_frame, op):
  235. if op == "ngroup":
  236. request.node.add_marker(
  237. pytest.mark.xfail(raises=ValueError, reason="ngroup not valid for NDFrame")
  238. )
  239. # GH 35964
  240. args = [0.0] if op == "fillna" else []
  241. if axis in (0, "index"):
  242. ones = np.ones(float_frame.shape[0])
  243. else:
  244. ones = np.ones(float_frame.shape[1])
  245. expected = float_frame.groupby(ones, axis=axis).transform(op, *args)
  246. result = float_frame.transform(op, axis, *args)
  247. tm.assert_frame_equal(result, expected)
  248. # same thing, but ensuring we have multiple blocks
  249. assert "E" not in float_frame.columns
  250. float_frame["E"] = float_frame["A"].copy()
  251. assert len(float_frame._mgr.arrays) > 1
  252. if axis in (0, "index"):
  253. ones = np.ones(float_frame.shape[0])
  254. else:
  255. ones = np.ones(float_frame.shape[1])
  256. expected2 = float_frame.groupby(ones, axis=axis).transform(op, *args)
  257. result2 = float_frame.transform(op, axis, *args)
  258. tm.assert_frame_equal(result2, expected2)
  259. @pytest.mark.parametrize("method", ["abs", "shift", "pct_change", "cumsum", "rank"])
  260. def test_transform_method_name(method):
  261. # GH 19760
  262. df = DataFrame({"A": [-1, 2]})
  263. result = df.transform(method)
  264. expected = operator.methodcaller(method)(df)
  265. tm.assert_frame_equal(result, expected)