From fc9821ca70a54c19f02409cf4aa35846dec00be7 Mon Sep 17 00:00:00 2001 From: Will Ayd Date: Tue, 1 Jan 2019 15:25:16 -0500 Subject: [PATCH 1/2] Cython language level 3 --- pandas/_libs/groupby.pyx | 6 +++--- pandas/_libs/tslib.pyx | 2 +- pandas/_libs/tslibs/ccalendar.pyx | 2 +- pandas/_libs/tslibs/conversion.pyx | 4 ++-- pandas/_libs/tslibs/fields.pyx | 4 ++-- pandas/_libs/tslibs/offsets.pyx | 2 +- pandas/_libs/tslibs/strptime.pyx | 6 +++--- pandas/_libs/tslibs/timestamps.pyx | 2 +- pandas/_libs/writers.pyx | 1 - setup.py | 2 +- 10 files changed, 15 insertions(+), 16 deletions(-) diff --git a/pandas/_libs/groupby.pyx b/pandas/_libs/groupby.pyx index e6036654c71c3..8e77389d715d6 100644 --- a/pandas/_libs/groupby.pyx +++ b/pandas/_libs/groupby.pyx @@ -56,10 +56,10 @@ cdef inline float64_t median_linear(float64_t* a, int n) nogil: n -= na_count if n % 2: - result = kth_smallest_c( a, n / 2, n) + result = kth_smallest_c( a, n // 2, n) else: - result = (kth_smallest_c(a, n / 2, n) + - kth_smallest_c(a, n / 2 - 1, n)) / 2 + result = (kth_smallest_c(a, n // 2, n) + + kth_smallest_c(a, n // 2 - 1, n)) / 2 if na_count: free(a) diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index 798e338d5581b..ac60366b88e1d 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -275,7 +275,7 @@ def format_array_from_datetime(ndarray[int64_t] values, object tz=None, dts.sec) if show_ns: - ns = dts.ps / 1000 + ns = dts.ps // 1000 res += '.%.9d' % (ns + 1000 * dts.us) elif show_us: res += '.%.6d' % dts.us diff --git a/pandas/_libs/tslibs/ccalendar.pyx b/pandas/_libs/tslibs/ccalendar.pyx index c48812acd3de1..9c88ca05ebcf0 100644 --- a/pandas/_libs/tslibs/ccalendar.pyx +++ b/pandas/_libs/tslibs/ccalendar.pyx @@ -159,7 +159,7 @@ cpdef int32_t get_week_of_year(int year, int month, int day) nogil: # estimate woy = (doy - 1) - dow + 3 if woy >= 0: - woy = woy / 7 + 1 + woy = woy // 7 + 1 # verify if woy < 0: diff --git a/pandas/_libs/tslibs/conversion.pyx b/pandas/_libs/tslibs/conversion.pyx index 6e4d79d7b6f9e..b3b8450f670ff 100644 --- a/pandas/_libs/tslibs/conversion.pyx +++ b/pandas/_libs/tslibs/conversion.pyx @@ -455,8 +455,8 @@ cdef _TSObject convert_str_to_tsobject(object ts, object tz, object unit, dt = datetime(obj.dts.year, obj.dts.month, obj.dts.day, obj.dts.hour, obj.dts.min, obj.dts.sec, obj.dts.us, obj.tzinfo) - obj = convert_datetime_to_tsobject(dt, tz, - nanos=obj.dts.ps / 1000) + obj = convert_datetime_to_tsobject( + dt, tz, nanos=obj.dts.ps // 1000) return obj else: diff --git a/pandas/_libs/tslibs/fields.pyx b/pandas/_libs/tslibs/fields.pyx index 5cda7992369fc..fca851eab2f1d 100644 --- a/pandas/_libs/tslibs/fields.pyx +++ b/pandas/_libs/tslibs/fields.pyx @@ -478,7 +478,7 @@ def get_date_field(ndarray[int64_t] dtindex, object field): continue dt64_to_dtstruct(dtindex[i], &dts) - out[i] = dts.ps / 1000 + out[i] = dts.ps // 1000 return out elif field == 'doy': with nogil: @@ -522,7 +522,7 @@ def get_date_field(ndarray[int64_t] dtindex, object field): dt64_to_dtstruct(dtindex[i], &dts) out[i] = dts.month - out[i] = ((out[i] - 1) / 3) + 1 + out[i] = ((out[i] - 1) // 3) + 1 return out elif field == 'dim': diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index 11ce539d25767..f9c9c8b56cf78 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -541,7 +541,7 @@ def shift_day(other: datetime, days: int) -> datetime: cdef inline int year_add_months(npy_datetimestruct dts, int months) nogil: """new year number after shifting npy_datetimestruct number of months""" - return dts.year + (dts.month + months - 1) / 12 + return dts.year + (dts.month + months - 1) // 12 cdef inline int month_add_months(npy_datetimestruct dts, int months) nogil: diff --git a/pandas/_libs/tslibs/strptime.pyx b/pandas/_libs/tslibs/strptime.pyx index 87658ae92175e..f6866f797d576 100644 --- a/pandas/_libs/tslibs/strptime.pyx +++ b/pandas/_libs/tslibs/strptime.pyx @@ -240,7 +240,7 @@ def array_strptime(object[:] values, object fmt, s += "0" * (9 - len(s)) us = long(s) ns = us % 1000 - us = us / 1000 + us = us // 1000 elif parse_code == 11: weekday = locale_time.f_weekday.index(found_dict['A'].lower()) elif parse_code == 12: @@ -662,7 +662,7 @@ cdef parse_timezone_directive(object z): gmtoff_remainder_padding = "0" * pad_number microseconds = int(gmtoff_remainder + gmtoff_remainder_padding) - total_minutes = ((hours * 60) + minutes + (seconds / 60) + - (microseconds / 60000000)) + total_minutes = ((hours * 60) + minutes + (seconds // 60) + + (microseconds // 60000000)) total_minutes = -total_minutes if z.startswith("-") else total_minutes return pytz.FixedOffset(total_minutes) diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index 4761c7ff1f4eb..91e32994d6a7f 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -68,7 +68,7 @@ cdef inline object create_timestamp_from_ts(int64_t value, dts.sec, dts.us, tz) ts_base.value = value ts_base.freq = freq - ts_base.nanosecond = dts.ps / 1000 + ts_base.nanosecond = dts.ps // 1000 return ts_base diff --git a/pandas/_libs/writers.pyx b/pandas/_libs/writers.pyx index 6449a331689ad..8f035d0c205e3 100644 --- a/pandas/_libs/writers.pyx +++ b/pandas/_libs/writers.pyx @@ -16,7 +16,6 @@ from numpy cimport ndarray, uint8_t ctypedef fused pandas_string: str - unicode bytes diff --git a/setup.py b/setup.py index 6cd359b281b56..00dd5b99fed67 100755 --- a/setup.py +++ b/setup.py @@ -464,7 +464,7 @@ def get_tag(self): # Note: if not using `cythonize`, coverage can be enabled by # pinning `ext.cython_directives = directives` to each ext in extensions. # github.com/cython/cython/wiki/enhancements-compilerdirectives#in-setuppy -directives = {'linetrace': False} +directives = {'linetrace': False, 'language_level': 3} macros = [] if linetrace: # https://pypkg.com/pypi/pytest-cython/f/tests/example-project/setup.py From 15fcf0ba6d9e4232029f51856894b721e4cb137e Mon Sep 17 00:00:00 2001 From: Will Ayd Date: Tue, 1 Jan 2019 16:03:46 -0500 Subject: [PATCH 2/2] Test failure fixup --- pandas/_libs/parsers.pyx | 4 ++-- pandas/_libs/tslibs/timedeltas.pyx | 2 +- pandas/io/sas/sas.pyx | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/_libs/parsers.pyx b/pandas/_libs/parsers.pyx index 169aa4ffe72da..b6710d971e089 100644 --- a/pandas/_libs/parsers.pyx +++ b/pandas/_libs/parsers.pyx @@ -938,7 +938,7 @@ cdef class TextReader: status = tokenize_nrows(self.parser, nrows) if self.parser.warn_msg != NULL: - print >> sys.stderr, self.parser.warn_msg + print(self.parser.warn_msg, file=sys.stderr) free(self.parser.warn_msg) self.parser.warn_msg = NULL @@ -966,7 +966,7 @@ cdef class TextReader: status = tokenize_all_rows(self.parser) if self.parser.warn_msg != NULL: - print >> sys.stderr, self.parser.warn_msg + print(self.parser.warn_msg, file=sys.stderr) free(self.parser.warn_msg) self.parser.warn_msg = NULL diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index 037e7de27adc3..6a90d4b53c209 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -577,7 +577,7 @@ def _binary_op_method_timedeltalike(op, name): # the PyDateTime_CheckExact case is for a datetime object that # is specifically *not* a Timestamp, as the Timestamp case will be # handled after `_validate_ops_compat` returns False below - from timestamps import Timestamp + from pandas._libs.tslibs.timestamps import Timestamp return op(self, Timestamp(other)) # We are implicitly requiring the canonical behavior to be # defined by Timestamp methods. diff --git a/pandas/io/sas/sas.pyx b/pandas/io/sas/sas.pyx index a5bfd5866a261..2d9986dba9c41 100644 --- a/pandas/io/sas/sas.pyx +++ b/pandas/io/sas/sas.pyx @@ -2,7 +2,7 @@ # cython: boundscheck=False, initializedcheck=False import numpy as np -import sas_constants as const +import pandas.io.sas.sas_constants as const ctypedef signed long long int64_t ctypedef unsigned char uint8_t