Skip to content

Commit 2143c24

Browse files
authored
CLN: rename compat.to_str to core.dtypes.common.ensure_str (#26811)
1 parent 646ff0b commit 2143c24

File tree

6 files changed

+26
-29
lines changed

6 files changed

+26
-29
lines changed

pandas/compat/__init__.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,6 @@
2424
# found at https://bitbucket.org/gutworth/six
2525

2626

27-
def to_str(s):
28-
"""
29-
Convert bytes and non-string into Python 3 str
30-
"""
31-
if isinstance(s, bytes):
32-
s = s.decode('utf-8')
33-
elif not isinstance(s, str):
34-
s = str(s)
35-
return s
36-
37-
3827
def set_function_name(f, name, cls):
3928
"""
4029
Bind the name/qualname attributes of the function

pandas/core/dtypes/cast.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66

77
from pandas._libs import lib, tslib, tslibs
88
from pandas._libs.tslibs import NaT, OutOfBoundsDatetime, Period, iNaT
9-
from pandas.compat import to_str
109

1110
from .common import (
1211
_INT64_DTYPE, _NS_DTYPE, _POSSIBLY_CAST_DTYPES, _TD_DTYPE, ensure_int8,
13-
ensure_int16, ensure_int32, ensure_int64, ensure_object, is_bool,
14-
is_bool_dtype, is_categorical_dtype, is_complex, is_complex_dtype,
12+
ensure_int16, ensure_int32, ensure_int64, ensure_object, ensure_str,
13+
is_bool, is_bool_dtype, is_categorical_dtype, is_complex, is_complex_dtype,
1514
is_datetime64_dtype, is_datetime64_ns_dtype, is_datetime64tz_dtype,
1615
is_datetime_or_timedelta_dtype, is_datetimelike, is_dtype_equal,
1716
is_extension_array_dtype, is_extension_type, is_float, is_float_dtype,
@@ -1189,7 +1188,7 @@ def construct_1d_arraylike_from_scalar(value, length, dtype):
11891188
# to allow numpy to take our string as a scalar value
11901189
dtype = object
11911190
if not isna(value):
1192-
value = to_str(value)
1191+
value = ensure_str(value)
11931192

11941193
subarr = np.empty(length, dtype=dtype)
11951194
subarr.fill(value)

pandas/core/dtypes/common.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
""" common type operations """
2-
from typing import Union
2+
from typing import Any, Union
33
import warnings
44

55
import numpy as np
@@ -69,6 +69,17 @@ def ensure_float(arr):
6969
ensure_object = algos.ensure_object
7070

7171

72+
def ensure_str(value: Union[bytes, Any]) -> str:
73+
"""
74+
Ensure that bytes and non-strings get converted into ``str`` objects.
75+
"""
76+
if isinstance(value, bytes):
77+
value = value.decode('utf-8')
78+
elif not isinstance(value, str):
79+
value = str(value)
80+
return value
81+
82+
7283
def ensure_categorical(arr):
7384
"""
7485
Ensure that an array-like object is a Categorical (if not already).

pandas/core/generic.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from pandas._config import config
1616

1717
from pandas._libs import Timestamp, iNaT, properties
18-
from pandas.compat import set_function_name, to_str
18+
from pandas.compat import set_function_name
1919
from pandas.compat.numpy import function as nv
2020
from pandas.errors import AbstractMethodError
2121
from pandas.util._decorators import (
@@ -24,7 +24,7 @@
2424

2525
from pandas.core.dtypes.cast import maybe_promote, maybe_upcast_putmask
2626
from pandas.core.dtypes.common import (
27-
ensure_int64, ensure_object, is_bool, is_bool_dtype,
27+
ensure_int64, ensure_object, ensure_str, is_bool, is_bool_dtype,
2828
is_datetime64_any_dtype, is_datetime64_dtype, is_datetime64tz_dtype,
2929
is_dict_like, is_extension_array_dtype, is_integer, is_list_like,
3030
is_number, is_numeric_dtype, is_object_dtype, is_period_arraylike,
@@ -4564,12 +4564,12 @@ def filter(self, items=None, like=None, regex=None, axis=None):
45644564
**{name: [r for r in items if r in labels]})
45654565
elif like:
45664566
def f(x):
4567-
return like in to_str(x)
4567+
return like in ensure_str(x)
45684568
values = labels.map(f)
45694569
return self.loc(axis=axis)[values]
45704570
elif regex:
45714571
def f(x):
4572-
return matcher.search(to_str(x)) is not None
4572+
return matcher.search(ensure_str(x)) is not None
45734573
matcher = re.compile(regex)
45744574
values = labels.map(f)
45754575
return self.loc(axis=axis)[values]

pandas/io/json/json.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66

77
import pandas._libs.json as json
88
from pandas._libs.tslibs import iNaT
9-
from pandas.compat import to_str
109
from pandas.errors import AbstractMethodError
1110

12-
from pandas.core.dtypes.common import is_period_dtype
11+
from pandas.core.dtypes.common import ensure_str, is_period_dtype
1312

1413
from pandas import DataFrame, MultiIndex, Series, isna, to_datetime
1514
from pandas.core.reshape.concat import concat
@@ -545,8 +544,7 @@ def read(self):
545544
if self.lines and self.chunksize:
546545
obj = concat(self)
547546
elif self.lines:
548-
549-
data = to_str(self.data)
547+
data = ensure_str(self.data)
550548
obj = self._get_object_parser(
551549
self._combine_lines(data.split('\n'))
552550
)

pandas/io/parsers.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717
import pandas._libs.ops as libops
1818
import pandas._libs.parsers as parsers
1919
from pandas._libs.tslibs import parsing
20-
import pandas.compat as compat
2120
from pandas.errors import (
2221
AbstractMethodError, EmptyDataError, ParserError, ParserWarning)
2322
from pandas.util._decorators import Appender
2423

2524
from pandas.core.dtypes.cast import astype_nansafe
2625
from pandas.core.dtypes.common import (
27-
ensure_object, is_bool_dtype, is_categorical_dtype, is_dtype_equal,
28-
is_extension_array_dtype, is_float, is_integer, is_integer_dtype,
29-
is_list_like, is_object_dtype, is_scalar, is_string_dtype, pandas_dtype)
26+
ensure_object, ensure_str, is_bool_dtype, is_categorical_dtype,
27+
is_dtype_equal, is_extension_array_dtype, is_float, is_integer,
28+
is_integer_dtype, is_list_like, is_object_dtype, is_scalar,
29+
is_string_dtype, pandas_dtype)
3030
from pandas.core.dtypes.dtypes import CategoricalDtype
3131
from pandas.core.dtypes.missing import isna
3232

@@ -1494,7 +1494,7 @@ def extract(r):
14941494
# If we find unnamed columns all in a single
14951495
# level, then our header was too long.
14961496
for n in range(len(columns[0])):
1497-
if all(compat.to_str(c[n]) in self.unnamed_cols for c in columns):
1497+
if all(ensure_str(col[n]) in self.unnamed_cols for col in columns):
14981498
raise ParserError(
14991499
"Passed header=[{header}] are too many rows for this "
15001500
"multi_index of columns"

0 commit comments

Comments
 (0)