From e947e4d2b5574382c3cca6dff120ff40a6a53d7f Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Tue, 28 Nov 2017 17:51:35 -0800 Subject: [PATCH 1/5] update imports of normalize_date --- pandas/core/indexes/datetimes.py | 17 ++++++++--------- pandas/core/tools/datetimes.py | 2 -- pandas/tests/indexes/datetimes/test_tools.py | 6 +++--- pandas/tseries/offsets.py | 4 ++-- 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 1578ae924c9bb..cd84228ec0f99 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -43,8 +43,7 @@ DatelikeOps, TimelikeOps, DatetimeIndexOpsMixin) from pandas.tseries.offsets import ( DateOffset, generate_range, Tick, CDay, prefix_mapping) -from pandas.core.tools.datetimes import ( - parse_time_string, normalize_date, to_time) + from pandas.core.tools.timedeltas import to_timedelta from pandas.util._decorators import (Appender, cache_readonly, deprecate_kwarg, Substitution) @@ -55,7 +54,7 @@ from pandas._libs import (lib, index as libindex, tslib as libts, algos as libalgos, join as libjoin, Timestamp) -from pandas._libs.tslibs import (timezones, conversion, fields, +from pandas._libs.tslibs import (timezones, conversion, fields, parsing, period as libperiod) # -------- some conversion wrapper functions @@ -524,14 +523,14 @@ def _generate(cls, start, end, periods, name, offset, if start is not None: if normalize: - start = normalize_date(start) + start = libts.normalize_date(start) _normalized = True else: _normalized = _normalized and start.time() == _midnight if end is not None: if normalize: - end = normalize_date(end) + end = libts.normalize_date(end) _normalized = True else: _normalized = _normalized and end.time() == _midnight @@ -1529,7 +1528,7 @@ def _maybe_cast_slice_bound(self, label, side, kind): if isinstance(label, compat.string_types): freq = getattr(self, 'freqstr', getattr(self, 'inferred_freq', None)) - _, parsed, reso = parse_time_string(label, freq) + _, parsed, reso = parsing.parse_time_string(label, freq) lower, upper = self._parsed_string_to_bounds(reso, parsed) # lower, upper form the half-open interval: # [parsed, parsed + 1 freq) @@ -1546,7 +1545,7 @@ def _maybe_cast_slice_bound(self, label, side, kind): def _get_string_slice(self, key, use_lhs=True, use_rhs=True): freq = getattr(self, 'freqstr', getattr(self, 'inferred_freq', None)) - _, parsed, reso = parse_time_string(key, freq) + _, parsed, reso = parsing.parse_time_string(key, freq) loc = self._partial_date_slice(reso, parsed, use_lhs=use_lhs, use_rhs=use_rhs) return loc @@ -1965,8 +1964,8 @@ def indexer_between_time(self, start_time, end_time, include_start=True, ------- values_between_time : TimeSeries """ - start_time = to_time(start_time) - end_time = to_time(end_time) + start_time = tools.to_time(start_time) + end_time = tools.to_time(end_time) time_micros = self._get_time_micros() start_micros = _time_to_micros(start_time) end_micros = _time_to_micros(end_time) diff --git a/pandas/core/tools/datetimes.py b/pandas/core/tools/datetimes.py index 219fb3f67db97..4245b9eb641ba 100644 --- a/pandas/core/tools/datetimes.py +++ b/pandas/core/tools/datetimes.py @@ -629,8 +629,6 @@ def calc_with_mask(carg, mask): return None -normalize_date = tslib.normalize_date - # Fixed time formats for time parsing _time_formats = ["%H:%M", "%H%M", "%I:%M%p", "%I%M%p", "%H:%M:%S", "%H%M%S", "%I:%M:%S%p", "%I%M%S%p"] diff --git a/pandas/tests/indexes/datetimes/test_tools.py b/pandas/tests/indexes/datetimes/test_tools.py index a1287c3102b77..adcd40e7317ce 100644 --- a/pandas/tests/indexes/datetimes/test_tools.py +++ b/pandas/tests/indexes/datetimes/test_tools.py @@ -16,7 +16,7 @@ from pandas._libs import tslib from pandas._libs.tslibs import parsing from pandas.core.tools import datetimes as tools -from pandas.core.tools.datetimes import normalize_date + from pandas.compat import lmap from pandas.compat.numpy import np_array_datetime64_compat from pandas.core.dtypes.common import is_datetime64_ns_dtype @@ -1576,12 +1576,12 @@ def test_coerce_of_invalid_datetimes(self): def test_normalize_date(): value = date(2012, 9, 7) - result = normalize_date(value) + result = tslib.normalize_date(value) assert (result == datetime(2012, 9, 7)) value = datetime(2012, 9, 7, 12) - result = normalize_date(value) + result = tslib.normalize_date(value) assert (result == datetime(2012, 9, 7)) diff --git a/pandas/tseries/offsets.py b/pandas/tseries/offsets.py index a3cddaa19dc17..857ec9e9881d9 100644 --- a/pandas/tseries/offsets.py +++ b/pandas/tseries/offsets.py @@ -8,7 +8,7 @@ import numpy as np from pandas.core.dtypes.generic import ABCSeries, ABCDatetimeIndex, ABCPeriod -from pandas.core.tools.datetimes import to_datetime, normalize_date +from pandas.core.tools.datetimes import to_datetime from pandas.core.common import AbstractMethodError # import after tools, dateutil check @@ -103,7 +103,7 @@ def wrapper(self, other): if self.normalize: # normalize_date returns normal datetime - result = normalize_date(result) + result = tslib.normalize_date(result) if tz is not None and result.tzinfo is None: result = tslib._localize_pydatetime(result, tz) From b6d64bce8ce76cf64f52302890eb50a2860acf75 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Tue, 28 Nov 2017 17:53:28 -0800 Subject: [PATCH 2/5] use nogil version of sqrt --- pandas/_libs/algos.pyx | 5 ++++- pandas/_libs/lib.pyx | 5 ++++- pandas/_libs/window.pyx | 1 + 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/pandas/_libs/algos.pyx b/pandas/_libs/algos.pyx index 61d543cd7303a..a8c748cbb37c0 100644 --- a/pandas/_libs/algos.pyx +++ b/pandas/_libs/algos.pyx @@ -27,7 +27,10 @@ from numpy cimport (ndarray, cdef double NaN = np.NaN cdef double nan = NaN -from libc.math cimport sqrt, fabs +from libc.math cimport fabs +cdef extern from "src/headers/math.h": + # use this version instead of `from libc.math cimport sqrt`; see GH#18420 + double sqrt(double x) nogil # this is our util.pxd from util cimport numeric, get_nat diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 02b3839ebf181..49f95bada603b 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -62,7 +62,10 @@ cdef int64_t NPY_NAT = util.get_nat() cimport util from util cimport is_array, _checknull -from libc.math cimport sqrt, fabs +from libc.math cimport fabs +cdef extern from "src/headers/math.h": + # use this version instead of `from libc.math cimport sqrt`; see GH#18420 + double sqrt(double x) nogil def values_from_object(object o): diff --git a/pandas/_libs/window.pyx b/pandas/_libs/window.pyx index ecce45742afa7..cc8bca573f379 100644 --- a/pandas/_libs/window.pyx +++ b/pandas/_libs/window.pyx @@ -37,6 +37,7 @@ from util cimport numeric cdef extern from "../src/headers/math.h": int signbit(double) nogil double sqrt(double x) nogil + # use this version instead of `from libc.math cimport sqrt`; see GH#18420 # Cython implementations of rolling sum, mean, variance, skewness, From 3d6d2cfe871bd00238f4c4f8408ad2671e6a5ae3 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Tue, 28 Nov 2017 17:55:24 -0800 Subject: [PATCH 3/5] cleanup cimports; avoid import * --- pandas/_libs/lib.pyx | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 49f95bada603b..4768fcca4a8c8 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -1,13 +1,18 @@ # cython: profile=False -cimport numpy as np -cimport cython -import numpy as np -import sys +import operator -cdef bint PY3 = (sys.version_info[0] >= 3) - -from numpy cimport * +cimport cython +from cython cimport Py_ssize_t +import numpy as np +cimport numpy as np +from numpy cimport (ndarray, PyArray_NDIM, PyArray_GETITEM, PyArray_SETITEM, + PyArray_ITER_DATA, PyArray_ITER_NEXT, PyArray_IterNew, + flatiter, NPY_OBJECT, + int64_t, + float32_t, float64_t, + uint8_t, uint64_t, + complex128_t) # initialize numpy np.import_array() np.import_ufunc() @@ -57,9 +62,9 @@ from tslib import NaT, Timestamp, Timedelta, array_to_datetime from interval import Interval from missing cimport checknull -cdef int64_t NPY_NAT = util.get_nat() cimport util +cdef int64_t NPY_NAT = util.get_nat() from util cimport is_array, _checknull from libc.math cimport fabs @@ -497,7 +502,6 @@ def maybe_booleans_to_slice(ndarray[uint8_t] mask): @cython.wraparound(False) @cython.boundscheck(False) def scalar_compare(ndarray[object] values, object val, object op): - import operator cdef: Py_ssize_t i, n = len(values) ndarray[uint8_t, cast=True] result @@ -532,7 +536,7 @@ def scalar_compare(ndarray[object] values, object val, object op): result[i] = True else: try: - result[i] = cpython.PyObject_RichCompareBool(x, val, flag) + result[i] = PyObject_RichCompareBool(x, val, flag) except (TypeError): result[i] = True elif flag == cpython.Py_EQ: @@ -544,7 +548,7 @@ def scalar_compare(ndarray[object] values, object val, object op): result[i] = False else: try: - result[i] = cpython.PyObject_RichCompareBool(x, val, flag) + result[i] = PyObject_RichCompareBool(x, val, flag) except (TypeError): result[i] = False @@ -556,7 +560,7 @@ def scalar_compare(ndarray[object] values, object val, object op): elif isnull_val: result[i] = False else: - result[i] = cpython.PyObject_RichCompareBool(x, val, flag) + result[i] = PyObject_RichCompareBool(x, val, flag) return result.view(bool) @@ -585,7 +589,6 @@ cpdef bint array_equivalent_object(object[:] left, object[:] right): @cython.wraparound(False) @cython.boundscheck(False) def vec_compare(ndarray[object] left, ndarray[object] right, object op): - import operator cdef: Py_ssize_t i, n = len(left) ndarray[uint8_t, cast=True] result @@ -620,7 +623,7 @@ def vec_compare(ndarray[object] left, ndarray[object] right, object op): if checknull(x) or checknull(y): result[i] = True else: - result[i] = cpython.PyObject_RichCompareBool(x, y, flag) + result[i] = PyObject_RichCompareBool(x, y, flag) else: for i in range(n): x = left[i] @@ -629,7 +632,7 @@ def vec_compare(ndarray[object] left, ndarray[object] right, object op): if checknull(x) or checknull(y): result[i] = False else: - result[i] = cpython.PyObject_RichCompareBool(x, y, flag) + result[i] = PyObject_RichCompareBool(x, y, flag) return result.view(bool) From e6e7bcbb3eef97f64656acdfd655dbf88f6ee455 Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Wed, 29 Nov 2017 10:12:37 -0800 Subject: [PATCH 4/5] get sqrt from util --- pandas/_libs/algos.pyx | 5 +---- pandas/_libs/lib.pyx | 5 +---- pandas/_libs/src/util.pxd | 6 ++++++ pandas/_libs/window.pyx | 4 +--- 4 files changed, 9 insertions(+), 11 deletions(-) diff --git a/pandas/_libs/algos.pyx b/pandas/_libs/algos.pyx index a8c748cbb37c0..606794011db5b 100644 --- a/pandas/_libs/algos.pyx +++ b/pandas/_libs/algos.pyx @@ -28,12 +28,9 @@ cdef double NaN = np.NaN cdef double nan = NaN from libc.math cimport fabs -cdef extern from "src/headers/math.h": - # use this version instead of `from libc.math cimport sqrt`; see GH#18420 - double sqrt(double x) nogil # this is our util.pxd -from util cimport numeric, get_nat +from util cimport numeric, get_nat, sqrt import missing diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index 4768fcca4a8c8..c50d820e177aa 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -65,12 +65,9 @@ from missing cimport checknull cimport util cdef int64_t NPY_NAT = util.get_nat() -from util cimport is_array, _checknull +from util cimport is_array, _checknull, sqrt from libc.math cimport fabs -cdef extern from "src/headers/math.h": - # use this version instead of `from libc.math cimport sqrt`; see GH#18420 - double sqrt(double x) nogil def values_from_object(object o): diff --git a/pandas/_libs/src/util.pxd b/pandas/_libs/src/util.pxd index 61783ab47cb86..aa765e772d524 100644 --- a/pandas/_libs/src/util.pxd +++ b/pandas/_libs/src/util.pxd @@ -2,6 +2,12 @@ from numpy cimport ndarray cimport numpy as cnp cimport cpython + +cdef extern from "headers/math.h": + # use this version instead of `from libc.math cimport sqrt`; see GH#18420 + double sqrt(double x) nogil + + cdef extern from "numpy_helper.h": void set_array_not_contiguous(ndarray ao) diff --git a/pandas/_libs/window.pyx b/pandas/_libs/window.pyx index cc8bca573f379..52a7cf2a5d0bd 100644 --- a/pandas/_libs/window.pyx +++ b/pandas/_libs/window.pyx @@ -32,12 +32,10 @@ cdef double NaN = np.NaN cdef inline int int_max(int a, int b): return a if a >= b else b cdef inline int int_min(int a, int b): return a if a <= b else b -from util cimport numeric +from util cimport numeric, sqrt cdef extern from "../src/headers/math.h": int signbit(double) nogil - double sqrt(double x) nogil - # use this version instead of `from libc.math cimport sqrt`; see GH#18420 # Cython implementations of rolling sum, mean, variance, skewness, From e743c1e13c87ee74dda74a7990e784ae4f534c7f Mon Sep 17 00:00:00 2001 From: Brock Mendel Date: Sun, 3 Dec 2017 12:01:21 -0800 Subject: [PATCH 5/5] revert sqrt changes --- pandas/_libs/algos.pyx | 4 ++-- pandas/_libs/lib.pyx | 4 ++-- pandas/_libs/src/util.pxd | 5 ----- pandas/_libs/window.pyx | 3 ++- 4 files changed, 6 insertions(+), 10 deletions(-) diff --git a/pandas/_libs/algos.pyx b/pandas/_libs/algos.pyx index 606794011db5b..df8f7bab51dbe 100644 --- a/pandas/_libs/algos.pyx +++ b/pandas/_libs/algos.pyx @@ -27,10 +27,10 @@ from numpy cimport (ndarray, cdef double NaN = np.NaN cdef double nan = NaN -from libc.math cimport fabs +from libc.math cimport fabs, sqrt # this is our util.pxd -from util cimport numeric, get_nat, sqrt +from util cimport numeric, get_nat import missing diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index c50d820e177aa..a39f83d5261c0 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -65,9 +65,9 @@ from missing cimport checknull cimport util cdef int64_t NPY_NAT = util.get_nat() -from util cimport is_array, _checknull, sqrt +from util cimport is_array, _checknull -from libc.math cimport fabs +from libc.math cimport fabs, sqrt def values_from_object(object o): diff --git a/pandas/_libs/src/util.pxd b/pandas/_libs/src/util.pxd index aa765e772d524..e5fe90aa81f7d 100644 --- a/pandas/_libs/src/util.pxd +++ b/pandas/_libs/src/util.pxd @@ -3,11 +3,6 @@ cimport numpy as cnp cimport cpython -cdef extern from "headers/math.h": - # use this version instead of `from libc.math cimport sqrt`; see GH#18420 - double sqrt(double x) nogil - - cdef extern from "numpy_helper.h": void set_array_not_contiguous(ndarray ao) diff --git a/pandas/_libs/window.pyx b/pandas/_libs/window.pyx index 52a7cf2a5d0bd..ecce45742afa7 100644 --- a/pandas/_libs/window.pyx +++ b/pandas/_libs/window.pyx @@ -32,10 +32,11 @@ cdef double NaN = np.NaN cdef inline int int_max(int a, int b): return a if a >= b else b cdef inline int int_min(int a, int b): return a if a <= b else b -from util cimport numeric, sqrt +from util cimport numeric cdef extern from "../src/headers/math.h": int signbit(double) nogil + double sqrt(double x) nogil # Cython implementations of rolling sum, mean, variance, skewness,