Skip to content

Commit e55bf8e

Browse files
committed
BUG: Fix filter method so that accepts byte and unicode
1 parent 96a5274 commit e55bf8e

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

pandas/core/generic.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@
5050
from pandas.tseries.frequencies import to_offset
5151
from pandas import compat
5252
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)
5556
from pandas.core.ops import _align_method_FRAME
5657
import pandas.core.nanops as nanops
5758
from pandas.util._decorators import (Appender, Substitution,
@@ -3218,14 +3219,22 @@ def filter(self, items=None, like=None, regex=None, axis=None):
32183219
**{name: [r for r in items if r in labels]})
32193220
elif like:
32203221
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):
32223225
x = str(x)
32233226
return like in x
32243227
values = labels.map(f)
32253228
return self.loc(axis=axis)[values]
32263229
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
32273236
matcher = re.compile(regex)
3228-
values = labels.map(lambda x: matcher.search(str(x)) is not None)
3237+
values = labels.map(f)
32293238
return self.loc(axis=axis)[values]
32303239
else:
32313240
raise TypeError('Must pass either `items`, `like`, or `regex`')

0 commit comments

Comments
 (0)