Skip to content

Commit b878f5b

Browse files
mroeschkejreback
authored andcommitted
CLN: Remove PY2/3 checks in cython files (#25876)
1 parent df6b2cd commit b878f5b

File tree

6 files changed

+8
-67
lines changed

6 files changed

+8
-67
lines changed

pandas/_libs/lib.pyx

+3-5
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ cdef:
7373
object oINT64_MIN = <int64_t>INT64_MIN
7474
object oUINT64_MAX = <uint64_t>UINT64_MAX
7575

76-
bint PY2 = sys.version_info[0] == 2
7776
float64_t NaN = <float64_t>np.NaN
7877

7978

@@ -942,10 +941,9 @@ _TYPE_MAP = {
942941
'complex64': 'complex',
943942
'complex128': 'complex',
944943
'c': 'complex',
945-
'string': 'string' if PY2 else 'bytes',
946-
'S': 'string' if PY2 else 'bytes',
947-
'unicode': 'unicode' if PY2 else 'string',
948-
'U': 'unicode' if PY2 else 'string',
944+
'string': 'bytes',
945+
'S': 'bytes',
946+
'U': 'string',
949947
'bool': 'boolean',
950948
'b': 'boolean',
951949
'datetime64[ns]': 'datetime64',

pandas/_libs/parsers.pyx

+5-30
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ CParserError = ParserError
6767

6868

6969
cdef:
70-
bint PY3 = (sys.version_info[0] >= 3)
71-
7270
float64_t INF = <float64_t>np.inf
7371
float64_t NEGINF = -INF
7472

@@ -633,12 +631,7 @@ cdef class TextReader:
633631
source = gzip.GzipFile(fileobj=source)
634632
elif self.compression == 'bz2':
635633
import bz2
636-
if isinstance(source, basestring) or PY3:
637-
source = bz2.BZ2File(source, 'rb')
638-
else:
639-
content = source.read()
640-
source.close()
641-
source = compat.StringIO(bz2.decompress(content))
634+
source = bz2.BZ2File(source, 'rb')
642635
elif self.compression == 'zip':
643636
import zipfile
644637
zip_file = zipfile.ZipFile(source)
@@ -1396,19 +1389,12 @@ def _ensure_encoded(list lst):
13961389
if isinstance(x, unicode):
13971390
x = PyUnicode_AsUTF8String(x)
13981391
elif not isinstance(x, bytes):
1399-
x = asbytes(x)
1392+
x = str(x).encode('utf-8')
14001393

14011394
result.append(x)
14021395
return result
14031396

14041397

1405-
cdef asbytes(object o):
1406-
if PY3:
1407-
return str(o).encode('utf-8')
1408-
else:
1409-
return str(o)
1410-
1411-
14121398
# common NA values
14131399
# no longer excluding inf representations
14141400
# '1.#INF','-1.#INF', '1.#INF000000',
@@ -1441,10 +1427,7 @@ cdef enum StringPath:
14411427
cdef inline StringPath _string_path(char *encoding):
14421428
if encoding != NULL and encoding != b"utf-8":
14431429
return ENCODED
1444-
elif PY3 or encoding != NULL:
1445-
return UTF8
1446-
else:
1447-
return CSTRING
1430+
return UTF8
14481431

14491432

14501433
# ----------------------------------------------------------------------
@@ -2155,10 +2138,7 @@ cdef raise_parser_error(object base, parser_t *parser):
21552138

21562139
message = '{base}. C error: '.format(base=base)
21572140
if parser.error_msg != NULL:
2158-
if PY3:
2159-
message += parser.error_msg.decode('utf-8')
2160-
else:
2161-
message += parser.error_msg
2141+
message += parser.error_msg.decode('utf-8')
21622142
else:
21632143
message += 'no error message set'
21642144

@@ -2257,12 +2237,7 @@ cdef _apply_converter(object f, parser_t *parser, int64_t col,
22572237

22582238
coliter_setup(&it, parser, col, line_start)
22592239

2260-
if not PY3 and c_encoding == NULL:
2261-
for i in range(lines):
2262-
COLITER_NEXT(it, word)
2263-
val = PyBytes_FromString(word)
2264-
result[i] = f(val)
2265-
elif ((PY3 and c_encoding == NULL) or c_encoding == b'utf-8'):
2240+
if c_encoding == NULL or c_encoding == b'utf-8':
22662241
for i in range(lines):
22672242
COLITER_NEXT(it, word)
22682243
val = PyUnicode_FromString(word)

pandas/_libs/tslib.pyx

-5
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ from pandas._libs.tslibs.timestamps cimport create_timestamp_from_ts
4545
from pandas._libs.tslibs.timestamps import Timestamp
4646

4747

48-
cdef bint PY2 = str == bytes
49-
50-
5148
cdef inline object create_datetime_from_ts(
5249
int64_t value, npy_datetimestruct dts,
5350
object tz, object freq):
@@ -579,8 +576,6 @@ cpdef array_to_datetime(ndarray[object] values, str errors='raise',
579576
if len(val) == 0 or val in nat_strings:
580577
iresult[i] = NPY_NAT
581578
continue
582-
if isinstance(val, unicode) and PY2:
583-
val = val.encode('utf-8')
584579

585580
try:
586581
_string_to_dts(val, &dts, &out_local, &out_tzoffset)

pandas/_libs/tslibs/offsets.pyx

-6
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ from pandas._libs.tslibs.np_datetime cimport (
3131
from pandas._libs.tslibs.timezones import UTC
3232

3333

34-
PY2 = bytes == str
35-
3634
# ---------------------------------------------------------------------
3735
# Constants
3836

@@ -552,10 +550,6 @@ class _Tick(object):
552550
result = self.delta.__rtruediv__(other)
553551
return _wrap_timedelta_result(result)
554552

555-
if PY2:
556-
__div__ = __truediv__
557-
__rdiv__ = __rtruediv__
558-
559553

560554
# ----------------------------------------------------------------------
561555
# RelativeDelta Arithmetic

pandas/_libs/tslibs/period.pyx

-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ from pandas._libs.tslibs.offsets cimport to_offset
5353
from pandas._libs.tslibs.offsets import _Tick
5454

5555
cdef:
56-
bint PY2 = str == bytes
5756
enum:
5857
INT32_MIN = -2147483648
5958

@@ -1287,9 +1286,6 @@ cdef object _period_strftime(int64_t value, int freq, object fmt):
12871286

12881287
result = result.replace(str_extra_fmts[i], repl)
12891288

1290-
if PY2:
1291-
result = result.decode('utf-8', 'ignore')
1292-
12931289
return result
12941290

12951291

pandas/_libs/tslibs/timedeltas.pyx

-17
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import textwrap
44
import warnings
55

66
import sys
7-
cdef bint PY3 = (sys.version_info[0] >= 3)
87

98
import cython
109

@@ -312,14 +311,6 @@ cdef inline int64_t cast_from_unit(object ts, object unit) except? -1:
312311
return <int64_t>(base * m) + <int64_t>(frac * m)
313312

314313

315-
cdef inline _decode_if_necessary(object ts):
316-
# decode ts if necessary
317-
if not isinstance(ts, unicode) and not PY3:
318-
ts = str(ts).decode('utf-8')
319-
320-
return ts
321-
322-
323314
cdef inline parse_timedelta_string(object ts):
324315
"""
325316
Parse a regular format timedelta string. Return an int64_t (in ns)
@@ -342,8 +333,6 @@ cdef inline parse_timedelta_string(object ts):
342333
if len(ts) == 0 or ts in nat_strings:
343334
return NPY_NAT
344335

345-
ts = _decode_if_necessary(ts)
346-
347336
for c in ts:
348337

349338
# skip whitespace / commas
@@ -651,8 +640,6 @@ cdef inline int64_t parse_iso_format_string(object ts) except? -1:
651640
bint have_dot = 0, have_value = 0, neg = 0
652641
list number = [], unit = []
653642

654-
ts = _decode_if_necessary(ts)
655-
656643
err_msg = "Invalid ISO 8601 Duration format - {}".format(ts)
657644

658645
for c in ts:
@@ -1389,10 +1376,6 @@ class Timedelta(_Timedelta):
13891376
return NaT
13901377
return float(other.value) / self.value
13911378

1392-
if not PY3:
1393-
__div__ = __truediv__
1394-
__rdiv__ = __rtruediv__
1395-
13961379
def __floordiv__(self, other):
13971380
# numpy does not implement floordiv for timedelta64 dtype, so we cannot
13981381
# just defer

0 commit comments

Comments
 (0)