Skip to content

CLN: Remove StringMixin from PandasObject #26505

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
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
14 changes: 14 additions & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,20 @@ are returned. (:issue:`21521`)
df.groupby("a").ffill()
``__str__`` methods now call ``__repr__`` rather than vica-versa
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Pandas has until now mostly defined string representations in a Pandas objects's
``__str__``/``__unicode__``/``__bytes__`` methods, and called ``__str__`` from the ``__repr__``
method, if a specific ``__repr__`` method is not found. This is not needed for Python3.
In Pandas 0.25, the string representations of Pandas objects are now generally
defined in ``__repr__``, and calls to ``__str__`` in general now pass the call on to
the ``__repr__``, if a specific ``__str__`` method doesn't exist, as is standard for Python.
This change is backward compatible for direct usage of Pandas, but if you subclass
Pandas objects *and* give your subclasses specific ``__str__``/``__repr__`` methods,
you may have to adjust your ``__str__``/``__repr__`` methods (:issue:`26495`).


.. _whatsnew_0250.api_breaking.deps:

Increased minimum versions for dependencies
Expand Down
6 changes: 1 addition & 5 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -2022,7 +2022,7 @@ def _get_repr(self, length=True, na_rep='NaN', footer=True):
result = formatter.to_string()
return str(result)

def __str__(self):
def __repr__(self):
"""
String representation.
"""
Expand All @@ -2037,10 +2037,6 @@ def __str__(self):

return result

def __repr__(self):
# We want to bypass the ExtensionArray.__repr__
return str(self)

def _maybe_coerce_indexer(self, indexer):
"""
return an indexer coerced to the codes dtype
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1831,7 +1831,7 @@ def _add_comparison_ops(cls):
# ----------
# Formatting
# -----------
def __str__(self):
def __repr__(self):
return '{self}\nFill: {fill}\n{index}'.format(
self=printing.pprint_thing(self),
fill=printing.pprint_thing(self.fill_value),
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def __repr__(self):
return str(self)


class PandasObject(StringMixin, DirNamesMixin):
class PandasObject(DirNamesMixin):

"""baseclass for various pandas objects"""

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

def __str__(self):
def __repr__(self):
"""
Return a string representation for a particular object.
"""
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ def _info_repr(self):
return info_repr_option and not (self._repr_fits_horizontal_() and
self._repr_fits_vertical_())

def __str__(self):
def __repr__(self):
"""
Return a string representation for a particular DataFrame.
"""
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2022,7 +2022,7 @@ def __setstate__(self, state):
# ----------------------------------------------------------------------
# Rendering Methods

def __str__(self):
def __repr__(self):
# string representation based upon iterating over self
# (since, by definition, `PandasContainers` are iterable)
prepr = '[%s]' % ','.join(map(pprint_thing, self))
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,8 @@ def __init__(self, obj, keys=None, axis=0, level=None,
def __len__(self):
return len(self.groups)

def __str__(self):
# TODO: Better str/repr for GroupBy object
def __repr__(self):
# TODO: Better repr for GroupBy object
return object.__repr__(self)

def _assure_grouper(self):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ def __deepcopy__(self, memo=None):
# --------------------------------------------------------------------
# Rendering Methods

def __str__(self):
def __repr__(self):
"""
Return a string representation for this object.
"""
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/frozen.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def values(self):
arr = self.view(np.ndarray).copy()
return arr

def __str__(self):
def __repr__(self):
"""
Return a string representation for this object.
"""
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,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 __str__(self):

def __repr__(self):
# don't want to print out all of the items here
name = pprint_thing(self.__class__.__name__)
if self._is_single_block:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def _post_setstate(self):
def __len__(self):
return len(self.items)

def __str__(self):
def __repr__(self):
output = pprint_thing(self.__class__.__name__)
for i, ax in enumerate(self.axes):
if i == 0:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def _compare_constructor(self, other, func):
# ----------------------------------------------------------------------
# Magic methods

def __str__(self):
def __repr__(self):
"""
Return a string representation for a particular Panel.
"""
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1384,7 +1384,7 @@ def reset_index(self, level=None, drop=False, name=None, inplace=False):
# ----------------------------------------------------------------------
# Rendering Methods

def __str__(self):
def __repr__(self):
"""
Return a string representation for a particular Series.
"""
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/sparse/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,8 @@ 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 __str__(self):
# currently, unicode is same as repr...fixes infinite loop
series_rep = Series.__str__(self)
def __repr__(self):
series_rep = Series.__repr__(self)
rep = '{series}\n{index!r}'.format(series=series_rep,
index=self.sp_index)
return rep
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def _get_window(self, other=None):
def _window_type(self):
return self.__class__.__name__

def __str__(self):
def __repr__(self):
"""
Provide a nice str repr of our rolling object.
"""
Expand Down