|
| 1 | +import numpy as np |
| 2 | +import pandas as pd |
| 3 | + |
| 4 | +from pandas import DataFrame, Series, Index |
| 5 | +from pandas.tools.hashing import hash_array, hash_pandas_object |
| 6 | +import pandas.util.testing as tm |
| 7 | + |
| 8 | + |
| 9 | +class TestHashing(tm.TestCase): |
| 10 | + |
| 11 | + _multiprocess_can_split_ = True |
| 12 | + |
| 13 | + def setUp(self): |
| 14 | + self.df = DataFrame( |
| 15 | + {'i32': np.array([1, 2, 3] * 3, dtype='int32'), |
| 16 | + 'f32': np.array([None, 2.5, 3.5] * 3, dtype='float32'), |
| 17 | + 'cat': Series(['a', 'b', 'c'] * 3).astype('category'), |
| 18 | + 'obj': Series(['d', 'e', 'f'] * 3), |
| 19 | + 'bool': np.array([True, False, True] * 3), |
| 20 | + 'dt': Series(pd.date_range('20130101', periods=9)), |
| 21 | + 'dt_tz': Series(pd.date_range('20130101', periods=9, |
| 22 | + tz='US/Eastern')), |
| 23 | + 'td': Series(pd.timedelta_range('2000', periods=9))}) |
| 24 | + |
| 25 | + def test_hash_array(self): |
| 26 | + for name, s in self.df.iteritems(): |
| 27 | + a = s.values |
| 28 | + tm.assert_numpy_array_equal(hash_array(a), hash_array(a)) |
| 29 | + |
| 30 | + def check_equal(self, obj): |
| 31 | + a = hash_pandas_object(obj) |
| 32 | + b = hash_pandas_object(obj) |
| 33 | + tm.assert_numpy_array_equal(a, b) |
| 34 | + |
| 35 | + a = obj.hash() |
| 36 | + b = obj.hash() |
| 37 | + tm.assert_numpy_array_equal(a, b) |
| 38 | + |
| 39 | + def test_hash_pandas_object(self): |
| 40 | + |
| 41 | + for obj in [Series([1, 2, 3]), |
| 42 | + Series([1.0, 1.5, 3.2]), |
| 43 | + Series([1.0, 1.5, 3.2], index=[1.5, 1.1, 3.3]), |
| 44 | + Series(['a', 'b', 'c']), |
| 45 | + Series([True, False, True]), |
| 46 | + Index([1, 2, 3]), |
| 47 | + Index([True, False, True]), |
| 48 | + DataFrame({'x': ['a', 'b', 'c'], 'y': [1, 2, 3]}), |
| 49 | + tm.makeMissingDataframe(), |
| 50 | + tm.makeMixedDataFrame(), |
| 51 | + tm.makeTimeDataFrame(), |
| 52 | + tm.makeTimeSeries(), |
| 53 | + tm.makeTimedeltaIndex()]: |
| 54 | + self.check_equal(obj) |
| 55 | + |
| 56 | + def test_errors(self): |
| 57 | + |
| 58 | + for obj in [pd.Timestamp('20130101'), tm.makePanel()]: |
| 59 | + def f(): |
| 60 | + hash_pandas_object(f) |
| 61 | + |
| 62 | + self.assertRaises(TypeError, f) |
0 commit comments