From 6a38d126bada2aa83ec948b750cd7a13257ac915 Mon Sep 17 00:00:00 2001 From: tp Date: Thu, 16 May 2019 21:55:40 +0200 Subject: [PATCH] CLN: remove __unicode__ --- pandas/core/arrays/categorical.py | 4 ++-- pandas/core/arrays/sparse.py | 2 +- pandas/core/base.py | 13 +++++-------- pandas/core/computation/expr.py | 2 +- pandas/core/computation/ops.py | 10 +++++----- pandas/core/computation/pytables.py | 6 +++--- pandas/core/computation/scope.py | 2 +- pandas/core/frame.py | 4 ++-- pandas/core/generic.py | 4 ++-- pandas/core/groupby/groupby.py | 4 ++-- pandas/core/indexes/base.py | 4 ++-- pandas/core/indexes/frozen.py | 6 +++--- pandas/core/internals/blocks.py | 2 +- pandas/core/internals/managers.py | 2 +- pandas/core/panel.py | 4 ++-- pandas/core/resample.py | 2 +- pandas/core/series.py | 4 ++-- pandas/core/sparse/series.py | 4 ++-- pandas/core/window.py | 2 +- pandas/io/pytables.py | 10 +++++----- pandas/io/stata.py | 2 +- pandas/tests/extension/decimal/test_decimal.py | 3 ++- pandas/tests/indexes/datetimes/test_formats.py | 2 +- pandas/tests/indexes/period/test_formats.py | 2 +- pandas/tests/indexes/timedeltas/test_formats.py | 2 +- pandas/tests/series/test_repr.py | 6 +++--- 26 files changed, 53 insertions(+), 55 deletions(-) diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 6eff6d9ae1091..d25ccd1b158be 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -2022,9 +2022,9 @@ def _get_repr(self, length=True, na_rep='NaN', footer=True): result = formatter.to_string() return str(result) - def __unicode__(self): + def __str__(self): """ - Unicode representation. + String representation. """ _maxlen = 10 if len(self._codes) > _maxlen: diff --git a/pandas/core/arrays/sparse.py b/pandas/core/arrays/sparse.py index 66ccd6848864d..2aaa00ff01355 100644 --- a/pandas/core/arrays/sparse.py +++ b/pandas/core/arrays/sparse.py @@ -1823,7 +1823,7 @@ def _add_comparison_ops(cls): # ---------- # Formatting # ----------- - def __unicode__(self): + def __str__(self): return '{self}\nFill: {fill}\n{index}'.format( self=printing.pprint_thing(self), fill=printing.pprint_thing(self.fill_value), diff --git a/pandas/core/base.py b/pandas/core/base.py index 5bccaeef66a82..1d0e7fc413eb9 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -34,7 +34,7 @@ class StringMixin: """ - Implements string methods so long as object defines a `__unicode__` method. + Implements string methods so long as object defines a `__str__` method. """ # side note - this could be made into a metaclass if more than one # object needs @@ -42,23 +42,20 @@ class StringMixin: # ---------------------------------------------------------------------- # Formatting - def __unicode__(self): - raise AbstractMethodError(self) - def __str__(self): """ Return a string representation for a particular Object """ - return self.__unicode__() + raise AbstractMethodError(self) def __bytes__(self): """ - Return a string representation for a particular object. + Return a bytes representation for a particular object. """ from pandas._config import get_option encoding = get_option("display.encoding") - return self.__unicode__().encode(encoding, 'replace') + return str(self).encode(encoding, 'replace') def __repr__(self): """ @@ -76,7 +73,7 @@ def _constructor(self): """class constructor (for this class it's just `__class__`""" return self.__class__ - def __unicode__(self): + def __str__(self): """ Return a string representation for a particular object. """ diff --git a/pandas/core/computation/expr.py b/pandas/core/computation/expr.py index cb9d537e61ebc..628a2244d0a6f 100644 --- a/pandas/core/computation/expr.py +++ b/pandas/core/computation/expr.py @@ -734,7 +734,7 @@ def assigner(self): def __call__(self): return self.terms(self.env) - def __unicode__(self): + def __str__(self): return printing.pprint_thing(self.terms) def __len__(self): diff --git a/pandas/core/computation/ops.py b/pandas/core/computation/ops.py index a79b1d1e58977..fd96739f4da76 100644 --- a/pandas/core/computation/ops.py +++ b/pandas/core/computation/ops.py @@ -67,7 +67,7 @@ def __init__(self, name, env, side=None, encoding=None): def local_name(self): return self.name.replace(_LOCAL_TAG, '') - def __unicode__(self): + def __str__(self): return pprint_thing(self.name) def __call__(self, *args, **kwargs): @@ -166,7 +166,7 @@ def _resolve_name(self): def name(self): return self.value - def __unicode__(self): + def __str__(self): # in python 2 str() of float # can truncate shorter than repr() return repr(self.name) @@ -188,7 +188,7 @@ def __init__(self, op, operands, *args, **kwargs): def __iter__(self): return iter(self.operands) - def __unicode__(self): + def __str__(self): """Print a generic n-ary operator and its operands using infix notation""" # recurse over the operands @@ -506,7 +506,7 @@ def __call__(self, env): operand = self.operand(env) return self.func(operand) - def __unicode__(self): + def __str__(self): return pprint_thing('{0}({1})'.format(self.op, self.operand)) @property @@ -531,7 +531,7 @@ def __call__(self, env): with np.errstate(all='ignore'): return self.func.func(*operands) - def __unicode__(self): + def __str__(self): operands = map(str, self.operands) return pprint_thing('{0}({1})'.format(self.op, ','.join(operands))) diff --git a/pandas/core/computation/pytables.py b/pandas/core/computation/pytables.py index 7365eea26e005..2a762b5ee24b6 100644 --- a/pandas/core/computation/pytables.py +++ b/pandas/core/computation/pytables.py @@ -230,7 +230,7 @@ def convert_values(self): class FilterBinOp(BinOp): - def __unicode__(self): + def __str__(self): return pprint_thing("[Filter : [{lhs}] -> [{op}]" .format(lhs=self.filter[0], op=self.filter[1])) @@ -302,7 +302,7 @@ def evaluate(self): class ConditionBinOp(BinOp): - def __unicode__(self): + def __str__(self): return pprint_thing("[Condition : [{cond}]]" .format(cond=self.condition)) @@ -549,7 +549,7 @@ def __init__(self, where, queryables=None, encoding=None, scope_level=0): encoding=encoding) self.terms = self.parse() - def __unicode__(self): + def __str__(self): if self.terms is not None: return pprint_thing(self.terms) return pprint_thing(self.expr) diff --git a/pandas/core/computation/scope.py b/pandas/core/computation/scope.py index 185b5dd36e4fc..729acdc52e24a 100644 --- a/pandas/core/computation/scope.py +++ b/pandas/core/computation/scope.py @@ -135,7 +135,7 @@ def __init__(self, level, global_dict=None, local_dict=None, resolvers=(), self.resolvers = DeepChainMap(*resolvers) self.temps = {} - def __unicode__(self): + def __str__(self): scope_keys = _get_pretty_string(list(self.scope.keys())) res_keys = _get_pretty_string(list(self.resolvers.keys())) unicode_str = '{name}(scope={scope_keys}, resolvers={res_keys})' diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 6ec36c62f0be8..76fd393341694 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -610,9 +610,9 @@ def _info_repr(self): return info_repr_option and not (self._repr_fits_horizontal_() and self._repr_fits_vertical_()) - def __unicode__(self): + def __str__(self): """ - Return a unicode string representation for a particular DataFrame. + Return a string representation for a particular DataFrame. """ buf = StringIO("") if self._info_repr(): diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 4999153207395..f105e7f516e4b 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -2022,8 +2022,8 @@ def __setstate__(self, state): # ---------------------------------------------------------------------- # Rendering Methods - def __unicode__(self): - # unicode representation based upon iterating over self + def __str__(self): + # string representation based upon iterating over self # (since, by definition, `PandasContainers` are iterable) prepr = '[%s]' % ','.join(map(pprint_thing, self)) return '%s(%s)' % (self.__class__.__name__, prepr) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index ae28c23b85a39..4e9e3b4963b6d 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -373,8 +373,8 @@ def __init__(self, obj, keys=None, axis=0, level=None, def __len__(self): return len(self.groups) - def __unicode__(self): - # TODO: Better unicode/repr for GroupBy object + def __str__(self): + # TODO: Better str/repr for GroupBy object return object.__repr__(self) def _assure_grouper(self): diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index bb12c2bc43a38..dd3717813ce3f 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -933,9 +933,9 @@ def __deepcopy__(self, memo=None): # -------------------------------------------------------------------- # Rendering Methods - def __unicode__(self): + def __str__(self): """ - Return a unicode string representation for this object. + Return a string representation for this object. """ klass = self.__class__.__name__ data = self._format_data() diff --git a/pandas/core/indexes/frozen.py b/pandas/core/indexes/frozen.py index 2132147130e6d..60e4253e3101b 100644 --- a/pandas/core/indexes/frozen.py +++ b/pandas/core/indexes/frozen.py @@ -108,7 +108,7 @@ def _disabled(self, *args, **kwargs): raise TypeError("'%s' does not support mutable operations." % self.__class__.__name__) - def __unicode__(self): + def __str__(self): return pprint_thing(self, quote_strings=True, escape_chars=('\t', '\r', '\n')) @@ -149,9 +149,9 @@ def values(self): arr = self.view(np.ndarray).copy() return arr - def __unicode__(self): + def __str__(self): """ - Return a unicode string representation for this object. + Return a string representation for this object. """ prepr = pprint_thing(self, escape_chars=('\t', '\r', '\n'), quote_strings=True) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 0c49ebb55acdd..7e8881f509e35 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -232,7 +232,7 @@ def make_block_same_class(self, values, placement=None, ndim=None, return make_block(values, placement=placement, ndim=ndim, klass=self.__class__, dtype=dtype) - def __unicode__(self): + def __str__(self): # don't want to print out all of the items here name = pprint_thing(self.__class__.__name__) diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 7077b1556e7e8..e5abac5d7a94f 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -291,7 +291,7 @@ def _post_setstate(self): def __len__(self): return len(self.items) - def __unicode__(self): + def __str__(self): output = pprint_thing(self.__class__.__name__) for i, ax in enumerate(self.axes): if i == 0: diff --git a/pandas/core/panel.py b/pandas/core/panel.py index 95a979c361cd7..b6b957c543df6 100644 --- a/pandas/core/panel.py +++ b/pandas/core/panel.py @@ -340,9 +340,9 @@ def _compare_constructor(self, other, func): # ---------------------------------------------------------------------- # Magic methods - def __unicode__(self): + def __str__(self): """ - Return a unicode string representation for a particular Panel. + Return a string representation for a particular Panel. """ class_name = str(self.__class__) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 7bf0c56c42214..874973846a006 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -79,7 +79,7 @@ def __init__(self, obj, groupby=None, axis=0, kind=None, **kwargs): if self.groupby is not None: self.groupby._set_grouper(self._convert_obj(obj), sort=True) - def __unicode__(self): + def __str__(self): """ Provide a nice str repr of our rolling object. """ diff --git a/pandas/core/series.py b/pandas/core/series.py index f0b674596656a..03745eb3394b6 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1387,9 +1387,9 @@ def reset_index(self, level=None, drop=False, name=None, inplace=False): # ---------------------------------------------------------------------- # Rendering Methods - def __unicode__(self): + def __str__(self): """ - Return a unicode string representation for a particular DataFrame. + Return a string representation for a particular Series. """ buf = StringIO("") width, height = get_terminal_size() diff --git a/pandas/core/sparse/series.py b/pandas/core/sparse/series.py index fc14b20f0c6fe..ae1c94e136475 100644 --- a/pandas/core/sparse/series.py +++ b/pandas/core/sparse/series.py @@ -217,9 +217,9 @@ def as_sparse_array(self, kind=None, fill_value=None, copy=False): return SparseArray(self.values, sparse_index=self.sp_index, fill_value=fill_value, kind=kind, copy=copy) - def __unicode__(self): + def __str__(self): # currently, unicode is same as repr...fixes infinite loop - series_rep = Series.__unicode__(self) + series_rep = Series.__str__(self) rep = '{series}\n{index!r}'.format(series=series_rep, index=self.sp_index) return rep diff --git a/pandas/core/window.py b/pandas/core/window.py index 2d7fdbeffbccc..cc74a8fa24a05 100644 --- a/pandas/core/window.py +++ b/pandas/core/window.py @@ -156,7 +156,7 @@ def _get_window(self, other=None): def _window_type(self): return self.__class__.__name__ - def __unicode__(self): + def __str__(self): """ Provide a nice str repr of our rolling object. """ diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index b0a00f25c538d..189c783bb176f 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -519,7 +519,7 @@ def __contains__(self, key): def __len__(self): return len(self.groups()) - def __unicode__(self): + def __str__(self): return '{type}\nFile path: {path}\n'.format( type=type(self), path=pprint_thing(self._path)) @@ -1586,7 +1586,7 @@ def set_table(self, table): self.table = table return self - def __unicode__(self): + def __str__(self): temp = tuple( map(pprint_thing, (self.name, @@ -1880,7 +1880,7 @@ def __init__(self, values=None, kind=None, typ=None, self.set_data(data) self.set_metadata(metadata) - def __unicode__(self): + def __str__(self): temp = tuple( map(pprint_thing, (self.name, @@ -2335,7 +2335,7 @@ def pandas_type(self): def format_type(self): return 'fixed' - def __unicode__(self): + def __str__(self): """ return a pretty representation of myself """ self.infer_axes() s = self.shape @@ -3076,7 +3076,7 @@ def table_type_short(self): def format_type(self): return 'table' - def __unicode__(self): + def __str__(self): """ return a pretty representatgion of myself """ self.infer_axes() dc = ",dc->[{columns}]".format(columns=(','.join( diff --git a/pandas/io/stata.py b/pandas/io/stata.py index c59e21678ed1e..27ddc4ef6f594 100644 --- a/pandas/io/stata.py +++ b/pandas/io/stata.py @@ -804,7 +804,7 @@ def __init__(self, value): value = property(lambda self: self._value, doc='The binary representation of the missing value.') - def __unicode__(self): + def __str__(self): return self.string def __repr__(self): diff --git a/pandas/tests/extension/decimal/test_decimal.py b/pandas/tests/extension/decimal/test_decimal.py index 87f67b9843bae..97fae41bcc720 100644 --- a/pandas/tests/extension/decimal/test_decimal.py +++ b/pandas/tests/extension/decimal/test_decimal.py @@ -387,5 +387,6 @@ def _formatting_values(self): ser = pd.Series(DecimalArray2([decimal.Decimal('1.0')])) - with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=True): + with tm.assert_produces_warning(DeprecationWarning, + check_stacklevel=False): repr(ser) diff --git a/pandas/tests/indexes/datetimes/test_formats.py b/pandas/tests/indexes/datetimes/test_formats.py index ddc79a5aaa8fa..37bd68176fe9e 100644 --- a/pandas/tests/indexes/datetimes/test_formats.py +++ b/pandas/tests/indexes/datetimes/test_formats.py @@ -62,7 +62,7 @@ def test_dti_repr_short(self): dr = pd.date_range(start='1/1/2012', periods=3) repr(dr) - @pytest.mark.parametrize('method', ['__repr__', '__unicode__', '__str__']) + @pytest.mark.parametrize('method', ['__repr__', '__str__']) def test_dti_representation(self, method): idxs = [] idxs.append(DatetimeIndex([], freq='D')) diff --git a/pandas/tests/indexes/period/test_formats.py b/pandas/tests/indexes/period/test_formats.py index fbfafd8b46d31..e1d1dd307ea76 100644 --- a/pandas/tests/indexes/period/test_formats.py +++ b/pandas/tests/indexes/period/test_formats.py @@ -61,7 +61,7 @@ def test_frame_repr(self): '2000-01-03 3') assert result == expected - @pytest.mark.parametrize('method', ['__repr__', '__unicode__', '__str__']) + @pytest.mark.parametrize('method', ['__repr__', '__str__']) def test_representation(self, method): # GH#7601 idx1 = PeriodIndex([], freq='D') diff --git a/pandas/tests/indexes/timedeltas/test_formats.py b/pandas/tests/indexes/timedeltas/test_formats.py index bc0368534415d..992e557041ecb 100644 --- a/pandas/tests/indexes/timedeltas/test_formats.py +++ b/pandas/tests/indexes/timedeltas/test_formats.py @@ -5,7 +5,7 @@ class TestTimedeltaIndexRendering: - @pytest.mark.parametrize('method', ['__repr__', '__unicode__', '__str__']) + @pytest.mark.parametrize('method', ['__repr__', '__str__']) def test_representation(self, method): idx1 = TimedeltaIndex([], freq='D') idx2 = TimedeltaIndex(['1 days'], freq='D') diff --git a/pandas/tests/series/test_repr.py b/pandas/tests/series/test_repr.py index 5c41ce360d3a5..032c730fea408 100644 --- a/pandas/tests/series/test_repr.py +++ b/pandas/tests/series/test_repr.py @@ -204,7 +204,7 @@ class County(StringMixin): name = 'San Sebastián' state = 'PR' - def __unicode__(self): + def __str__(self): return self.name + ', ' + self.state cat = pd.Categorical([County() for _ in range(61)]) @@ -219,7 +219,7 @@ def test_categorical_repr(self): exp = ("0 1\n1 2\n2 3\n3 4\n" + "dtype: category\nCategories (4, int64): [1, 2, 3, 4]") - assert exp == a.__unicode__() + assert exp == a.__str__() a = Series(Categorical(["a", "b"] * 25)) exp = ("0 a\n1 b\n" + " ..\n" + "48 a\n49 b\n" + @@ -231,7 +231,7 @@ def test_categorical_repr(self): a = Series(Categorical(["a", "b"], categories=levs, ordered=True)) exp = ("0 a\n1 b\n" + "dtype: category\n" "Categories (26, object): [a < b < c < d ... w < x < y < z]") - assert exp == a.__unicode__() + assert exp == a.__str__() def test_categorical_series_repr(self): s = Series(Categorical([1, 2, 3]))