|
50 | 50 | from pandas.tseries.frequencies import to_offset
|
51 | 51 | from pandas import compat
|
52 | 52 | from pandas.compat.numpy import function as nv
|
53 |
| -from pandas.compat import (map, zip, lzip, lrange, string_types, |
54 |
| - isidentifier, set_function_name, cPickle as pkl) |
| 53 | +from pandas.compat import (map, zip, lzip, lrange, string_types, binary_type, |
| 54 | + bytes_to_str, isidentifier, set_function_name, |
| 55 | + cPickle as pkl) |
55 | 56 | from pandas.core.ops import _align_method_FRAME
|
56 | 57 | import pandas.core.nanops as nanops
|
57 | 58 | from pandas.util._decorators import (Appender, Substitution,
|
@@ -3218,14 +3219,22 @@ def filter(self, items=None, like=None, regex=None, axis=None):
|
3218 | 3219 | **{name: [r for r in items if r in labels]})
|
3219 | 3220 | elif like:
|
3220 | 3221 | def f(x):
|
3221 |
| - if not isinstance(x, string_types): |
| 3222 | + if isinstance(x, binary_type): |
| 3223 | + x = bytes_to_str(x) |
| 3224 | + elif not isinstance(x, string_types): |
3222 | 3225 | x = str(x)
|
3223 | 3226 | return like in x
|
3224 | 3227 | values = labels.map(f)
|
3225 | 3228 | return self.loc(axis=axis)[values]
|
3226 | 3229 | elif regex:
|
| 3230 | + def f(x): |
| 3231 | + if isinstance(x, binary_type): |
| 3232 | + x = bytes_to_str(x) |
| 3233 | + elif not isinstance(x, string_types): |
| 3234 | + x = str(x) |
| 3235 | + return matcher.search(x) is not None |
3227 | 3236 | matcher = re.compile(regex)
|
3228 |
| - values = labels.map(lambda x: matcher.search(str(x)) is not None) |
| 3237 | + values = labels.map(f) |
3229 | 3238 | return self.loc(axis=axis)[values]
|
3230 | 3239 | else:
|
3231 | 3240 | raise TypeError('Must pass either `items`, `like`, or `regex`')
|
|
0 commit comments