Skip to content

Commit ce357d9

Browse files
topper-123jreback
authored andcommitted
CLN: Move base.StringMixin to computations.common (#27746)
1 parent 9c37226 commit ce357d9

File tree

7 files changed

+22
-46
lines changed

7 files changed

+22
-46
lines changed

pandas/core/base.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,6 @@
4646
)
4747

4848

49-
class StringMixin:
50-
"""
51-
Implements string methods so long as object defines a `__str__` method.
52-
"""
53-
54-
# side note - this could be made into a metaclass if more than one
55-
# object needs
56-
57-
# ----------------------------------------------------------------------
58-
# Formatting
59-
60-
def __str__(self):
61-
"""
62-
Return a string representation for a particular Object
63-
"""
64-
raise AbstractMethodError(self)
65-
66-
def __repr__(self):
67-
"""
68-
Return a string representation for a particular object.
69-
"""
70-
return str(self)
71-
72-
7349
class PandasObject(DirNamesMixin):
7450

7551
"""baseclass for various pandas objects"""

pandas/core/computation/common.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,8 @@ def _remove_spaces_column_name(name):
3636

3737
class NameResolutionError(NameError):
3838
pass
39+
40+
41+
class StringMixin:
42+
# TODO: delete this class. Removing this ATM caused a failure.
43+
pass

pandas/core/computation/expr.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
import pandas as pd
1515
from pandas.core import common as com
16-
from pandas.core.base import StringMixin
1716
from pandas.core.computation.common import (
1817
_BACKTICK_QUOTED_STRING,
1918
_remove_spaces_column_name,
@@ -799,7 +798,7 @@ def __init__(self, env, engine, parser, preparser=lambda x: x):
799798
super().__init__(env, engine, parser, preparser=preparser)
800799

801800

802-
class Expr(StringMixin):
801+
class Expr:
803802

804803
"""Object encapsulating an expression.
805804
@@ -831,7 +830,7 @@ def assigner(self):
831830
def __call__(self):
832831
return self.terms(self.env)
833832

834-
def __str__(self):
833+
def __repr__(self):
835834
return printing.pprint_thing(self.terms)
836835

837836
def __len__(self):

pandas/core/computation/ops.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
from pandas.core.dtypes.common import is_list_like, is_scalar
1414

15-
from pandas.core.base import StringMixin
1615
import pandas.core.common as com
1716
from pandas.core.computation.common import _ensure_decoded, _result_type_many
1817
from pandas.core.computation.scope import _DEFAULT_GLOBALS
@@ -63,7 +62,7 @@ def __init__(self, name, is_local):
6362
super().__init__(msg.format(name))
6463

6564

66-
class Term(StringMixin):
65+
class Term:
6766
def __new__(cls, name, env, side=None, encoding=None):
6867
klass = Constant if not isinstance(name, str) else cls
6968
supr_new = super(Term, klass).__new__
@@ -82,7 +81,7 @@ def __init__(self, name, env, side=None, encoding=None):
8281
def local_name(self):
8382
return self.name.replace(_LOCAL_TAG, "")
8483

85-
def __str__(self):
84+
def __repr__(self):
8685
return pprint_thing(self.name)
8786

8887
def __call__(self, *args, **kwargs):
@@ -182,7 +181,7 @@ def _resolve_name(self):
182181
def name(self):
183182
return self.value
184183

185-
def __str__(self):
184+
def __repr__(self):
186185
# in python 2 str() of float
187186
# can truncate shorter than repr()
188187
return repr(self.name)
@@ -191,7 +190,7 @@ def __str__(self):
191190
_bool_op_map = {"not": "~", "and": "&", "or": "|"}
192191

193192

194-
class Op(StringMixin):
193+
class Op:
195194

196195
"""Hold an operator of arbitrary arity
197196
"""
@@ -204,7 +203,7 @@ def __init__(self, op, operands, *args, **kwargs):
204203
def __iter__(self):
205204
return iter(self.operands)
206205

207-
def __str__(self):
206+
def __repr__(self):
208207
"""Print a generic n-ary operator and its operands using infix
209208
notation"""
210209
# recurse over the operands
@@ -537,7 +536,7 @@ def __call__(self, env):
537536
operand = self.operand(env)
538537
return self.func(operand)
539538

540-
def __str__(self):
539+
def __repr__(self):
541540
return pprint_thing("{0}({1})".format(self.op, self.operand))
542541

543542
@property
@@ -562,7 +561,7 @@ def __call__(self, env):
562561
with np.errstate(all="ignore"):
563562
return self.func.func(*operands)
564563

565-
def __str__(self):
564+
def __repr__(self):
566565
operands = map(str, self.operands)
567566
return pprint_thing("{0}({1})".format(self.op, ",".join(operands)))
568567

pandas/core/computation/pytables.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from pandas.core.dtypes.common import is_list_like
1212

1313
import pandas as pd
14-
from pandas.core.base import StringMixin
1514
import pandas.core.common as com
1615
from pandas.core.computation import expr, ops
1716
from pandas.core.computation.common import _ensure_decoded
@@ -32,8 +31,7 @@ def __init__(self, level, global_dict=None, local_dict=None, queryables=None):
3231
class Term(ops.Term):
3332
def __new__(cls, name, env, side=None, encoding=None):
3433
klass = Constant if not isinstance(name, str) else cls
35-
supr_new = StringMixin.__new__
36-
return supr_new(klass)
34+
return object.__new__(klass)
3735

3836
def __init__(self, name, env, side=None, encoding=None):
3937
super().__init__(name, env, side=side, encoding=encoding)
@@ -231,7 +229,7 @@ def convert_values(self):
231229

232230

233231
class FilterBinOp(BinOp):
234-
def __str__(self):
232+
def __repr__(self):
235233
return pprint_thing(
236234
"[Filter : [{lhs}] -> [{op}]".format(lhs=self.filter[0], op=self.filter[1])
237235
)
@@ -297,7 +295,7 @@ def evaluate(self):
297295

298296

299297
class ConditionBinOp(BinOp):
300-
def __str__(self):
298+
def __repr__(self):
301299
return pprint_thing("[Condition : [{cond}]]".format(cond=self.condition))
302300

303301
def invert(self):
@@ -548,7 +546,7 @@ def __init__(self, where, queryables=None, encoding=None, scope_level=0):
548546
)
549547
self.terms = self.parse()
550548

551-
def __str__(self):
549+
def __repr__(self):
552550
if self.terms is not None:
553551
return pprint_thing(self.terms)
554552
return pprint_thing(self.expr)

pandas/core/computation/scope.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
from pandas._libs.tslibs import Timestamp
1616
from pandas.compat.chainmap import DeepChainMap
1717

18-
from pandas.core.base import StringMixin
1918
import pandas.core.computation as compu
19+
from pandas.core.computation.common import StringMixin
2020

2121

2222
def _ensure_scope(
@@ -141,7 +141,7 @@ def __init__(
141141
self.resolvers = DeepChainMap(*resolvers)
142142
self.temps = {}
143143

144-
def __str__(self):
144+
def __repr__(self):
145145
scope_keys = _get_pretty_string(list(self.scope.keys()))
146146
res_keys = _get_pretty_string(list(self.resolvers.keys()))
147147
unicode_str = "{name}(scope={scope_keys}, resolvers={res_keys})"

pandas/tests/series/test_repr.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
period_range,
1515
timedelta_range,
1616
)
17-
from pandas.core.base import StringMixin
1817
from pandas.core.index import MultiIndex
1918
import pandas.util.testing as tm
2019

@@ -226,11 +225,11 @@ class TestCategoricalRepr:
226225
def test_categorical_repr_unicode(self):
227226
# see gh-21002
228227

229-
class County(StringMixin):
228+
class County:
230229
name = "San Sebastián"
231230
state = "PR"
232231

233-
def __str__(self):
232+
def __repr__(self):
234233
return self.name + ", " + self.state
235234

236235
cat = pd.Categorical([County() for _ in range(61)])

0 commit comments

Comments
 (0)