test_common.py 7.8 KB

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