Skip to content

Commit 1033e8b

Browse files
mingglijreback
authored andcommitted
TST: Refactor test_maybe_match_name and test_hash_pandas_object (#21600)
1 parent 51d548d commit 1033e8b

File tree

2 files changed

+39
-59
lines changed

2 files changed

+39
-59
lines changed

pandas/tests/test_common.py

+10-26
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ def test_mut_exclusive():
2525

2626

2727
def test_get_callable_name():
28-
from functools import partial
2928
getname = com._get_callable_name
3029

3130
def fn(x):
@@ -154,8 +153,7 @@ def test_random_state():
154153

155154
# Check with random state object
156155
state2 = npr.RandomState(10)
157-
assert (com._random_state(state2).uniform() ==
158-
npr.RandomState(10).uniform())
156+
assert com._random_state(state2).uniform() == npr.RandomState(10).uniform()
159157

160158
# check with no arg random state
161159
assert com._random_state() is np.random
@@ -168,29 +166,15 @@ def test_random_state():
168166
com._random_state(5.5)
169167

170168

171-
def test_maybe_match_name():
172-
173-
matched = ops._maybe_match_name(
174-
Series([1], name='x'), Series(
175-
[2], name='x'))
176-
assert (matched == 'x')
177-
178-
matched = ops._maybe_match_name(
179-
Series([1], name='x'), Series(
180-
[2], name='y'))
181-
assert (matched is None)
182-
183-
matched = ops._maybe_match_name(Series([1]), Series([2], name='x'))
184-
assert (matched is None)
185-
186-
matched = ops._maybe_match_name(Series([1], name='x'), Series([2]))
187-
assert (matched is None)
188-
189-
matched = ops._maybe_match_name(Series([1], name='x'), [2])
190-
assert (matched == 'x')
191-
192-
matched = ops._maybe_match_name([1], Series([2], name='y'))
193-
assert (matched == 'y')
169+
@pytest.mark.parametrize('left, right, expected', [
170+
(Series([1], name='x'), Series([2], name='x'), 'x'),
171+
(Series([1], name='x'), Series([2], name='y'), None),
172+
(Series([1]), Series([2], name='x'), None),
173+
(Series([1], name='x'), Series([2]), None),
174+
(Series([1], name='x'), [2], 'x'),
175+
([1], Series([2], name='y'), 'y')])
176+
def test_maybe_match_name(left, right, expected):
177+
assert ops._maybe_match_name(left, right) == expected
194178

195179

196180
def test_dict_compat():

pandas/tests/util/test_hashing.py

+29-33
Original file line numberDiff line numberDiff line change
@@ -142,39 +142,35 @@ def test_multiindex_objects(self):
142142
tm.assert_numpy_array_equal(np.sort(result),
143143
np.sort(expected))
144144

145-
def test_hash_pandas_object(self):
146-
147-
for obj in [Series([1, 2, 3]),
148-
Series([1.0, 1.5, 3.2]),
149-
Series([1.0, 1.5, np.nan]),
150-
Series([1.0, 1.5, 3.2], index=[1.5, 1.1, 3.3]),
151-
Series(['a', 'b', 'c']),
152-
Series(['a', np.nan, 'c']),
153-
Series(['a', None, 'c']),
154-
Series([True, False, True]),
155-
Series(),
156-
Index([1, 2, 3]),
157-
Index([True, False, True]),
158-
DataFrame({'x': ['a', 'b', 'c'], 'y': [1, 2, 3]}),
159-
DataFrame(),
160-
tm.makeMissingDataframe(),
161-
tm.makeMixedDataFrame(),
162-
tm.makeTimeDataFrame(),
163-
tm.makeTimeSeries(),
164-
tm.makeTimedeltaIndex(),
165-
tm.makePeriodIndex(),
166-
Series(tm.makePeriodIndex()),
167-
Series(pd.date_range('20130101',
168-
periods=3, tz='US/Eastern')),
169-
MultiIndex.from_product(
170-
[range(5),
171-
['foo', 'bar', 'baz'],
172-
pd.date_range('20130101', periods=2)]),
173-
MultiIndex.from_product(
174-
[pd.CategoricalIndex(list('aabc')),
175-
range(3)])]:
176-
self.check_equal(obj)
177-
self.check_not_equal_with_index(obj)
145+
@pytest.mark.parametrize('obj', [
146+
Series([1, 2, 3]),
147+
Series([1.0, 1.5, 3.2]),
148+
Series([1.0, 1.5, np.nan]),
149+
Series([1.0, 1.5, 3.2], index=[1.5, 1.1, 3.3]),
150+
Series(['a', 'b', 'c']),
151+
Series(['a', np.nan, 'c']),
152+
Series(['a', None, 'c']),
153+
Series([True, False, True]),
154+
Series(),
155+
Index([1, 2, 3]),
156+
Index([True, False, True]),
157+
DataFrame({'x': ['a', 'b', 'c'], 'y': [1, 2, 3]}),
158+
DataFrame(),
159+
tm.makeMissingDataframe(),
160+
tm.makeMixedDataFrame(),
161+
tm.makeTimeDataFrame(),
162+
tm.makeTimeSeries(),
163+
tm.makeTimedeltaIndex(),
164+
tm.makePeriodIndex(),
165+
Series(tm.makePeriodIndex()),
166+
Series(pd.date_range('20130101', periods=3, tz='US/Eastern')),
167+
MultiIndex.from_product([range(5), ['foo', 'bar', 'baz'],
168+
pd.date_range('20130101', periods=2)]),
169+
MultiIndex.from_product([pd.CategoricalIndex(list('aabc')), range(3)])
170+
])
171+
def test_hash_pandas_object(self, obj):
172+
self.check_equal(obj)
173+
self.check_not_equal_with_index(obj)
178174

179175
def test_hash_pandas_object2(self):
180176
for name, s in self.df.iteritems():

0 commit comments

Comments
 (0)