conftest.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import pytest
  2. from pandas import Series
  3. from pandas.core.strings.accessor import StringMethods
  4. _any_string_method = [
  5. ("cat", (), {"sep": ","}),
  6. ("cat", (Series(list("zyx")),), {"sep": ",", "join": "left"}),
  7. ("center", (10,), {}),
  8. ("contains", ("a",), {}),
  9. ("count", ("a",), {}),
  10. ("decode", ("UTF-8",), {}),
  11. ("encode", ("UTF-8",), {}),
  12. ("endswith", ("a",), {}),
  13. ("endswith", ((),), {}),
  14. ("endswith", (("a",),), {}),
  15. ("endswith", (("a", "b"),), {}),
  16. ("endswith", (("a", "MISSING"),), {}),
  17. ("endswith", ("a",), {"na": True}),
  18. ("endswith", ("a",), {"na": False}),
  19. ("extract", ("([a-z]*)",), {"expand": False}),
  20. ("extract", ("([a-z]*)",), {"expand": True}),
  21. ("extractall", ("([a-z]*)",), {}),
  22. ("find", ("a",), {}),
  23. ("findall", ("a",), {}),
  24. ("get", (0,), {}),
  25. # because "index" (and "rindex") fail intentionally
  26. # if the string is not found, search only for empty string
  27. ("index", ("",), {}),
  28. ("join", (",",), {}),
  29. ("ljust", (10,), {}),
  30. ("match", ("a",), {}),
  31. ("fullmatch", ("a",), {}),
  32. ("normalize", ("NFC",), {}),
  33. ("pad", (10,), {}),
  34. ("partition", (" ",), {"expand": False}),
  35. ("partition", (" ",), {"expand": True}),
  36. ("repeat", (3,), {}),
  37. ("replace", ("a", "z"), {}),
  38. ("rfind", ("a",), {}),
  39. ("rindex", ("",), {}),
  40. ("rjust", (10,), {}),
  41. ("rpartition", (" ",), {"expand": False}),
  42. ("rpartition", (" ",), {"expand": True}),
  43. ("slice", (0, 1), {}),
  44. ("slice_replace", (0, 1, "z"), {}),
  45. ("split", (" ",), {"expand": False}),
  46. ("split", (" ",), {"expand": True}),
  47. ("startswith", ("a",), {}),
  48. ("startswith", (("a",),), {}),
  49. ("startswith", (("a", "b"),), {}),
  50. ("startswith", (("a", "MISSING"),), {}),
  51. ("startswith", ((),), {}),
  52. ("startswith", ("a",), {"na": True}),
  53. ("startswith", ("a",), {"na": False}),
  54. ("removeprefix", ("a",), {}),
  55. ("removesuffix", ("a",), {}),
  56. # translating unicode points of "a" to "d"
  57. ("translate", ({97: 100},), {}),
  58. ("wrap", (2,), {}),
  59. ("zfill", (10,), {}),
  60. ] + list(
  61. zip(
  62. [
  63. # methods without positional arguments: zip with empty tuple and empty dict
  64. "capitalize",
  65. "cat",
  66. "get_dummies",
  67. "isalnum",
  68. "isalpha",
  69. "isdecimal",
  70. "isdigit",
  71. "islower",
  72. "isnumeric",
  73. "isspace",
  74. "istitle",
  75. "isupper",
  76. "len",
  77. "lower",
  78. "lstrip",
  79. "partition",
  80. "rpartition",
  81. "rsplit",
  82. "rstrip",
  83. "slice",
  84. "slice_replace",
  85. "split",
  86. "strip",
  87. "swapcase",
  88. "title",
  89. "upper",
  90. "casefold",
  91. ],
  92. [()] * 100,
  93. [{}] * 100,
  94. )
  95. )
  96. ids, _, _ = zip(*_any_string_method) # use method name as fixture-id
  97. missing_methods = {f for f in dir(StringMethods) if not f.startswith("_")} - set(ids)
  98. # test that the above list captures all methods of StringMethods
  99. assert not missing_methods
  100. @pytest.fixture(params=_any_string_method, ids=ids)
  101. def any_string_method(request):
  102. """
  103. Fixture for all public methods of `StringMethods`
  104. This fixture returns a tuple of the method name and sample arguments
  105. necessary to call the method.
  106. Returns
  107. -------
  108. method_name : str
  109. The name of the method in `StringMethods`
  110. args : tuple
  111. Sample values for the positional arguments
  112. kwargs : dict
  113. Sample values for the keyword arguments
  114. Examples
  115. --------
  116. >>> def test_something(any_string_method):
  117. ... s = Series(['a', 'b', np.nan, 'd'])
  118. ...
  119. ... method_name, args, kwargs = any_string_method
  120. ... method = getattr(s.str, method_name)
  121. ... # will not raise
  122. ... method(*args, **kwargs)
  123. """
  124. return request.param