Skip to content

Commit 19626d2

Browse files
jschendeljreback
authored andcommitted
CLN: Remove type definitions from pandas.compat (#25903)
1 parent d2d9e2d commit 19626d2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+386
-536
lines changed

pandas/_libs/parsers.pyx

+2-3
Original file line numberDiff line numberDiff line change
@@ -592,8 +592,7 @@ cdef class TextReader:
592592
if not QUOTE_MINIMAL <= quoting <= QUOTE_NONE:
593593
raise TypeError('bad "quoting" value')
594594

595-
if not isinstance(quote_char, (str, compat.text_type,
596-
bytes)) and quote_char is not None:
595+
if not isinstance(quote_char, (str, bytes)) and quote_char is not None:
597596
dtype = type(quote_char).__name__
598597
raise TypeError('"quotechar" must be string, '
599598
'not {dtype}'.format(dtype=dtype))
@@ -2123,7 +2122,7 @@ cdef raise_parser_error(object base, parser_t *parser):
21232122

21242123
# PyErr_Fetch only returned the error message in *value,
21252124
# so the Exception class must be extracted from *type.
2126-
if isinstance(old_exc, compat.string_types):
2125+
if isinstance(old_exc, str):
21272126
if type != NULL:
21282127
exc_type = <object>type
21292128
else:

pandas/_libs/testing.pyx

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import numpy as np
22

3-
from pandas import compat
43
from pandas.core.dtypes.missing import isna, array_equivalent
54
from pandas.core.dtypes.common import is_dtype_equal
65

@@ -108,8 +107,7 @@ cpdef assert_almost_equal(a, b,
108107
if isinstance(a, dict) or isinstance(b, dict):
109108
return assert_dict_equal(a, b)
110109

111-
if (isinstance(a, compat.string_types) or
112-
isinstance(b, compat.string_types)):
110+
if isinstance(a, str) or isinstance(b, str):
113111
assert a == b, "%r != %r" % (a, b)
114112
return True
115113

pandas/_libs/tslibs/parsing.pyx

+3-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ from cpython.datetime cimport datetime
1212
import numpy as np
1313

1414
import six
15-
from six import binary_type, text_type
1615

1716
# Avoid import from outside _libs
1817
if sys.version_info.major == 2:
@@ -102,7 +101,7 @@ def parse_time_string(arg, freq=None, dayfirst=None, yearfirst=None):
102101
103102
Parameters
104103
----------
105-
arg : compat.string_types
104+
arg : str
106105
freq : str or DateOffset, default None
107106
Helps with interpreting time string if supplied
108107
dayfirst : bool, default None
@@ -537,13 +536,13 @@ class _timelex(object):
537536
if six.PY2:
538537
# In Python 2, we can't duck type properly because unicode has
539538
# a 'decode' function, and we'd be double-decoding
540-
if isinstance(instream, (binary_type, bytearray)):
539+
if isinstance(instream, (bytes, bytearray)):
541540
instream = instream.decode()
542541
else:
543542
if getattr(instream, 'decode', None) is not None:
544543
instream = instream.decode()
545544

546-
if isinstance(instream, text_type):
545+
if isinstance(instream, str):
547546
self.stream = instream
548547
elif getattr(instream, 'read', None) is None:
549548
raise TypeError(

pandas/_libs/tslibs/period.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -2390,7 +2390,7 @@ class Period(_Period):
23902390
23912391
Parameters
23922392
----------
2393-
value : Period or compat.string_types, default None
2393+
value : Period or str, default None
23942394
The time period represented (e.g., '4Q2005')
23952395
freq : str, default None
23962396
One of pandas period strings or corresponding objects

pandas/compat/__init__.py

+3-55
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88
* lists: lrange(), lmap(), lzip(), lfilter()
99
* iterable method compatibility: iteritems, iterkeys, itervalues
1010
* Uses the original method if available, otherwise uses items, keys, values.
11-
* types:
12-
* text_type: unicode in Python 2, str in Python 3
13-
* binary_type: str in Python 2, bytes in Python 3
14-
* string_types: basestring in Python 2, str in Python 3
1511
* bind_method: binds functions to classes
1612
* add_metaclass(metaclass) - class decorator that recreates class with with the
1713
given metaclass instead (and avoids intermediary class creation)
@@ -30,7 +26,6 @@
3026
import sys
3127
import platform
3228
import types
33-
from unicodedata import east_asian_width
3429
import struct
3530
import inspect
3631
from collections import namedtuple
@@ -203,39 +198,18 @@ class to receive bound method
203198
# The license for this library can be found in LICENSES/SIX and the code can be
204199
# found at https://bitbucket.org/gutworth/six
205200

206-
# Definition of East Asian Width
207-
# http://unicode.org/reports/tr11/
208-
# Ambiguous width can be changed by option
209-
_EAW_MAP = {'Na': 1, 'N': 1, 'W': 2, 'F': 2, 'H': 1}
210201

211202
if PY3:
212-
string_types = str,
213-
text_type = str
214-
binary_type = bytes
215-
216203
def to_str(s):
217204
"""
218205
Convert bytes and non-string into Python 3 str
219206
"""
220-
if isinstance(s, binary_type):
207+
if isinstance(s, bytes):
221208
s = bytes_to_str(s)
222-
elif not isinstance(s, string_types):
209+
elif not isinstance(s, str):
223210
s = str(s)
224211
return s
225212

226-
def strlen(data, encoding=None):
227-
# encoding is for compat with PY2
228-
return len(data)
229-
230-
def east_asian_len(data, encoding=None, ambiguous_width=1):
231-
"""
232-
Calculate display width considering unicode East Asian Width
233-
"""
234-
if isinstance(data, text_type):
235-
return sum(_EAW_MAP.get(east_asian_width(c), ambiguous_width) for c in data)
236-
else:
237-
return len(data)
238-
239213
def set_function_name(f, name, cls):
240214
""" Bind the name/qualname attributes of the function """
241215
f.__name__ = name
@@ -245,45 +219,19 @@ def set_function_name(f, name, cls):
245219
f.__module__ = cls.__module__
246220
return f
247221
else:
248-
string_types = basestring,
249-
text_type = unicode
250-
binary_type = str
251-
252222
def to_str(s):
253223
"""
254224
Convert unicode and non-string into Python 2 str
255225
"""
256-
if not isinstance(s, string_types):
226+
if not isinstance(s, basestring):
257227
s = str(s)
258228
return s
259229

260-
def strlen(data, encoding=None):
261-
try:
262-
data = data.decode(encoding)
263-
except UnicodeError:
264-
pass
265-
return len(data)
266-
267-
def east_asian_len(data, encoding=None, ambiguous_width=1):
268-
"""
269-
Calculate display width considering unicode East Asian Width
270-
"""
271-
if isinstance(data, text_type):
272-
try:
273-
data = data.decode(encoding)
274-
except UnicodeError:
275-
pass
276-
return sum(_EAW_MAP.get(east_asian_width(c), ambiguous_width) for c in data)
277-
else:
278-
return len(data)
279-
280230
def set_function_name(f, name, cls):
281231
""" Bind the name attributes of the function """
282232
f.__name__ = name
283233
return f
284234

285-
string_and_binary_types = string_types + (binary_type,)
286-
287235

288236
def add_metaclass(metaclass):
289237
"""Class decorator for creating a class with a metaclass."""

pandas/compat/numpy/__init__.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import re
44
import numpy as np
55
from distutils.version import LooseVersion
6-
from pandas.compat import string_types, string_and_binary_types
76

87

98
# numpy versioning
@@ -27,7 +26,7 @@
2726

2827

2928
def tz_replacer(s):
30-
if isinstance(s, string_types):
29+
if isinstance(s, str):
3130
if s.endswith('Z'):
3231
s = s[:-1]
3332
elif _tz_regex.search(s):
@@ -53,8 +52,7 @@ def np_array_datetime64_compat(arr, *args, **kwargs):
5352
warning, when need to pass '2015-01-01 09:00:00'
5453
"""
5554
# is_list_like
56-
if (hasattr(arr, '__iter__')
57-
and not isinstance(arr, string_and_binary_types)):
55+
if (hasattr(arr, '__iter__') and not isinstance(arr, (str, bytes))):
5856
arr = [tz_replacer(s) for s in arr]
5957
else:
6058
arr = tz_replacer(arr)

pandas/compat/pickle_compat.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import pickle as pkl
77
import sys
88

9-
from pandas.compat import string_types # noqa
10-
119
import pandas # noqa
1210
from pandas import Index, compat
1311

@@ -41,7 +39,7 @@ def load_reduce(self):
4139
# try to re-encode the arguments
4240
if getattr(self, 'encoding', None) is not None:
4341
args = tuple(arg.encode(self.encoding)
44-
if isinstance(arg, string_types)
42+
if isinstance(arg, str)
4543
else arg for arg in args)
4644
try:
4745
stack[-1] = func(*args)

pandas/core/apply.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ def __init__(self, obj, func, broadcast, raw, reduce, result_type,
7171
self.result_type = result_type
7272

7373
# curry if needed
74-
if ((kwds or args) and
75-
not isinstance(func, (np.ufunc, compat.string_types))):
74+
if (kwds or args) and not isinstance(func, (np.ufunc, str)):
7675

7776
def f(x):
7877
return func(x, *args, **kwds)
@@ -119,7 +118,7 @@ def get_result(self):
119118
return self.apply_empty_result()
120119

121120
# string dispatch
122-
if isinstance(self.f, compat.string_types):
121+
if isinstance(self.f, str):
123122
# Support for `frame.transform('method')`
124123
# Some methods (shift, etc.) require the axis argument, others
125124
# don't, so inspect and insert if necessary.

pandas/core/arrays/array_.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
is_datetime64_ns_dtype, is_extension_array_dtype, is_timedelta64_ns_dtype)
99
from pandas.core.dtypes.dtypes import ExtensionDtype, registry
1010

11-
from pandas import compat
12-
1311

1412
def array(data, # type: Sequence[object]
1513
dtype=None, # type: Optional[Union[str, np.dtype, ExtensionDtype]]
@@ -227,7 +225,7 @@ def array(data, # type: Sequence[object]
227225
dtype = data.dtype
228226

229227
# this returns None for not-found dtypes.
230-
if isinstance(dtype, compat.string_types):
228+
if isinstance(dtype, str):
231229
dtype = registry.find(dtype) or dtype
232230

233231
if is_extension_array_dtype(dtype):

pandas/core/arrays/categorical.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1946,7 +1946,7 @@ def _tidy_repr(self, max_vals=10, footer=True):
19461946
result = '{result}\n{footer}'.format(
19471947
result=result, footer=self._repr_footer())
19481948

1949-
return compat.text_type(result)
1949+
return str(result)
19501950

19511951
def _repr_categories(self):
19521952
"""
@@ -2010,7 +2010,7 @@ def _get_repr(self, length=True, na_rep='NaN', footer=True):
20102010
formatter = fmt.CategoricalFormatter(self, length=length,
20112011
na_rep=na_rep, footer=footer)
20122012
result = formatter.to_string()
2013-
return compat.text_type(result)
2013+
return str(result)
20142014

20152015
def __unicode__(self):
20162016
"""

pandas/core/arrays/datetimelike.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from pandas._libs.tslibs.timedeltas import Timedelta, delta_to_nanoseconds
1313
from pandas._libs.tslibs.timestamps import (
1414
RoundTo, maybe_integer_op_deprecated, round_nsint64)
15-
import pandas.compat as compat
1615
from pandas.compat.numpy import function as nv
1716
from pandas.errors import (
1817
AbstractMethodError, NullFrequencyError, PerformanceWarning)
@@ -649,7 +648,7 @@ def searchsorted(self, value, side='left', sorter=None):
649648
indices : array of ints
650649
Array of insertion points with the same shape as `value`.
651650
"""
652-
if isinstance(value, compat.string_types):
651+
if isinstance(value, str):
653652
value = self._scalar_from_string(value)
654653

655654
if not (isinstance(value, (self._scalar_type, type(self)))
@@ -1154,7 +1153,7 @@ def _time_shift(self, periods, freq=None):
11541153
Frequency increment to shift by.
11551154
"""
11561155
if freq is not None and freq != self.freq:
1157-
if isinstance(freq, compat.string_types):
1156+
if isinstance(freq, str):
11581157
freq = frequencies.to_offset(freq)
11591158
offset = periods * freq
11601159
result = self + offset

pandas/core/arrays/datetimes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def wrapper(self, other):
137137

138138
other = lib.item_from_zerodim(other)
139139

140-
if isinstance(other, (datetime, np.datetime64, compat.string_types)):
140+
if isinstance(other, (datetime, np.datetime64, str)):
141141
if isinstance(other, (datetime, np.datetime64)):
142142
# GH#18435 strings get a pass from tzawareness compat
143143
self._assert_tzawareness_compat(other)
@@ -2031,7 +2031,7 @@ def validate_tz_from_dtype(dtype, tz):
20312031
ValueError : on tzinfo mismatch
20322032
"""
20332033
if dtype is not None:
2034-
if isinstance(dtype, compat.string_types):
2034+
if isinstance(dtype, str):
20352035
try:
20362036
dtype = DatetimeTZDtype.construct_from_string(dtype)
20372037
except TypeError:

pandas/core/arrays/integer.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import numpy as np
66

77
from pandas._libs import lib
8-
from pandas.compat import set_function_name, string_types
8+
from pandas.compat import set_function_name
99
from pandas.util._decorators import cache_readonly
1010

1111
from pandas.core.dtypes.base import ExtensionDtype
@@ -154,7 +154,7 @@ def coerce_to_array(values, dtype, mask=None, copy=False):
154154
dtype = values.dtype
155155

156156
if dtype is not None:
157-
if (isinstance(dtype, string_types) and
157+
if (isinstance(dtype, str) and
158158
(dtype.startswith("Int") or dtype.startswith("UInt"))):
159159
# Avoid DeprecationWarning from NumPy about np.dtype("Int64")
160160
# https://github.com/numpy/numpy/pull/7476

pandas/core/arrays/sparse.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def __hash__(self):
110110
def __eq__(self, other):
111111
# We have to override __eq__ to handle NA values in _metadata.
112112
# The base class does simple == checks, which fail for NA.
113-
if isinstance(other, compat.string_types):
113+
if isinstance(other, str):
114114
try:
115115
other = self.construct_from_string(other)
116116
except TypeError:
@@ -277,7 +277,7 @@ def _parse_subtype(dtype):
277277
@classmethod
278278
def is_dtype(cls, dtype):
279279
dtype = getattr(dtype, 'dtype', dtype)
280-
if (isinstance(dtype, compat.string_types) and
280+
if (isinstance(dtype, str) and
281281
dtype.startswith("Sparse")):
282282
sub_type, _ = cls._parse_subtype(dtype)
283283
dtype = np.dtype(sub_type)
@@ -358,7 +358,7 @@ def _subtype_with_str(self):
358358
>>> dtype._subtype_with_str
359359
str
360360
"""
361-
if isinstance(self.fill_value, compat.string_types):
361+
if isinstance(self.fill_value, str):
362362
return type(self.fill_value)
363363
return self.subtype
364364

@@ -584,7 +584,7 @@ def __init__(self, data, sparse_index=None, index=None, fill_value=None,
584584
data = data.sp_values
585585

586586
# Handle use-provided dtype
587-
if isinstance(dtype, compat.string_types):
587+
if isinstance(dtype, str):
588588
# Two options: dtype='int', regular numpy dtype
589589
# or dtype='Sparse[int]', a sparse dtype
590590
try:

pandas/core/arrays/timedeltas.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636

3737

3838
def _is_convertible_to_td(key):
39-
return isinstance(key, (Tick, timedelta,
40-
np.timedelta64, compat.string_types))
39+
return isinstance(key, (Tick, timedelta, np.timedelta64, str))
4140

4241

4342
def _field_accessor(name, alias, docstring=None):

0 commit comments

Comments
 (0)