Skip to content

Commit 7eb7bb9

Browse files
topper-123jreback
authored andcommitted
CLN: Remove StringMixin from PandasObject (#26505)
1 parent 3de0453 commit 7eb7bb9

File tree

15 files changed

+31
-23
lines changed

15 files changed

+31
-23
lines changed

doc/source/whatsnew/v0.25.0.rst

+14
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

+1-5
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

+1-1
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

+2-2
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

+1-1
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

+1-1
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

+2-2
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

+1-1
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

+1-1
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

+1-2
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:

pandas/core/internals/managers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ def _post_setstate(self):
291291
def __len__(self):
292292
return len(self.items)
293293

294-
def __str__(self):
294+
def __repr__(self):
295295
output = pprint_thing(self.__class__.__name__)
296296
for i, ax in enumerate(self.axes):
297297
if i == 0:

pandas/core/panel.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ def _compare_constructor(self, other, func):
340340
# ----------------------------------------------------------------------
341341
# Magic methods
342342

343-
def __str__(self):
343+
def __repr__(self):
344344
"""
345345
Return a string representation for a particular Panel.
346346
"""

pandas/core/series.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1384,7 +1384,7 @@ def reset_index(self, level=None, drop=False, name=None, inplace=False):
13841384
# ----------------------------------------------------------------------
13851385
# Rendering Methods
13861386

1387-
def __str__(self):
1387+
def __repr__(self):
13881388
"""
13891389
Return a string representation for a particular Series.
13901390
"""

pandas/core/sparse/series.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,8 @@ def as_sparse_array(self, kind=None, fill_value=None, copy=False):
217217
return SparseArray(self.values, sparse_index=self.sp_index,
218218
fill_value=fill_value, kind=kind, copy=copy)
219219

220-
def __str__(self):
221-
# currently, unicode is same as repr...fixes infinite loop
222-
series_rep = Series.__str__(self)
220+
def __repr__(self):
221+
series_rep = Series.__repr__(self)
223222
rep = '{series}\n{index!r}'.format(series=series_rep,
224223
index=self.sp_index)
225224
return rep

pandas/core/window.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def _get_window(self, other=None):
157157
def _window_type(self):
158158
return self.__class__.__name__
159159

160-
def __str__(self):
160+
def __repr__(self):
161161
"""
162162
Provide a nice str repr of our rolling object.
163163
"""

0 commit comments

Comments
 (0)