Skip to content

Commit

Permalink
TST: Refactor test_maybe_match_name and test_hash_pandas_object (#21600)
Browse files Browse the repository at this point in the history
  • Loading branch information
minggli authored and jreback committed Jun 25, 2018
1 parent 51d548d commit 1033e8b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 59 deletions.
36 changes: 10 additions & 26 deletions pandas/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def test_mut_exclusive():


def test_get_callable_name():
from functools import partial
getname = com._get_callable_name

def fn(x):
Expand Down Expand Up @@ -154,8 +153,7 @@ def test_random_state():

# Check with random state object
state2 = npr.RandomState(10)
assert (com._random_state(state2).uniform() ==
npr.RandomState(10).uniform())
assert com._random_state(state2).uniform() == npr.RandomState(10).uniform()

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


def test_maybe_match_name():

matched = ops._maybe_match_name(
Series([1], name='x'), Series(
[2], name='x'))
assert (matched == 'x')

matched = ops._maybe_match_name(
Series([1], name='x'), Series(
[2], name='y'))
assert (matched is None)

matched = ops._maybe_match_name(Series([1]), Series([2], name='x'))
assert (matched is None)

matched = ops._maybe_match_name(Series([1], name='x'), Series([2]))
assert (matched is None)

matched = ops._maybe_match_name(Series([1], name='x'), [2])
assert (matched == 'x')

matched = ops._maybe_match_name([1], Series([2], name='y'))
assert (matched == 'y')
@pytest.mark.parametrize('left, right, expected', [
(Series([1], name='x'), Series([2], name='x'), 'x'),
(Series([1], name='x'), Series([2], name='y'), None),
(Series([1]), Series([2], name='x'), None),
(Series([1], name='x'), Series([2]), None),
(Series([1], name='x'), [2], 'x'),
([1], Series([2], name='y'), 'y')])
def test_maybe_match_name(left, right, expected):
assert ops._maybe_match_name(left, right) == expected


def test_dict_compat():
Expand Down
62 changes: 29 additions & 33 deletions pandas/tests/util/test_hashing.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,39 +142,35 @@ def test_multiindex_objects(self):
tm.assert_numpy_array_equal(np.sort(result),
np.sort(expected))

def test_hash_pandas_object(self):

for obj in [Series([1, 2, 3]),
Series([1.0, 1.5, 3.2]),
Series([1.0, 1.5, np.nan]),
Series([1.0, 1.5, 3.2], index=[1.5, 1.1, 3.3]),
Series(['a', 'b', 'c']),
Series(['a', np.nan, 'c']),
Series(['a', None, 'c']),
Series([True, False, True]),
Series(),
Index([1, 2, 3]),
Index([True, False, True]),
DataFrame({'x': ['a', 'b', 'c'], 'y': [1, 2, 3]}),
DataFrame(),
tm.makeMissingDataframe(),
tm.makeMixedDataFrame(),
tm.makeTimeDataFrame(),
tm.makeTimeSeries(),
tm.makeTimedeltaIndex(),
tm.makePeriodIndex(),
Series(tm.makePeriodIndex()),
Series(pd.date_range('20130101',
periods=3, tz='US/Eastern')),
MultiIndex.from_product(
[range(5),
['foo', 'bar', 'baz'],
pd.date_range('20130101', periods=2)]),
MultiIndex.from_product(
[pd.CategoricalIndex(list('aabc')),
range(3)])]:
self.check_equal(obj)
self.check_not_equal_with_index(obj)
@pytest.mark.parametrize('obj', [
Series([1, 2, 3]),
Series([1.0, 1.5, 3.2]),
Series([1.0, 1.5, np.nan]),
Series([1.0, 1.5, 3.2], index=[1.5, 1.1, 3.3]),
Series(['a', 'b', 'c']),
Series(['a', np.nan, 'c']),
Series(['a', None, 'c']),
Series([True, False, True]),
Series(),
Index([1, 2, 3]),
Index([True, False, True]),
DataFrame({'x': ['a', 'b', 'c'], 'y': [1, 2, 3]}),
DataFrame(),
tm.makeMissingDataframe(),
tm.makeMixedDataFrame(),
tm.makeTimeDataFrame(),
tm.makeTimeSeries(),
tm.makeTimedeltaIndex(),
tm.makePeriodIndex(),
Series(tm.makePeriodIndex()),
Series(pd.date_range('20130101', periods=3, tz='US/Eastern')),
MultiIndex.from_product([range(5), ['foo', 'bar', 'baz'],
pd.date_range('20130101', periods=2)]),
MultiIndex.from_product([pd.CategoricalIndex(list('aabc')), range(3)])
])
def test_hash_pandas_object(self, obj):
self.check_equal(obj)
self.check_not_equal_with_index(obj)

def test_hash_pandas_object2(self):
for name, s in self.df.iteritems():
Expand Down

0 comments on commit 1033e8b

Please sign in to comment.