tests_pandas.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. from tqdm import tqdm
  2. from .tests_tqdm import StringIO, closing, importorskip, mark, skip
  3. pytestmark = mark.slow
  4. random = importorskip('numpy.random')
  5. rand = random.rand
  6. randint = random.randint
  7. pd = importorskip('pandas')
  8. def test_pandas_setup():
  9. """Test tqdm.pandas()"""
  10. with closing(StringIO()) as our_file:
  11. tqdm.pandas(file=our_file, leave=True, ascii=True, total=123)
  12. series = pd.Series(randint(0, 50, (100,)))
  13. series.progress_apply(lambda x: x + 10)
  14. res = our_file.getvalue()
  15. assert '100/123' in res
  16. def test_pandas_rolling_expanding():
  17. """Test pandas.(Series|DataFrame).(rolling|expanding)"""
  18. with closing(StringIO()) as our_file:
  19. tqdm.pandas(file=our_file, leave=True, ascii=True)
  20. series = pd.Series(randint(0, 50, (123,)))
  21. res1 = series.rolling(10).progress_apply(lambda x: 1, raw=True)
  22. res2 = series.rolling(10).apply(lambda x: 1, raw=True)
  23. assert res1.equals(res2)
  24. res3 = series.expanding(10).progress_apply(lambda x: 2, raw=True)
  25. res4 = series.expanding(10).apply(lambda x: 2, raw=True)
  26. assert res3.equals(res4)
  27. expects = ['114it'] # 123-10+1
  28. for exres in expects:
  29. our_file.seek(0)
  30. if our_file.getvalue().count(exres) < 2:
  31. our_file.seek(0)
  32. raise AssertionError(
  33. f"\nExpected:\n{exres} at least twice.\nIn:\n{our_file.read()}\n")
  34. def test_pandas_series():
  35. """Test pandas.Series.progress_apply and .progress_map"""
  36. with closing(StringIO()) as our_file:
  37. tqdm.pandas(file=our_file, leave=True, ascii=True)
  38. series = pd.Series(randint(0, 50, (123,)))
  39. res1 = series.progress_apply(lambda x: x + 10)
  40. res2 = series.apply(lambda x: x + 10)
  41. assert res1.equals(res2)
  42. res3 = series.progress_map(lambda x: x + 10)
  43. res4 = series.map(lambda x: x + 10)
  44. assert res3.equals(res4)
  45. expects = ['100%', '123/123']
  46. for exres in expects:
  47. our_file.seek(0)
  48. if our_file.getvalue().count(exres) < 2:
  49. our_file.seek(0)
  50. raise AssertionError(
  51. f"\nExpected:\n{exres} at least twice.\nIn:\n{our_file.read()}\n")
  52. def test_pandas_data_frame():
  53. """Test pandas.DataFrame.progress_apply and .progress_applymap"""
  54. with closing(StringIO()) as our_file:
  55. tqdm.pandas(file=our_file, leave=True, ascii=True)
  56. df = pd.DataFrame(randint(0, 50, (100, 200)))
  57. def task_func(x):
  58. return x + 1
  59. # applymap
  60. res1 = df.progress_applymap(task_func)
  61. res2 = df.applymap(task_func)
  62. assert res1.equals(res2)
  63. # apply unhashable
  64. res1 = []
  65. df.progress_apply(res1.extend)
  66. assert len(res1) == df.size
  67. # apply
  68. for axis in [0, 1, 'index', 'columns']:
  69. res3 = df.progress_apply(task_func, axis=axis)
  70. res4 = df.apply(task_func, axis=axis)
  71. assert res3.equals(res4)
  72. our_file.seek(0)
  73. if our_file.read().count('100%') < 3:
  74. our_file.seek(0)
  75. raise AssertionError(
  76. f"\nExpected:\n100% at least three times\nIn:\n{our_file.read()}\n")
  77. # apply_map, apply axis=0, apply axis=1
  78. expects = ['20000/20000', '200/200', '100/100']
  79. for exres in expects:
  80. our_file.seek(0)
  81. if our_file.getvalue().count(exres) < 1:
  82. our_file.seek(0)
  83. raise AssertionError(
  84. f"\nExpected:\n{exres} at least once.\nIn:\n{our_file.read()}\n")
  85. def test_pandas_groupby_apply():
  86. """Test pandas.DataFrame.groupby(...).progress_apply"""
  87. with closing(StringIO()) as our_file:
  88. tqdm.pandas(file=our_file, leave=False, ascii=True)
  89. df = pd.DataFrame(randint(0, 50, (500, 3)))
  90. df.groupby(0).progress_apply(lambda x: None)
  91. dfs = pd.DataFrame(randint(0, 50, (500, 3)), columns=list('abc'))
  92. dfs.groupby(['a']).progress_apply(lambda x: None)
  93. df2 = df = pd.DataFrame({'a': randint(1, 8, 10000), 'b': rand(10000)})
  94. res1 = df2.groupby("a").apply(max)
  95. res2 = df2.groupby("a").progress_apply(max)
  96. assert res1.equals(res2)
  97. our_file.seek(0)
  98. # don't expect final output since no `leave` and
  99. # high dynamic `miniters`
  100. nexres = '100%|##########|'
  101. if nexres in our_file.read():
  102. our_file.seek(0)
  103. raise AssertionError(f"\nDid not expect:\n{nexres}\nIn:{our_file.read()}\n")
  104. with closing(StringIO()) as our_file:
  105. tqdm.pandas(file=our_file, leave=True, ascii=True)
  106. dfs = pd.DataFrame(randint(0, 50, (500, 3)), columns=list('abc'))
  107. dfs.loc[0] = [2, 1, 1]
  108. dfs['d'] = 100
  109. expects = ['500/500', '1/1', '4/4', '2/2']
  110. dfs.groupby(dfs.index).progress_apply(lambda x: None)
  111. dfs.groupby('d').progress_apply(lambda x: None)
  112. dfs.groupby(dfs.columns, axis=1).progress_apply(lambda x: None)
  113. dfs.groupby([2, 2, 1, 1], axis=1).progress_apply(lambda x: None)
  114. our_file.seek(0)
  115. if our_file.read().count('100%') < 4:
  116. our_file.seek(0)
  117. raise AssertionError(
  118. f"\nExpected:\n100% at least four times\nIn:\n{our_file.read()}\n")
  119. for exres in expects:
  120. our_file.seek(0)
  121. if our_file.getvalue().count(exres) < 1:
  122. our_file.seek(0)
  123. raise AssertionError(
  124. f"\nExpected:\n{exres} at least once.\nIn:\n{our_file.read()}\n")
  125. def test_pandas_leave():
  126. """Test pandas with `leave=True`"""
  127. with closing(StringIO()) as our_file:
  128. df = pd.DataFrame(randint(0, 100, (1000, 6)))
  129. tqdm.pandas(file=our_file, leave=True, ascii=True)
  130. df.groupby(0).progress_apply(lambda x: None)
  131. our_file.seek(0)
  132. exres = '100%|##########| 100/100'
  133. if exres not in our_file.read():
  134. our_file.seek(0)
  135. raise AssertionError(f"\nExpected:\n{exres}\nIn:{our_file.read()}\n")
  136. def test_pandas_apply_args_deprecation():
  137. """Test warning info in
  138. `pandas.Dataframe(Series).progress_apply(func, *args)`"""
  139. try:
  140. from tqdm import tqdm_pandas
  141. except ImportError as err:
  142. skip(str(err))
  143. with closing(StringIO()) as our_file:
  144. tqdm_pandas(tqdm(file=our_file, leave=False, ascii=True, ncols=20))
  145. df = pd.DataFrame(randint(0, 50, (500, 3)))
  146. df.progress_apply(lambda x: None, 1) # 1 shall cause a warning
  147. # Check deprecation message
  148. res = our_file.getvalue()
  149. assert all(i in res for i in (
  150. "TqdmDeprecationWarning", "not supported",
  151. "keyword arguments instead"))
  152. def test_pandas_deprecation():
  153. """Test bar object instance as argument deprecation"""
  154. try:
  155. from tqdm import tqdm_pandas
  156. except ImportError as err:
  157. skip(str(err))
  158. with closing(StringIO()) as our_file:
  159. tqdm_pandas(tqdm(file=our_file, leave=False, ascii=True, ncols=20))
  160. df = pd.DataFrame(randint(0, 50, (500, 3)))
  161. df.groupby(0).progress_apply(lambda x: None)
  162. # Check deprecation message
  163. assert "TqdmDeprecationWarning" in our_file.getvalue()
  164. assert "instead of `tqdm_pandas(tqdm(...))`" in our_file.getvalue()
  165. with closing(StringIO()) as our_file:
  166. tqdm_pandas(tqdm, file=our_file, leave=False, ascii=True, ncols=20)
  167. df = pd.DataFrame(randint(0, 50, (500, 3)))
  168. df.groupby(0).progress_apply(lambda x: None)
  169. # Check deprecation message
  170. assert "TqdmDeprecationWarning" in our_file.getvalue()
  171. assert "instead of `tqdm_pandas(tqdm, ...)`" in our_file.getvalue()