|
11 | 11 | from pandas.lib import Timestamp
|
12 | 12 |
|
13 | 13 | from pandas.util.decorators import cache_readonly
|
14 |
| -from pandas.core.common import isnull |
| 14 | +from pandas.core.common import isnull, bind_method |
15 | 15 | import pandas.core.common as com
|
16 | 16 | from pandas.util import py3compat
|
17 | 17 | from pandas.core.config import get_option
|
@@ -2688,6 +2688,53 @@ def _wrap_joined_index(self, joined, other):
|
2688 | 2688 | return MultiIndex.from_tuples(joined, names=names)
|
2689 | 2689 |
|
2690 | 2690 |
|
| 2691 | +def hashable_class_factory(klass, hash_func=None): |
| 2692 | + """Creates Hashable Class for given Index type |
| 2693 | + and adds `ashashable` method to the Index""" |
| 2694 | + |
| 2695 | + class HashableIndexMixin(object): |
| 2696 | + """ |
| 2697 | + Implements hashing methods...note that this is |
| 2698 | + very crude, and *only* works if it's mixed into a parent |
| 2699 | + class that is a subclass of (or just is) an index |
| 2700 | + """ |
| 2701 | + |
| 2702 | + def __init__(self, index): |
| 2703 | + self._index = index |
| 2704 | + |
| 2705 | + def __eq__(self, other): |
| 2706 | + if issubclass(other, klass): |
| 2707 | + return (self.values == other.values).all() |
| 2708 | + else: |
| 2709 | + return False |
| 2710 | + |
| 2711 | + if hash_func: |
| 2712 | + __hash__ = hash_func |
| 2713 | + else: |
| 2714 | + def __hash__(self): |
| 2715 | + return hash(str(self)) |
| 2716 | + |
| 2717 | + # should this be a property?? |
| 2718 | + def asindex(self): |
| 2719 | + return self._index |
| 2720 | + |
| 2721 | + HashableClass = type("Hashable{klass}".format(klass=klass.__name__), (HashableIndexMixin, klass)) |
| 2722 | + |
| 2723 | + def ashashable(self): |
| 2724 | + """convert {klass} to a hashable type that |
| 2725 | + can be used for key/value lookup |
| 2726 | + """ |
| 2727 | + return HashableClass(self) |
| 2728 | + |
| 2729 | + ashashable.__doc__ = ashashable.__doc__.format(klass=klass.__name__) |
| 2730 | + bind_method(klass, "ashashable", ashashable) |
| 2731 | + |
| 2732 | + return HashableClass |
| 2733 | + |
| 2734 | +HashableIndex = hashable_class_factory(Index) |
| 2735 | +HashableInt64Index = hashable_class_factory(Int64Index) |
| 2736 | +HashableMultiIndex = hashable_class_factory(MultiIndex) |
| 2737 | + |
2691 | 2738 | # For utility purposes
|
2692 | 2739 |
|
2693 | 2740 | def _sparsify(label_list, start=0,sentinal=''):
|
|
0 commit comments