Skip to content

Commit af0ecbe

Browse files
mroeschkejreback
authored andcommitted
CLN: Misc PY2/3 compat functions (#26008)
1 parent 0610a60 commit af0ecbe

File tree

10 files changed

+15
-71
lines changed

10 files changed

+15
-71
lines changed

pandas/compat/__init__.py

+3-52
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
* lists: lrange(), lmap(), lzip(), lfilter()
99
* iterable method compatibility: iteritems, iterkeys, itervalues
1010
* Uses the original method if available, otherwise uses items, keys, values.
11-
* bind_method: binds functions to classes
1211
* add_metaclass(metaclass) - class decorator that recreates class with with the
1312
given metaclass instead (and avoids intermediary class creation)
1413
@@ -22,7 +21,6 @@
2221
from distutils.version import LooseVersion
2322
import sys
2423
import platform
25-
import types
2624
import struct
2725

2826
PY2 = sys.version_info[0] == 2
@@ -32,8 +30,6 @@
3230
PY37 = sys.version_info >= (3, 7)
3331
PYPY = platform.python_implementation() == 'PyPy'
3432

35-
from pandas.compat.chainmap import DeepChainMap
36-
3733

3834
# list-producing versions of the major Python iterating functions
3935
def lrange(*args, **kwargs):
@@ -52,29 +48,6 @@ def lfilter(*args, **kwargs):
5248
return list(filter(*args, **kwargs))
5349

5450

55-
if PY3:
56-
def isidentifier(s):
57-
return s.isidentifier()
58-
59-
def str_to_bytes(s, encoding=None):
60-
return s.encode(encoding or 'ascii')
61-
62-
def bytes_to_str(b, encoding=None):
63-
return b.decode(encoding or 'utf-8')
64-
65-
else:
66-
# Python 2
67-
_name_re = re.compile(r"[a-zA-Z_][a-zA-Z0-9_]*$")
68-
69-
def isidentifier(s, dotted=False):
70-
return bool(_name_re.match(s))
71-
72-
def str_to_bytes(s, encoding='ascii'):
73-
return s
74-
75-
def bytes_to_str(b, encoding='ascii'):
76-
return b
77-
7851
if PY2:
7952
def iteritems(obj, **kw):
8053
return obj.iteritems(**kw)
@@ -95,30 +68,6 @@ def iterkeys(obj, **kw):
9568
def itervalues(obj, **kw):
9669
return iter(obj.values(**kw))
9770

98-
99-
def bind_method(cls, name, func):
100-
"""Bind a method to class, python 2 and python 3 compatible.
101-
102-
Parameters
103-
----------
104-
105-
cls : type
106-
class to receive bound method
107-
name : basestring
108-
name of method on class instance
109-
func : function
110-
function to be bound as method
111-
112-
113-
Returns
114-
-------
115-
None
116-
"""
117-
# only python 2 has bound/unbound method issue
118-
if not PY3:
119-
setattr(cls, name, types.MethodType(func, None, cls))
120-
else:
121-
setattr(cls, name, func)
12271
# ----------------------------------------------------------------------------
12372
# functions largely based / taken from the six module
12473

@@ -133,7 +82,7 @@ def to_str(s):
13382
Convert bytes and non-string into Python 3 str
13483
"""
13584
if isinstance(s, bytes):
136-
s = bytes_to_str(s)
85+
s = s.decode('utf-8')
13786
elif not isinstance(s, str):
13887
s = str(s)
13988
return s
@@ -172,6 +121,7 @@ def wrapper(cls):
172121
return metaclass(cls.__name__, cls.__bases__, orig_vars)
173122
return wrapper
174123

124+
175125
if PY3:
176126
def raise_with_traceback(exc, traceback=Ellipsis):
177127
if traceback == Ellipsis:
@@ -207,6 +157,7 @@ def raise_with_traceback(exc, traceback=Ellipsis):
207157
else:
208158
re_type = type(re.compile(''))
209159

160+
210161
# https://github.com/pandas-dev/pandas/pull/9123
211162
def is_platform_little_endian():
212163
""" am I little endian """

pandas/core/computation/pytables.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import numpy as np
77

88
from pandas._libs.tslibs import Timedelta, Timestamp
9-
from pandas.compat import DeepChainMap
9+
from pandas.compat.chainmap import DeepChainMap
1010

1111
from pandas.core.dtypes.common import is_list_like
1212

pandas/core/computation/scope.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import numpy as np
1414

1515
from pandas._libs.tslibs import Timestamp
16-
from pandas.compat import DeepChainMap
16+
from pandas.compat.chainmap import DeepChainMap
1717

1818
from pandas.core.base import StringMixin
1919
import pandas.core.computation as compu

pandas/core/generic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from pandas._libs import Timestamp, iNaT, properties
1818
import pandas.compat as compat
19-
from pandas.compat import isidentifier, lrange, lzip, set_function_name, to_str
19+
from pandas.compat import lrange, lzip, set_function_name, to_str
2020
from pandas.compat.numpy import function as nv
2121
from pandas.errors import AbstractMethodError
2222
from pandas.util._decorators import (
@@ -5150,7 +5150,7 @@ def _dir_additions(self):
51505150
If info_axis is a MultiIndex, it's first level values are used.
51515151
"""
51525152
additions = {c for c in self._info_axis.unique(level=0)[:100]
5153-
if isinstance(c, str) and isidentifier(c)}
5153+
if isinstance(c, str) and c.isidentifier()}
51545154
return super(NDFrame, self)._dir_additions().union(additions)
51555155

51565156
# ----------------------------------------------------------------------

pandas/core/ops.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
from pandas._libs import algos as libalgos, lib, ops as libops
1414
import pandas.compat as compat
15-
from pandas.compat import bind_method
1615
from pandas.errors import NullFrequencyError
1716
from pandas.util._decorators import Appender
1817

@@ -1545,7 +1544,7 @@ def add_methods(cls, new_methods):
15451544
force = not (issubclass(cls, ABCSparseArray) and
15461545
name.startswith('__i'))
15471546
if force or name not in cls.__dict__:
1548-
bind_method(cls, name, method)
1547+
setattr(cls, name, method)
15491548

15501549

15511550
# ----------------------------------------------------------------------

pandas/io/clipboards.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
from io import StringIO
33
import warnings
44

5-
import pandas.compat as compat
6-
75
from pandas.core.dtypes.generic import ABCDataFrame
86

97
from pandas import get_option, option_context
@@ -38,10 +36,8 @@ def read_clipboard(sep=r'\s+', **kwargs): # pragma: no cover
3836

3937
# Try to decode (if needed, as "text" might already be a string here).
4038
try:
41-
text = compat.bytes_to_str(
42-
text, encoding=(kwargs.get('encoding') or
43-
get_option('display.encoding'))
44-
)
39+
text = text.decode(kwargs.get('encoding')
40+
or get_option('display.encoding'))
4541
except AttributeError:
4642
pass
4743

pandas/io/common.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from urllib.request import pathname2url, urlopen
1717
import zipfile
1818

19-
import pandas.compat as compat
2019
from pandas.errors import ( # noqa
2120
AbstractMethodError, DtypeWarning, EmptyDataError, ParserError,
2221
ParserWarning)
@@ -460,7 +459,7 @@ def __next__(self):
460459

461460
# readline returns bytes, not str, but Python's CSV reader
462461
# expects str, so convert the output to str before continuing
463-
newline = compat.bytes_to_str(newline)
462+
newline = newline.decode('utf-8')
464463

465464
# mmap doesn't raise if reading past the allocated
466465
# data but instead returns an empty string, so raise

pandas/tests/io/formats/test_printing.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import pandas._config.config as cf
66

77
import pandas as pd
8-
from pandas import compat
98

109
import pandas.io.formats.format as fmt
1110
import pandas.io.formats.printing as printing
@@ -27,7 +26,7 @@ def test_repr_binary_type():
2726
raw = bytes(letters, encoding=cf.get_option('display.encoding'))
2827
except TypeError:
2928
raw = bytes(letters)
30-
b = str(compat.bytes_to_str(raw))
29+
b = str(raw.decode('utf-8'))
3130
res = printing.pprint_thing(b, quote_strings=True)
3231
assert res == repr(b)
3332
res = printing.pprint_thing(b, quote_strings=False)

pandas/tests/io/json/test_ujson.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ def test_decode_big_escape(self):
654654
# Make sure no Exception is raised.
655655
for _ in range(10):
656656
base = '\u00e5'.encode("utf-8")
657-
quote = compat.str_to_bytes("\"")
657+
quote = b'"'
658658

659659
escape_input = quote + (base * 1024 * 1024 * 2) + quote
660660
ujson.decode(escape_input)

pandas/tests/series/test_api.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import pytest
99

1010
import pandas.compat as compat
11-
from pandas.compat import isidentifier, lzip
11+
from pandas.compat import lzip
1212

1313
import pandas as pd
1414
from pandas import (
@@ -282,7 +282,7 @@ def test_index_tab_completion(self, index):
282282
for i, x in enumerate(s.index.unique(level=0)):
283283
if i < 100:
284284
assert (not isinstance(x, str) or
285-
not isidentifier(x) or x in dir_s)
285+
not x.isidentifier() or x in dir_s)
286286
else:
287287
assert x not in dir_s
288288

0 commit comments

Comments
 (0)