test_common.py 6.5 KB

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