Skip to content

CLN: Remove u and u_safe from pandas.compat #25852

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions pandas/compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
Key items to import for 2/3 compatible code:
* iterators: reduce()
* lists: lrange(), lmap(), lzip(), lfilter()
* unicode: u() [no unicode builtin in Python 3]
* longs: long (int in Python 3)
* iterable method compatibility: iteritems, iterkeys, itervalues
* Uses the original method if available, otherwise uses items, keys, values.
Expand Down Expand Up @@ -256,12 +255,6 @@ class to receive bound method
text_type = str
binary_type = bytes

def u(s):
return s

def u_safe(s):
return s

def to_str(s):
"""
Convert bytes and non-string into Python 3 str
Expand Down Expand Up @@ -305,15 +298,6 @@ def set_function_name(f, name, cls):
text_type = unicode
binary_type = str

def u(s):
return unicode(s, "unicode_escape")

def u_safe(s):
try:
return unicode(s, "unicode_escape")
except:
return s

def to_str(s):
"""
Convert unicode and non-string into Python 2 str
Expand Down
2 changes: 1 addition & 1 deletion pandas/compat/pickle_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pickle as pkl
import sys

from pandas.compat import string_types, u # noqa
from pandas.compat import string_types # noqa

import pandas # noqa
from pandas import Index, compat
Expand Down
4 changes: 2 additions & 2 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pytest
from pytz import FixedOffset, utc

from pandas.compat import PY3, u
from pandas.compat import PY3
import pandas.util._test_decorators as td

import pandas as pd
Expand Down Expand Up @@ -561,7 +561,7 @@ def any_numpy_dtype(request):
# categoricals are handled separately
_any_skipna_inferred_dtype = [
('string', ['a', np.nan, 'c']),
('unicode' if not PY3 else 'string', [u('a'), np.nan, u('c')]),
('unicode' if not PY3 else 'string', ['a', np.nan, 'c']),
('bytes' if PY3 else 'string', [b'a', np.nan, b'c']),
('empty', [np.nan, np.nan, np.nan]),
('empty', []),
Expand Down
10 changes: 5 additions & 5 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from pandas._libs import algos as libalgos, lib
import pandas.compat as compat
from pandas.compat import lzip, u
from pandas.compat import lzip
from pandas.compat.numpy import function as nv
from pandas.util._decorators import (
Appender, Substitution, cache_readonly, deprecate_kwarg)
Expand Down Expand Up @@ -1947,10 +1947,10 @@ def _tidy_repr(self, max_vals=10, footer=True):
head = self[:num]._get_repr(length=False, footer=False)
tail = self[-(max_vals - num):]._get_repr(length=False, footer=False)

result = u('{head}, ..., {tail}').format(head=head[:-1], tail=tail[1:])
result = '{head}, ..., {tail}'.format(head=head[:-1], tail=tail[1:])
if footer:
result = u('{result}\n{footer}').format(result=result,
footer=self._repr_footer())
result = '{result}\n{footer}'.format(
result=result, footer=self._repr_footer())

return compat.text_type(result)

Expand Down Expand Up @@ -2008,7 +2008,7 @@ def _repr_categories_info(self):

def _repr_footer(self):

return u('Length: {length}\n{info}').format(
return 'Length: {length}\n{info}'.format(
length=len(self), info=self._repr_categories_info())

def _get_repr(self, length=True, na_rep='NaN', footer=True):
Expand Down
24 changes: 12 additions & 12 deletions pandas/core/computation/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import numpy as np

from pandas._libs.tslibs import Timedelta, Timestamp
from pandas.compat import DeepChainMap, string_types, u
from pandas.compat import DeepChainMap, string_types

from pandas.core.dtypes.common import is_list_like

Expand Down Expand Up @@ -182,43 +182,43 @@ def stringify(value):

kind = _ensure_decoded(self.kind)
meta = _ensure_decoded(self.meta)
if kind == u('datetime64') or kind == u('datetime'):
if kind == 'datetime64' or kind == 'datetime':
if isinstance(v, (int, float)):
v = stringify(v)
v = _ensure_decoded(v)
v = Timestamp(v)
if v.tz is not None:
v = v.tz_convert('UTC')
return TermValue(v, v.value, kind)
elif kind == u('timedelta64') or kind == u('timedelta'):
elif kind == 'timedelta64' or kind == 'timedelta':
v = Timedelta(v, unit='s').value
return TermValue(int(v), v, kind)
elif meta == u('category'):
elif meta == 'category':
metadata = com.values_from_object(self.metadata)
result = metadata.searchsorted(v, side='left')

# result returns 0 if v is first element or if v is not in metadata
# check that metadata contains v
if not result and v not in metadata:
result = -1
return TermValue(result, result, u('integer'))
elif kind == u('integer'):
return TermValue(result, result, 'integer')
elif kind == 'integer':
v = int(float(v))
return TermValue(v, v, kind)
elif kind == u('float'):
elif kind == 'float':
v = float(v)
return TermValue(v, v, kind)
elif kind == u('bool'):
elif kind == 'bool':
if isinstance(v, string_types):
v = not v.strip().lower() in [u('false'), u('f'), u('no'),
u('n'), u('none'), u('0'),
u('[]'), u('{}'), u('')]
v = not v.strip().lower() in ['false', 'f', 'no',
'n', 'none', '0',
'[]', '{}', '']
else:
v = bool(v)
return TermValue(v, v, kind)
elif isinstance(v, string_types):
# string quoting
return TermValue(v, stringify(v), u('string'))
return TermValue(v, stringify(v), 'string')
else:
raise TypeError("Cannot compare {v} of type {typ} to {kind} column"
.format(v=v, typ=type(v), kind=kind))
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from pandas import compat
from pandas.compat import (
PY36, Iterator, StringIO, lmap, lzip, raise_with_traceback,
string_and_binary_types, u)
string_and_binary_types)
from pandas.compat.numpy import function as nv
from pandas.core.dtypes.cast import (
maybe_upcast,
Expand Down Expand Up @@ -620,7 +620,7 @@ def __unicode__(self):
Invoked by unicode(df) in py2 only. Yields a Unicode String in both
py2/py3.
"""
buf = StringIO(u(""))
buf = StringIO("")
if self._info_repr():
self.info(buf=buf)
return buf.getvalue()
Expand All @@ -644,7 +644,7 @@ def _repr_html_(self):
Mainly for IPython notebook.
"""
if self._info_repr():
buf = StringIO(u(""))
buf = StringIO("")
self.info(buf=buf)
# need to escape the <class>, should be the first line.
val = buf.getvalue().replace('<', r'&lt;', 1)
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from pandas._libs.tslibs import OutOfBoundsDatetime, Timedelta, Timestamp
from pandas._libs.tslibs.timezones import tz_compare
import pandas.compat as compat
from pandas.compat import set_function_name, u
from pandas.compat import set_function_name
from pandas.compat.numpy import function as nv
from pandas.util._decorators import Appender, Substitution, cache_readonly

Expand Down Expand Up @@ -931,14 +931,14 @@ def __unicode__(self):
attrs = self._format_attrs()
space = self._format_space()

prepr = (u(",%s") %
space).join(u("%s=%s") % (k, v) for k, v in attrs)
prepr = (",%s" %
space).join("%s=%s" % (k, v) for k, v in attrs)

# no data provided, just attributes
if data is None:
data = ''

res = u("%s(%s%s)") % (klass, data, prepr)
res = "%s(%s%s)" % (klass, data, prepr)

return res

Expand Down
13 changes: 6 additions & 7 deletions pandas/core/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import numpy as np

import pandas.compat as compat
from pandas.compat import u
from pandas.compat.numpy import function as nv
from pandas.util._decorators import Appender, Substitution, deprecate_kwarg
from pandas.util._validators import validate_axis_style_args
Expand Down Expand Up @@ -356,18 +355,18 @@ def __unicode__(self):

class_name = str(self.__class__)

dims = u('Dimensions: {dimensions}'.format(dimensions=' x '.join(
dims = 'Dimensions: {dimensions}'.format(dimensions=' x '.join(
["{shape} ({axis})".format(shape=shape, axis=axis) for axis, shape
in zip(self._AXIS_ORDERS, self.shape)])))
in zip(self._AXIS_ORDERS, self.shape)]))

def axis_pretty(a):
v = getattr(self, a)
if len(v) > 0:
return u('{ax} axis: {x} to {y}'.format(ax=a.capitalize(),
x=pprint_thing(v[0]),
y=pprint_thing(v[-1])))
return '{ax} axis: {x} to {y}'.format(ax=a.capitalize(),
x=pprint_thing(v[0]),
y=pprint_thing(v[-1]))
else:
return u('{ax} axis: None'.format(ax=a.capitalize()))
return '{ax} axis: None'.format(ax=a.capitalize())

output = '\n'.join(
[class_name, dims] + [axis_pretty(a) for a in self._AXIS_ORDERS])
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/reshape/reshape.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from pandas._libs import algos as _algos, reshape as _reshape
from pandas._libs.sparse import IntIndex
from pandas.compat import PY2, text_type, u
from pandas.compat import PY2, text_type

from pandas.core.dtypes.cast import maybe_promote
from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -914,7 +914,7 @@ def _make_col_name(prefix, prefix_sep, level):
if PY2 and (isinstance(prefix, text_type) or
isinstance(prefix_sep, text_type) or
isinstance(level, text_type)):
fstr = u(fstr)
fstr = fstr
return fstr.format(prefix=prefix,
prefix_sep=prefix_sep,
level=level)
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from pandas._libs import iNaT, index as libindex, lib, tslibs
import pandas.compat as compat
from pandas.compat import PY36, StringIO, u
from pandas.compat import PY36, StringIO
from pandas.compat.numpy import function as nv
from pandas.util._decorators import Appender, Substitution, deprecate
from pandas.util._validators import validate_bool_kwarg
Expand Down Expand Up @@ -1379,7 +1379,7 @@ def __unicode__(self):
Invoked by unicode(df) in py2 only. Yields a Unicode String in both
py2/py3.
"""
buf = StringIO(u(""))
buf = StringIO("")
width, height = get_terminal_size()
max_rows = (height if get_option("display.max_rows") == 0 else
get_option("display.max_rows"))
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2833,7 +2833,7 @@ def normalize(self, form):
normalized : Series/Index of objects
"""
import unicodedata
f = lambda x: unicodedata.normalize(form, compat.u_safe(x))
f = lambda x: unicodedata.normalize(form, x)
result = _na_map(f, self._parent)
return self._wrap_result(result)

Expand Down Expand Up @@ -3187,10 +3187,10 @@ def rindex(self, sub, start=0, end=None):
istitle = _noarg_wrapper(lambda x: x.istitle(),
docstring=_shared_docs['ismethods'] %
_shared_docs['istitle'])
isnumeric = _noarg_wrapper(lambda x: compat.u_safe(x).isnumeric(),
isnumeric = _noarg_wrapper(lambda x: x.isnumeric(),
docstring=_shared_docs['ismethods'] %
_shared_docs['isnumeric'])
isdecimal = _noarg_wrapper(lambda x: compat.u_safe(x).isdecimal(),
isdecimal = _noarg_wrapper(lambda x: x.isdecimal(),
docstring=_shared_docs['ismethods'] %
_shared_docs['isdecimal'])

Expand Down
4 changes: 2 additions & 2 deletions pandas/io/excel/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import warnings

import pandas.compat as compat
from pandas.compat import add_metaclass, string_types, u
from pandas.compat import add_metaclass, string_types
from pandas.errors import EmptyDataError
from pandas.util._decorators import Appender, deprecate_kwarg

Expand Down Expand Up @@ -715,7 +715,7 @@ def check_extension(cls, ext):
if ext.startswith('.'):
ext = ext[1:]
if not any(ext in extension for extension in cls.supported_extensions):
msg = (u("Invalid extension for engine '{engine}': '{ext}'")
msg = ("Invalid extension for engine '{engine}': '{ext}'"
.format(engine=pprint_thing(cls.engine),
ext=pprint_thing(ext)))
raise ValueError(msg)
Expand Down
Loading