Skip to content

CLN: remove __bytes__ #26447

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 1 commit into from
May 19, 2019
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ Other API Changes
- Bug in :meth:`DatetimeIndex.snap` which didn't preserving the ``name`` of the input :class:`Index` (:issue:`25575`)
- The ``arg`` argument in :meth:`pandas.core.groupby.DataFrameGroupBy.agg` has been renamed to ``func`` (:issue:`26089`)
- The ``arg`` argument in :meth:`pandas.core.window._Window.aggregate` has been renamed to ``func`` (:issue:`26372`)
- Most Pandas classes had a ``__bytes__`` method, which was used for getting a python2-style bytestring representation of the object. This method has been removed as a part of dropping Python2 (:issue:`26447`)

.. _whatsnew_0250.deprecations:

Expand Down
9 changes: 0 additions & 9 deletions pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,6 @@ def __str__(self):
"""
raise AbstractMethodError(self)

def __bytes__(self):
"""
Return a bytes representation for a particular object.
"""
from pandas._config import get_option

encoding = get_option("display.encoding")
return str(self).encode(encoding, 'replace')

def __repr__(self):
"""
Return a string representation for a particular object.
Expand Down
9 changes: 0 additions & 9 deletions pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,6 @@ def __str__(self):
"""
return self.name

def __bytes__(self):
"""
Return a string representation for a particular object.
"""
from pandas._config import get_option

encoding = get_option("display.encoding")
return str(self).encode(encoding, 'replace')

def __repr__(self):
"""
Return a string representation for a particular object.
Expand Down
9 changes: 6 additions & 3 deletions pandas/tests/frame/test_repr_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,12 @@ def test_unicode_string_with_unicode(self):
df = DataFrame({'A': ["\u05d0"]})
str(df)

def test_bytestring_with_unicode(self):
df = DataFrame({'A': ["\u05d0"]})
bytes(df)
def test_str_to_bytes_raises(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think OK to just remove these tests; error message just matches stdlib

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just keep them? these things are now (after this PR) supposed to raise,so just have a test for that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not hugely tied to it but these tests are making assertions about things that the stdlib does instead of something pandas explicitly does, so just seems strange to me that have that in our test suite. Let's see what others think

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I see your point. Not a hard point for me either. I can remove them later tonight.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think these tests are fine; pretty innocuous and prevent adding back a __bytes__.

# GH 26447
df = DataFrame({'A': ["abc"]})
msg = "^'str' object cannot be interpreted as an integer$"
with pytest.raises(TypeError, match=msg):
bytes(df)

def test_very_wide_info_repr(self):
df = DataFrame(np.random.randn(10, 20),
Expand Down
6 changes: 0 additions & 6 deletions pandas/tests/indexes/multi/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,6 @@ def test_unicode_string_with_unicode():
str(idx)


def test_bytestring_with_unicode():
d = {"a": ["\u05d0", 2, 3], "b": [4, 5, 6], "c": [7, 8, 9]}
idx = pd.DataFrame(d).set_index(["a", "b"]).index
bytes(idx)


def test_repr_max_seq_item_setting(idx):
# GH10182
idx = idx.repeat(50)
Expand Down
10 changes: 6 additions & 4 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2388,10 +2388,12 @@ def test_print_unicode_columns(self):
"c": [7, 8, 9]})
repr(df.columns) # should not raise UnicodeDecodeError

@pytest.mark.parametrize("func", [str, bytes])
def test_with_unicode(self, func):
index = Index(list(range(1000)))
func(index)
def test_str_to_bytes_raises(self):
# GH 26447
index = Index([str(x) for x in range(10)])
msg = "^'str' object cannot be interpreted as an integer$"
with pytest.raises(TypeError, match=msg):
bytes(index)

def test_intersect_str_dates(self):
dt_dates = [datetime(2012, 2, 9), datetime(2012, 2, 22)]
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/indexes/test_frozen.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import warnings

import numpy as np
import pytest

from pandas.core.indexes.frozen import FrozenList, FrozenNDArray
from pandas.tests.test_base import CheckImmutable, CheckStringMixin
Expand Down Expand Up @@ -49,6 +50,12 @@ def test_difference_dupe(self):
expected = FrozenList([1, 3])
self.check_result(result, expected)

def test_tricky_container_to_bytes_raises(self):
# GH 26447
msg = "^'str' object cannot be interpreted as an integer$"
with pytest.raises(TypeError, match=msg):
bytes(self.unicode_container)


class TestFrozenNDArray(CheckImmutable, CheckStringMixin):
mutable_methods = ('put', 'itemset', 'fill')
Expand All @@ -68,6 +75,9 @@ def test_constructor_warns(self):
with tm.assert_produces_warning(FutureWarning):
FrozenNDArray([1, 2, 3])

def test_tricky_container_to_bytes(self):
bytes(self.unicode_container)

def test_shallow_copying(self):
original = self.container.copy()
assert isinstance(self.container.view(), FrozenNDArray)
Expand Down
10 changes: 7 additions & 3 deletions pandas/tests/series/test_repr.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import datetime, timedelta

import numpy as np
import pytest

import pandas as pd
from pandas import (
Expand Down Expand Up @@ -152,9 +153,12 @@ def test_unicode_string_with_unicode(self):
df = Series(["\u05d0"], name="\u05d1")
str(df)

def test_bytestring_with_unicode(self):
df = Series(["\u05d0"], name="\u05d1")
bytes(df)
def test_str_to_bytes_raises(self):
# GH 26447
df = Series(["abc"], name="abc")
msg = "^'str' object cannot be interpreted as an integer$"
with pytest.raises(TypeError, match=msg):
bytes(df)

def test_timeseries_repr_object_dtype(self):
index = Index([datetime(2000, 1, 1) + timedelta(i)
Expand Down
1 change: 0 additions & 1 deletion pandas/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def test_tricky_container(self):
pytest.skip('Need unicode_container to test with this')
repr(self.unicode_container)
str(self.unicode_container)
bytes(self.unicode_container)
Copy link
Contributor Author

@topper-123 topper-123 May 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This raises in the FrozenList case, but not the FrozenNDArray case. So I move the bytes test to test_frozen.py and have seperate tests for FrozenList and FrozenNDArray.



class CheckImmutable:
Expand Down