Skip to content

Commit a17e570

Browse files
committed
CLN: Remove StringMixin from PandasObject
1 parent 6d2398a commit a17e570

File tree

15 files changed

+31
-23
lines changed

15 files changed

+31
-23
lines changed

doc/source/whatsnew/v0.25.0.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,20 @@ are returned. (:issue:`21521`)
213213
df.groupby("a").ffill()
214214
215215
216+
``__str__`` methods now call ``__repr__`` rather than vica-versa
217+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
218+
219+
Pandas has until now mostly defined string representations in a Pandas objects's
220+
``__str__``/``__unicode__``/``__bytes__`` methods, and called ``__str__`` from the ``__repr__``
221+
method, if a specific ``__repr__`` method is not found. This is not needed for Python3.
222+
In Pandas 0.25, the string representations of Pandas objects are now generally
223+
defined in ``__repr__``, and calls to ``__str__`` in general now pass the call on to
224+
the ``__repr__``, if a specific ``__str__`` method doesn't exist, as is standard for Python.
225+
This change is backward compatible for direct usage of Pandas, but if you subclass
226+
Pandas objects *and* give your subclasses specific ``__str__``/``__repr__`` methods,
227+
you may have to adjust your ``__str__``/``__repr__`` methods (:issue:`26495`).
228+
229+
216230
.. _whatsnew_0250.api_breaking.deps:
217231

218232
Increased minimum versions for dependencies

pandas/core/arrays/categorical.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,7 +2022,7 @@ def _get_repr(self, length=True, na_rep='NaN', footer=True):
20222022
result = formatter.to_string()
20232023
return str(result)
20242024

2025-
def __str__(self):
2025+
def __repr__(self):
20262026
"""
20272027
String representation.
20282028
"""
@@ -2037,10 +2037,6 @@ def __str__(self):
20372037

20382038
return result
20392039

2040-
def __repr__(self):
2041-
# We want to bypass the ExtensionArray.__repr__
2042-
return str(self)
2043-
20442040
def _maybe_coerce_indexer(self, indexer):
20452041
"""
20462042
return an indexer coerced to the codes dtype

pandas/core/arrays/sparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1831,7 +1831,7 @@ def _add_comparison_ops(cls):
18311831
# ----------
18321832
# Formatting
18331833
# -----------
1834-
def __str__(self):
1834+
def __repr__(self):
18351835
return '{self}\nFill: {fill}\n{index}'.format(
18361836
self=printing.pprint_thing(self),
18371837
fill=printing.pprint_thing(self.fill_value),

pandas/core/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def __repr__(self):
5555
return str(self)
5656

5757

58-
class PandasObject(StringMixin, DirNamesMixin):
58+
class PandasObject(DirNamesMixin):
5959

6060
"""baseclass for various pandas objects"""
6161

@@ -64,7 +64,7 @@ def _constructor(self):
6464
"""class constructor (for this class it's just `__class__`"""
6565
return self.__class__
6666

67-
def __str__(self):
67+
def __repr__(self):
6868
"""
6969
Return a string representation for a particular object.
7070
"""

pandas/core/frame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ def _info_repr(self):
610610
return info_repr_option and not (self._repr_fits_horizontal_() and
611611
self._repr_fits_vertical_())
612612

613-
def __str__(self):
613+
def __repr__(self):
614614
"""
615615
Return a string representation for a particular DataFrame.
616616
"""

pandas/core/generic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2022,7 +2022,7 @@ def __setstate__(self, state):
20222022
# ----------------------------------------------------------------------
20232023
# Rendering Methods
20242024

2025-
def __str__(self):
2025+
def __repr__(self):
20262026
# string representation based upon iterating over self
20272027
# (since, by definition, `PandasContainers` are iterable)
20282028
prepr = '[%s]' % ','.join(map(pprint_thing, self))

pandas/core/groupby/groupby.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,8 @@ def __init__(self, obj, keys=None, axis=0, level=None,
373373
def __len__(self):
374374
return len(self.groups)
375375

376-
def __str__(self):
377-
# TODO: Better str/repr for GroupBy object
376+
def __repr__(self):
377+
# TODO: Better repr for GroupBy object
378378
return object.__repr__(self)
379379

380380
def _assure_grouper(self):

pandas/core/indexes/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ def __deepcopy__(self, memo=None):
932932
# --------------------------------------------------------------------
933933
# Rendering Methods
934934

935-
def __str__(self):
935+
def __repr__(self):
936936
"""
937937
Return a string representation for this object.
938938
"""

pandas/core/indexes/frozen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def values(self):
149149
arr = self.view(np.ndarray).copy()
150150
return arr
151151

152-
def __str__(self):
152+
def __repr__(self):
153153
"""
154154
Return a string representation for this object.
155155
"""

pandas/core/internals/blocks.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,7 @@ def make_block_same_class(self, values, placement=None, ndim=None,
233233
return make_block(values, placement=placement, ndim=ndim,
234234
klass=self.__class__, dtype=dtype)
235235

236-
def __str__(self):
237-
236+
def __repr__(self):
238237
# don't want to print out all of the items here
239238
name = pprint_thing(self.__class__.__name__)
240239
if self._is_single_block:

0 commit comments

Comments
 (0)