diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a83de7309c116..09b56b3541388 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -44,6 +44,7 @@ repos: - flake8-bugbear==21.3.2 - pandas-dev-flaker==0.2.0 - id: flake8 + alias: flake8-cython name: flake8 (cython) types: [cython] args: [--append-config=flake8/cython.cfg] diff --git a/flake8/cython.cfg b/flake8/cython.cfg index 2dfe47b60b4c1..a9584ad2e0994 100644 --- a/flake8/cython.cfg +++ b/flake8/cython.cfg @@ -1,3 +1,17 @@ [flake8] filename = *.pyx,*.pxd -select=E501,E302,E203,E111,E114,E221,E303,E128,E231,E126,E265,E305,E301,E127,E261,E271,E129,W291,E222,E241,E123,F403,C400,C401,C402,C403,C404,C405,C406,C407,C408,C409,C410,C411 +extend_ignore= + # whitespace before '(' + E211, + # missing whitespace around operator + E225, + # missing whitespace around arithmetic operator + E226, + # missing whitespace around bitwise or shift operator + E227, + # ambiguous variable name (# FIXME maybe this one can be fixed) + E741, + # invalid syntax + E999, + # invalid escape sequence (# FIXME maybe this one can be fixed) + W605, diff --git a/pandas/_libs/algos.pyx b/pandas/_libs/algos.pyx index 6c5388a38c345..2353c66f3378f 100644 --- a/pandas/_libs/algos.pyx +++ b/pandas/_libs/algos.pyx @@ -274,16 +274,22 @@ cdef inline numeric kth_smallest_c(numeric* arr, Py_ssize_t k, Py_ssize_t n) nog j = m while 1: - while arr[i] < x: i += 1 - while x < arr[j]: j -= 1 + while arr[i] < x: + i += 1 + while x < arr[j]: + j -= 1 if i <= j: swap(&arr[i], &arr[j]) - i += 1; j -= 1 + i += 1 + j -= 1 - if i > j: break + if i > j: + break - if j < k: l = i - if k < i: m = j + if j < k: + l = i + if k < i: + m = j return arr[k] diff --git a/pandas/_libs/khash.pxd b/pandas/_libs/khash.pxd index b9c18d6c86039..a9f819e5e16db 100644 --- a/pandas/_libs/khash.pxd +++ b/pandas/_libs/khash.pxd @@ -26,20 +26,20 @@ cdef extern from "khash_python.h": double imag bint are_equivalent_khcomplex128_t \ - "kh_complex_hash_equal" (khcomplex128_t a, khcomplex128_t b) nogil + "kh_complex_hash_equal" (khcomplex128_t a, khcomplex128_t b) nogil ctypedef struct khcomplex64_t: float real float imag bint are_equivalent_khcomplex64_t \ - "kh_complex_hash_equal" (khcomplex64_t a, khcomplex64_t b) nogil + "kh_complex_hash_equal" (khcomplex64_t a, khcomplex64_t b) nogil bint are_equivalent_float64_t \ - "kh_floats_hash_equal" (float64_t a, float64_t b) nogil + "kh_floats_hash_equal" (float64_t a, float64_t b) nogil bint are_equivalent_float32_t \ - "kh_floats_hash_equal" (float32_t a, float32_t b) nogil + "kh_floats_hash_equal" (float32_t a, float32_t b) nogil uint32_t kh_python_hash_func(object key) bint kh_python_hash_equal(object a, object b) diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 4ab2497be94d5..76955a7e27679 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -2107,9 +2107,9 @@ cpdef bint is_interval_array(ndarray values): return False elif numeric: if not ( - util.is_float_object(val.left) - or util.is_integer_object(val.left) - ): + util.is_float_object(val.left) + or util.is_integer_object(val.left) + ): # i.e. datetime64 or timedelta64 return False elif td64: diff --git a/pandas/_libs/parsers.pyx b/pandas/_libs/parsers.pyx index 3655d6efad66e..25028b06f7bad 100644 --- a/pandas/_libs/parsers.pyx +++ b/pandas/_libs/parsers.pyx @@ -356,7 +356,7 @@ cdef class TextReader: thousands=None, # bytes | str dtype=None, usecols=None, - on_bad_lines = ERROR, + on_bad_lines=ERROR, bint na_filter=True, na_values=None, na_fvalues=None, @@ -1442,7 +1442,7 @@ cdef _categorical_convert(parser_t *parser, int64_t col, if na_filter: if kh_get_str_starts_item(na_hashset, word): - # is in NA values + # is in NA values na_count += 1 codes[i] = NA continue @@ -1578,7 +1578,7 @@ cdef inline int _try_double_nogil(parser_t *parser, strcasecmp(word, cposinfty) == 0): data[0] = INF elif (strcasecmp(word, cneginf) == 0 or - strcasecmp(word, cneginfty) == 0 ): + strcasecmp(word, cneginfty) == 0): data[0] = NEGINF else: return 1 diff --git a/pandas/_libs/tslibs/conversion.pyx b/pandas/_libs/tslibs/conversion.pyx index 865185f9acea7..fe9fa4169c547 100644 --- a/pandas/_libs/tslibs/conversion.pyx +++ b/pandas/_libs/tslibs/conversion.pyx @@ -200,7 +200,7 @@ cdef inline int64_t get_datetime64_nanos(object val) except? -1: @cython.boundscheck(False) @cython.wraparound(False) -def ensure_datetime64ns(arr: ndarray, copy: bool=True): +def ensure_datetime64ns(arr: ndarray, copy: bool = True): """ Ensure a np.datetime64 array has dtype specifically 'datetime64[ns]' @@ -260,7 +260,7 @@ def ensure_datetime64ns(arr: ndarray, copy: bool=True): return result -def ensure_timedelta64ns(arr: ndarray, copy: bool=True): +def ensure_timedelta64ns(arr: ndarray, copy: bool = True): """ Ensure a np.timedelta64 array has dtype specifically 'timedelta64[ns]' diff --git a/pandas/_libs/tslibs/np_datetime.pyx b/pandas/_libs/tslibs/np_datetime.pyx index 418730277ed6b..bc03a3e7f885e 100644 --- a/pandas/_libs/tslibs/np_datetime.pyx +++ b/pandas/_libs/tslibs/np_datetime.pyx @@ -38,7 +38,7 @@ cdef extern from "src/datetime/np_datetime.h": void pandas_timedelta_to_timedeltastruct(npy_timedelta val, NPY_DATETIMEUNIT fr, pandas_timedeltastruct *result - ) nogil + ) nogil npy_datetimestruct _NS_MIN_DTS, _NS_MAX_DTS diff --git a/pandas/_libs/tslibs/offsets.pyx b/pandas/_libs/tslibs/offsets.pyx index 0faf5fb0a741a..bb9d199023e9a 100644 --- a/pandas/_libs/tslibs/offsets.pyx +++ b/pandas/_libs/tslibs/offsets.pyx @@ -1454,7 +1454,7 @@ cdef class BusinessHour(BusinessMixin): def __init__( self, n=1, normalize=False, start="09:00", end="17:00", offset=timedelta(0) - ): + ): BusinessMixin.__init__(self, n, normalize, offset) # must be validated here to equality check @@ -3897,7 +3897,7 @@ cdef ndarray[int64_t] _shift_bdays(const int64_t[:] i8other, int periods): return result.base -def shift_month(stamp: datetime, months: int, day_opt: object=None) -> datetime: +def shift_month(stamp: datetime, months: int, day_opt: object = None) -> datetime: """ Given a datetime (or Timestamp) `stamp`, an integer `months` and an option `day_opt`, return a new datetimelike that many months later, diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index 0bb431bc8e1cd..c53c8635c10e9 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -197,7 +197,7 @@ cdef freq_conv_func get_asfreq_func(int from_freq, int to_freq) nogil: return asfreq_BtoW elif to_group == FR_BUS: return no_op - elif to_group in [FR_DAY, FR_HR, FR_MIN, FR_SEC, FR_MS, FR_US, FR_NS]: + elif to_group in [FR_DAY, FR_HR, FR_MIN, FR_SEC, FR_MS, FR_US, FR_NS]: return asfreq_BtoDT else: return nofunc diff --git a/pandas/_libs/tslibs/strptime.pyx b/pandas/_libs/tslibs/strptime.pyx index ffa29b44a366a..e7fb38db2aa17 100644 --- a/pandas/_libs/tslibs/strptime.pyx +++ b/pandas/_libs/tslibs/strptime.pyx @@ -199,17 +199,17 @@ def array_strptime(ndarray[object] values, object fmt, bint exact=True, errors=' year = int(found_dict['Y']) elif parse_code == 2: month = int(found_dict['m']) - elif parse_code == 3: # elif group_key == 'B': + elif parse_code == 3: month = locale_time.f_month.index(found_dict['B'].lower()) - elif parse_code == 4: # elif group_key == 'b': + elif parse_code == 4: month = locale_time.a_month.index(found_dict['b'].lower()) - elif parse_code == 5: # elif group_key == 'd': + elif parse_code == 5: day = int(found_dict['d']) - elif parse_code == 6: # elif group_key == 'H': + elif parse_code == 6: hour = int(found_dict['H']) elif parse_code == 7: hour = int(found_dict['I']) diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index f536c8dd76f0d..59b62849484a6 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -641,7 +641,8 @@ def _binary_op_method_timedeltalike(op, name): return NaT elif is_datetime64_object(other) or ( - PyDateTime_Check(other) and not isinstance(other, ABCTimestamp)): + PyDateTime_Check(other) and not isinstance(other, ABCTimestamp) + ): # this 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 diff --git a/pandas/_libs/tslibs/timestamps.pyx b/pandas/_libs/tslibs/timestamps.pyx index fa86b7d9899af..7ed0f110f36db 100644 --- a/pandas/_libs/tslibs/timestamps.pyx +++ b/pandas/_libs/tslibs/timestamps.pyx @@ -1958,7 +1958,7 @@ default 'raise' self.second / 3600.0 + self.microsecond / 3600.0 / 1e+6 + self.nanosecond / 3600.0 / 1e+9 - ) / 24.0) + ) / 24.0) # Aliases