Skip to content

Commit 3f27c2f

Browse files
committed
ENH: Add cross-compatible bind_method for classes to core/common
1 parent 9389318 commit 3f27c2f

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

pandas/core/common.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import pickle
99

1010
import itertools
11+
import types
12+
import operator
13+
import warnings
1114
from datetime import datetime
1215

1316
from numpy.lib.format import read_array, write_array
@@ -42,6 +45,30 @@ class AmbiguousIndexError(PandasError, KeyError):
4245
pass
4346

4447

48+
def bind_method(cls, name, func):
49+
"""Bind a method to class, python 2 and python 3 compatible.
50+
51+
Parameters
52+
----------
53+
54+
cls : type
55+
class to receive bound method
56+
name : basestring
57+
name of method on class instance
58+
func : function
59+
function to be bound as method
60+
61+
62+
Returns
63+
-------
64+
None
65+
"""
66+
# only python 2 has bound/unbound method issue
67+
if not py3compat.PY3:
68+
setattr(cls, name, types.MethodType(func, None, cls))
69+
else:
70+
setattr(cls, name, func)
71+
4572
_POSSIBLY_CAST_DTYPES = set([ np.dtype(t) for t in ['M8[ns]','m8[ns]','O','int8','uint8','int16','uint16','int32','uint32','int64','uint64'] ])
4673
_NS_DTYPE = np.dtype('M8[ns]')
4774
_TD_DTYPE = np.dtype('m8[ns]')

0 commit comments

Comments
 (0)