test_common.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import collections
  2. from functools import partial
  3. import string
  4. import subprocess
  5. import sys
  6. import textwrap
  7. import numpy as np
  8. import pytest
  9. import pandas as pd
  10. from pandas import Series
  11. import pandas._testing as tm
  12. from pandas.core import ops
  13. import pandas.core.common as com
  14. from pandas.util.version import Version
  15. def test_get_callable_name():
  16. getname = com.get_callable_name
  17. def fn(x):
  18. return x
  19. lambda_ = lambda x: x
  20. part1 = partial(fn)
  21. part2 = partial(part1)
  22. class somecall:
  23. def __call__(self):
  24. # This shouldn't actually get called below; somecall.__init__
  25. # should.
  26. raise NotImplementedError
  27. assert getname(fn) == "fn"
  28. assert getname(lambda_)
  29. assert getname(part1) == "fn"
  30. assert getname(part2) == "fn"
  31. assert getname(somecall()) == "somecall"
  32. assert getname(1) is None
  33. def test_any_none():
  34. assert com.any_none(1, 2, 3, None)
  35. assert not com.any_none(1, 2, 3, 4)
  36. def test_all_not_none():
  37. assert com.all_not_none(1, 2, 3, 4)
  38. assert not com.all_not_none(1, 2, 3, None)
  39. assert not com.all_not_none(None, None, None, None)
  40. def test_random_state():
  41. # Check with seed
  42. state = com.random_state(5)
  43. assert state.uniform() == np.random.RandomState(5).uniform()
  44. # Check with random state object
  45. state2 = np.random.RandomState(10)
  46. assert com.random_state(state2).uniform() == np.random.RandomState(10).uniform()
  47. # check with no arg random state
  48. assert com.random_state() is np.random
  49. # check array-like
  50. # GH32503
  51. state_arr_like = np.random.default_rng(None).integers(
  52. 0, 2**31, size=624, dtype="uint32"
  53. )
  54. assert (
  55. com.random_state(state_arr_like).uniform()
  56. == np.random.RandomState(state_arr_like).uniform()
  57. )
  58. # Check BitGenerators
  59. # GH32503
  60. assert (
  61. com.random_state(np.random.MT19937(3)).uniform()
  62. == np.random.RandomState(np.random.MT19937(3)).uniform()
  63. )
  64. assert (
  65. com.random_state(np.random.PCG64(11)).uniform()
  66. == np.random.RandomState(np.random.PCG64(11)).uniform()
  67. )
  68. # Error for floats or strings
  69. msg = (
  70. "random_state must be an integer, array-like, a BitGenerator, Generator, "
  71. "a numpy RandomState, or None"
  72. )
  73. with pytest.raises(ValueError, match=msg):
  74. com.random_state("test")
  75. with pytest.raises(ValueError, match=msg):
  76. com.random_state(5.5)
  77. @pytest.mark.parametrize(
  78. "left, right, expected",
  79. [
  80. (Series([1], name="x"), Series([2], name="x"), "x"),
  81. (Series([1], name="x"), Series([2], name="y"), None),
  82. (Series([1]), Series([2], name="x"), None),
  83. (Series([1], name="x"), Series([2]), None),
  84. (Series([1], name="x"), [2], "x"),
  85. ([1], Series([2], name="y"), "y"),
  86. # matching NAs
  87. (Series([1], name=np.nan), pd.Index([], name=np.nan), np.nan),
  88. (Series([1], name=np.nan), pd.Index([], name=pd.NaT), None),
  89. (Series([1], name=pd.NA), pd.Index([], name=pd.NA), pd.NA),
  90. # tuple name GH#39757
  91. (
  92. Series([1], name=np.int64(1)),
  93. pd.Index([], name=(np.int64(1), np.int64(2))),
  94. None,
  95. ),
  96. (
  97. Series([1], name=(np.int64(1), np.int64(2))),
  98. pd.Index([], name=(np.int64(1), np.int64(2))),
  99. (np.int64(1), np.int64(2)),
  100. ),
  101. pytest.param(
  102. Series([1], name=(np.float64("nan"), np.int64(2))),
  103. pd.Index([], name=(np.float64("nan"), np.int64(2))),
  104. (np.float64("nan"), np.int64(2)),
  105. marks=pytest.mark.xfail(
  106. reason="Not checking for matching NAs inside tuples."
  107. ),
  108. ),
  109. ],
  110. )
  111. def test_maybe_match_name(left, right, expected):
  112. res = ops.common._maybe_match_name(left, right)
  113. assert res is expected or res == expected
  114. def test_standardize_mapping():
  115. # No uninitialized defaultdicts
  116. msg = r"to_dict\(\) only accepts initialized defaultdicts"
  117. with pytest.raises(TypeError, match=msg):
  118. com.standardize_mapping(collections.defaultdict)
  119. # No non-mapping subtypes, instance
  120. msg = "unsupported type: <class 'list'>"
  121. with pytest.raises(TypeError, match=msg):
  122. com.standardize_mapping([])
  123. # No non-mapping subtypes, class
  124. with pytest.raises(TypeError, match=msg):
  125. com.standardize_mapping(list)
  126. fill = {"bad": "data"}
  127. assert com.standardize_mapping(fill) == dict
  128. # Convert instance to type
  129. assert com.standardize_mapping({}) == dict
  130. dd = collections.defaultdict(list)
  131. assert isinstance(com.standardize_mapping(dd), partial)
  132. def test_git_version():
  133. # GH 21295
  134. git_version = pd.__git_version__
  135. assert len(git_version) == 40
  136. assert all(c in string.hexdigits for c in git_version)
  137. def test_version_tag():
  138. version = Version(pd.__version__)
  139. try:
  140. version > Version("0.0.1")
  141. except TypeError:
  142. raise ValueError(
  143. "No git tags exist, please sync tags between upstream and your repo"
  144. )
  145. @pytest.mark.parametrize(
  146. "obj", [(obj,) for obj in pd.__dict__.values() if callable(obj)]
  147. )
  148. def test_serializable(obj):
  149. # GH 35611
  150. unpickled = tm.round_trip_pickle(obj)
  151. assert type(obj) == type(unpickled)
  152. class TestIsBoolIndexer:
  153. def test_non_bool_array_with_na(self):
  154. # in particular, this should not raise
  155. arr = np.array(["A", "B", np.nan], dtype=object)
  156. assert not com.is_bool_indexer(arr)
  157. def test_list_subclass(self):
  158. # GH#42433
  159. class MyList(list):
  160. pass
  161. val = MyList(["a"])
  162. assert not com.is_bool_indexer(val)
  163. val = MyList([True])
  164. assert com.is_bool_indexer(val)
  165. def test_frozenlist(self):
  166. # GH#42461
  167. data = {"col1": [1, 2], "col2": [3, 4]}
  168. df = pd.DataFrame(data=data)
  169. frozen = df.index.names[1:]
  170. assert not com.is_bool_indexer(frozen)
  171. result = df[frozen]
  172. expected = df[[]]
  173. tm.assert_frame_equal(result, expected)
  174. @pytest.mark.parametrize("with_exception", [True, False])
  175. def test_temp_setattr(with_exception):
  176. # GH#45954
  177. ser = Series(dtype=object)
  178. ser.name = "first"
  179. # Raise a ValueError in either case to satisfy pytest.raises
  180. match = "Inside exception raised" if with_exception else "Outside exception raised"
  181. with pytest.raises(ValueError, match=match):
  182. with com.temp_setattr(ser, "name", "second"):
  183. assert ser.name == "second"
  184. if with_exception:
  185. raise ValueError("Inside exception raised")
  186. raise ValueError("Outside exception raised")
  187. assert ser.name == "first"
  188. @pytest.mark.single_cpu
  189. def test_str_size():
  190. # GH#21758
  191. a = "a"
  192. expected = sys.getsizeof(a)
  193. pyexe = sys.executable.replace("\\", "/")
  194. call = [
  195. pyexe,
  196. "-c",
  197. "a='a';import sys;sys.getsizeof(a);import pandas;print(sys.getsizeof(a));",
  198. ]
  199. result = subprocess.check_output(call).decode()[-4:-1].strip("\n")
  200. assert int(result) == int(expected)
  201. @pytest.mark.single_cpu
  202. def test_bz2_missing_import():
  203. # Check whether bz2 missing import is handled correctly (issue #53857)
  204. code = """
  205. import sys
  206. sys.modules['bz2'] = None
  207. import pytest
  208. import pandas as pd
  209. from pandas.compat import get_bz2_file
  210. msg = 'bz2 module not available.'
  211. with pytest.raises(RuntimeError, match=msg):
  212. get_bz2_file()
  213. """
  214. code = textwrap.dedent(code)
  215. call = [sys.executable, "-c", code]
  216. subprocess.check_output(call)