Skip to content

Commit ba0e377

Browse files
committed
ENH: Implement to_str method for Python 2/3 compatibility
1 parent 96a5274 commit ba0e377

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

pandas/compat/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,16 @@ def u(s):
257257
def u_safe(s):
258258
return s
259259

260+
def to_str(s):
261+
"""
262+
Convert bytes and non-string into Python 3 str
263+
"""
264+
if isinstance(s, binary_type):
265+
s = bytes_to_str(s)
266+
elif not isinstance(s, string_types):
267+
s = str(s)
268+
return s
269+
260270
def strlen(data, encoding=None):
261271
# encoding is for compat with PY2
262272
return len(data)
@@ -302,6 +312,14 @@ def u_safe(s):
302312
except:
303313
return s
304314

315+
def to_str(s):
316+
"""
317+
Convert unicode and non-string into Python 2 str
318+
"""
319+
if not isinstance(s, string_types):
320+
s = str(s)
321+
return s
322+
305323
def strlen(data, encoding=None):
306324
try:
307325
data = data.decode(encoding)

0 commit comments

Comments
 (0)