Skip to content

API: Series[bytes].astype(str) behavior #49658

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 12 commits into from
Feb 15, 2023
5 changes: 4 additions & 1 deletion pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,10 @@ cpdef ndarray[object] ensure_string_array(
continue

if not checknull(val):
if not util.is_float_object(val):
if isinstance(val, bytes):
# GH#?? see test_astype_str_from_bytes
result[i] = val.decode()
elif not util.is_float_object(val):
# f"{val}" is faster than str(val)
result[i] = f"{val}"
else:
Expand Down
8 changes: 2 additions & 6 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1031,12 +1031,8 @@ def astype(self, dtype, copy: bool = True):
new_values = cls._from_sequence(self, dtype=dtype, copy=copy)

else:
if dtype == str:
# GH#38607 see test_astype_str_from_bytes
new_values = values.astype(dtype, copy=copy)
else:
# GH#13149 specifically use astype_nansafe instead of astype
new_values = astype_nansafe(values, dtype=dtype, copy=copy)
# GH#13149 specifically use astype_nansafe instead of astype
new_values = astype_nansafe(values, dtype=dtype, copy=copy)

# pass copy=False because any copying will be done in the astype above
if self._is_backward_compat_public_numeric_index:
Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
pa_version_under9p0,
)
from pandas.errors import PerformanceWarning
import pandas.util._test_decorators as td

import pandas as pd
import pandas._testing as tm
Expand Down Expand Up @@ -234,6 +235,29 @@ def test_astype_str(self, data, request):
)
super().test_astype_str(data)

@pytest.mark.parametrize(
"nullable_string_dtype",
[
"string[python]",
pytest.param(
"string[pyarrow]", marks=td.skip_if_no("pyarrow", min_version="1.0.0")
),
],
)
def test_astype_string(self, data, nullable_string_dtype):
# with binary dtype
pa_dtype = data.dtype.pyarrow_dtype
if pa.types.is_binary(pa_dtype):
# in this case we end up doing val.decode() instead of str(val)
# so get e.g. "a" instead of "b'a'"
result = pd.Series(data[:5]).astype(nullable_string_dtype)
expected = pd.Series(
[x.decode() for x in data[:5]], dtype=nullable_string_dtype
)
self.assert_series_equal(result, expected)
else:
super().test_astype_string(data, nullable_string_dtype)


class TestConstructors(base.BaseConstructorsTests):
def test_from_dtype(self, data, request):
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/indexes/object/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pandas import (
Index,
NaT,
Series,
)
import pandas._testing as tm

Expand All @@ -14,6 +15,12 @@ def test_astype_str_from_bytes():
expected = Index(["あ", "a"], dtype="object")
tm.assert_index_equal(result, expected)

# while we're here, check that Series.astype behaves the same

result = Series(idx).astype(str)
expected = Series(expected)
tm.assert_series_equal(result, expected)


def test_astype_invalid_nas_to_tdt64_raises():
# GH#45722 don't cast np.datetime64 NaTs to timedelta64 NaT
Expand Down
6 changes: 5 additions & 1 deletion pandas/tests/series/methods/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,11 @@ def test_astype_unicode(self):
former_encoding = None

if sys.getdefaultencoding() == "utf-8":
test_series.append(Series(["野菜食べないとやばい".encode()]))
item = "野菜食べないとやばい"
ser = Series([item.encode()])
res = ser.astype("unicode")
expected = Series([item])
tm.assert_series_equal(res, expected)

for ser in test_series:
res = ser.astype("unicode")
Expand Down