diff --git a/pandas/io/sas/saslib.pyx b/pandas/io/sas/saslib.pyx index a66d62ea41581..4396180da44cb 100644 --- a/pandas/io/sas/saslib.pyx +++ b/pandas/io/sas/saslib.pyx @@ -311,7 +311,7 @@ cdef class Parser(object): self.current_page_subheaders_count =\ self.parser._current_page_subheaders_count - cdef bint readline(self): + cdef readline(self): cdef: int offset, bit_offset, align_correction diff --git a/pandas/lib.pyx b/pandas/lib.pyx index fce6a3d03287e..b4724bc3dd59b 100644 --- a/pandas/lib.pyx +++ b/pandas/lib.pyx @@ -3,6 +3,7 @@ cimport numpy as np cimport cython import numpy as np import sys +cdef bint PY3 = (sys.version_info[0] >= 3) from numpy cimport * @@ -42,7 +43,6 @@ cdef extern from "Python.h": Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength) except -1 - cimport cpython isnan = np.isnan @@ -1568,62 +1568,65 @@ def get_blkno_indexers(int64_t[:] blknos, bint group=True): int64_t cur_blkno Py_ssize_t i, start, stop, n, diff + object blkno list group_order dict group_slices int64_t[:] res_view n = blknos.shape[0] - if n > 0: - start = 0 - cur_blkno = blknos[start] + if n == 0: + return - if group == False: - for i in range(1, n): - if blknos[i] != cur_blkno: - yield cur_blkno, slice(start, i) + start = 0 + cur_blkno = blknos[start] - start = i - cur_blkno = blknos[i] + if group == False: + for i in range(1, n): + if blknos[i] != cur_blkno: + yield cur_blkno, slice(start, i) - yield cur_blkno, slice(start, n) - else: - group_order = [] - group_dict = {} - - for i in range(1, n): - if blknos[i] != cur_blkno: - if cur_blkno not in group_dict: - group_order.append(cur_blkno) - group_dict[cur_blkno] = [(start, i)] - else: - group_dict[cur_blkno].append((start, i)) - - start = i - cur_blkno = blknos[i] - - if cur_blkno not in group_dict: - group_order.append(cur_blkno) - group_dict[cur_blkno] = [(start, n)] - else: - group_dict[cur_blkno].append((start, n)) + start = i + cur_blkno = blknos[i] - for blkno in group_order: - slices = group_dict[blkno] - if len(slices) == 1: - yield blkno, slice(slices[0][0], slices[0][1]) + yield cur_blkno, slice(start, n) + else: + group_order = [] + group_dict = {} + + for i in range(1, n): + if blknos[i] != cur_blkno: + if cur_blkno not in group_dict: + group_order.append(cur_blkno) + group_dict[cur_blkno] = [(start, i)] else: - tot_len = sum(stop - start for start, stop in slices) - result = np.empty(tot_len, dtype=np.int64) - res_view = result + group_dict[cur_blkno].append((start, i)) - i = 0 - for start, stop in slices: - for diff in range(start, stop): - res_view[i] = diff - i += 1 + start = i + cur_blkno = blknos[i] - yield blkno, result + if cur_blkno not in group_dict: + group_order.append(cur_blkno) + group_dict[cur_blkno] = [(start, n)] + else: + group_dict[cur_blkno].append((start, n)) + + for blkno in group_order: + slices = group_dict[blkno] + if len(slices) == 1: + yield blkno, slice(slices[0][0], slices[0][1]) + else: + tot_len = sum([stop - start for start, stop in slices]) + result = np.empty(tot_len, dtype=np.int64) + res_view = result + + i = 0 + for start, stop in slices: + for diff in range(start, stop): + res_view[i] = diff + i += 1 + + yield blkno, result @cython.boundscheck(False) @@ -1670,7 +1673,7 @@ cpdef slice_canonize(slice s): Convert slice to canonical bounded form. """ cdef: - Py_ssize_t start, stop, step, length + Py_ssize_t start = 0, stop = 0, step = 1, length if s.step is None: step = 1 @@ -1727,6 +1730,7 @@ cpdef slice_get_indices_ex(slice slc, Py_ssize_t objlen=PY_SSIZE_T_MAX): PySlice_GetIndicesEx(<PySliceObject *>slc, objlen, &start, &stop, &step, &length) + return start, stop, step, length diff --git a/pandas/src/datetime_helper.h b/pandas/src/datetime_helper.h index 2b24028ff3d8c..bef4b4266c824 100644 --- a/pandas/src/datetime_helper.h +++ b/pandas/src/datetime_helper.h @@ -15,10 +15,6 @@ The full license is in the LICENSE file, distributed with this software. #include "numpy/arrayobject.h" #include "numpy/arrayscalars.h" -#if PY_MAJOR_VERSION >= 3 -#define PyInt_AS_LONG PyLong_AsLong -#endif - npy_int64 get_long_attr(PyObject *o, const char *attr) { npy_int64 long_val; PyObject *value = PyObject_GetAttrString(o, attr); diff --git a/pandas/tslib.pyx b/pandas/tslib.pyx index dd2f5174a516b..fc6e689a35d81 100644 --- a/pandas/tslib.pyx +++ b/pandas/tslib.pyx @@ -8,10 +8,8 @@ from numpy cimport (int8_t, int32_t, int64_t, import_array, ndarray, from datetime cimport get_datetime64_value, get_timedelta64_value import numpy as np -# GH3363 -from sys import version_info -cdef bint PY2 = version_info[0] == 2 -cdef bint PY3 = not PY2 +import sys +cdef bint PY3 = (sys.version_info[0] >= 3) from cpython cimport ( PyTypeObject, @@ -24,11 +22,8 @@ from cpython cimport ( PyUnicode_AsUTF8String, ) - -# Cython < 0.17 doesn't have this in cpython cdef extern from "Python.h": cdef PyTypeObject *Py_TYPE(object) - int PySlice_Check(object) cdef extern from "datetime_helper.h": double total_seconds(object) @@ -2121,8 +2116,8 @@ _DEFAULT_DATETIME = datetime(1, 1, 1).replace( hour=0, minute=0, second=0, microsecond=0) _MONTHS = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'] -_MONTH_NUMBERS = dict((k, i) for i, k in enumerate(_MONTHS)) -_MONTH_ALIASES = dict((k + 1, v) for k, v in enumerate(_MONTHS)) +_MONTH_NUMBERS = {k: i for i, k in enumerate(_MONTHS)} +_MONTH_ALIASES = {(k + 1): v for k, v in enumerate(_MONTHS)} cpdef object _get_rule_month(object source, object default='DEC'): @@ -5529,8 +5524,8 @@ class TimeRE(dict): 'B': self.__seqToRE(self.locale_time.f_month[1:], 'B'), 'b': self.__seqToRE(self.locale_time.a_month[1:], 'b'), 'p': self.__seqToRE(self.locale_time.am_pm, 'p'), - 'Z': self.__seqToRE((tz for tz_names in self.locale_time.timezone - for tz in tz_names), + 'Z': self.__seqToRE([tz for tz_names in self.locale_time.timezone + for tz in tz_names], 'Z'), '%': '%'}) base.__setitem__('W', base.__getitem__('U').replace('U', 'W')) @@ -5553,7 +5548,7 @@ class TimeRE(dict): break else: return '' - regex = '|'.join(re_escape(stuff) for stuff in to_convert) + regex = '|'.join([re_escape(stuff) for stuff in to_convert]) regex = '(?P<%s>%s' % (directive, regex) return '%s)' % regex diff --git a/setup.py b/setup.py index 1c9972defabbe..0c4dd33a70482 100755 --- a/setup.py +++ b/setup.py @@ -462,6 +462,7 @@ def pxd(name): tseries_depends = ['pandas/src/datetime/np_datetime.h', 'pandas/src/datetime/np_datetime_strings.h', + 'pandas/src/datetime_helper.h', 'pandas/src/period_helper.h', 'pandas/src/datetime.pxd'] @@ -584,6 +585,7 @@ def pxd(name): ujson_ext = Extension('pandas.json', depends=['pandas/src/ujson/lib/ultrajson.h', + 'pandas/src/datetime_helper.h', 'pandas/src/numpy_helper.h'], sources=['pandas/src/ujson/python/ujson.c', 'pandas/src/ujson/python/objToJSON.c',