Skip to content

[CLN] cy cleanup, de-duplication #21826

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 4 additions & 13 deletions pandas/_libs/index.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ cdef inline bint is_definitely_invalid_key(object val):
or PyList_Check(val) or hasattr(val, '_data'))


def get_value_at(ndarray arr, object loc):
cpdef get_value_at(ndarray arr, object loc, object tz=None):
if arr.descr.type_num == NPY_DATETIME:
return Timestamp(util.get_value_at(arr, loc))
return Timestamp(util.get_value_at(arr, loc), tz=tz)
elif arr.descr.type_num == NPY_TIMEDELTA:
return Timedelta(util.get_value_at(arr, loc))
return util.get_value_at(arr, loc)
Expand All @@ -69,12 +69,7 @@ cpdef object get_value_box(ndarray arr, object loc):
if i >= sz or sz == 0 or i < 0:
raise IndexError('index out of bounds')

if arr.descr.type_num == NPY_DATETIME:
return Timestamp(util.get_value_1d(arr, i))
elif arr.descr.type_num == NPY_TIMEDELTA:
return Timedelta(util.get_value_1d(arr, i))
else:
return util.get_value_1d(arr, i)
return get_value_at(arr, i, tz=None)


# Don't populate hash tables in monotonic indexes larger than this
Expand Down Expand Up @@ -115,11 +110,7 @@ cdef class IndexEngine:
if PySlice_Check(loc) or cnp.PyArray_Check(loc):
return arr[loc]
else:
if arr.descr.type_num == NPY_DATETIME:
return Timestamp(util.get_value_at(arr, loc), tz=tz)
elif arr.descr.type_num == NPY_TIMEDELTA:
return Timedelta(util.get_value_at(arr, loc))
return util.get_value_at(arr, loc)
return get_value_at(arr, loc, tz=tz)

cpdef set_value(self, ndarray arr, object key, object value):
"""
Expand Down
4 changes: 2 additions & 2 deletions pandas/_libs/indexing.pyx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# cython: profile=False

cdef class _NDFrameIndexerBase:
'''
"""
A base class for _NDFrameIndexer for fast instantiation and attribute
access.
'''
"""
cdef public object obj, name, _ndim

def __init__(self, name, obj):
Expand Down
2 changes: 0 additions & 2 deletions pandas/_libs/tslibs/ccalendar.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ Cython implementations of functions resembling the stdlib calendar module
cimport cython
from cython cimport Py_ssize_t

cimport numpy as cnp
from numpy cimport int64_t, int32_t
cnp.import_array()

from locale import LC_TIME
from strptime import LocaleTime
Expand Down
8 changes: 4 additions & 4 deletions pandas/_libs/tslibs/conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,9 @@ cdef inline void localize_tso(_TSObject obj, tzinfo tz):
elif treat_tz_as_dateutil(tz):
dt64_to_dtstruct(obj.value + deltas[pos], &obj.dts)
else:
# TODO: this case is never reached in the tests, but get_dst_info
# has a path that returns typ = None and empty deltas.
# --> Is this path possible?
pass

obj.tzinfo = tz
Expand Down Expand Up @@ -1145,10 +1148,7 @@ cdef ndarray[int64_t] _normalize_local(ndarray[int64_t] stamps, object tz):
# Adjust datetime64 timestamp, recompute datetimestruct
trans, deltas, typ = get_dst_info(tz)

_pos = trans.searchsorted(stamps, side='right') - 1
if _pos.dtype != np.int64:
_pos = _pos.astype(np.int64)
pos = _pos
pos = trans.searchsorted(stamps, side='right') - 1

# statictzinfo
if typ not in ['pytz', 'dateutil']:
Expand Down
5 changes: 1 addition & 4 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -960,10 +960,7 @@ cdef ndarray[int64_t] localize_dt64arr_to_period(ndarray[int64_t] stamps,
# Adjust datetime64 timestamp, recompute datetimestruct
trans, deltas, typ = get_dst_info(tz)

_pos = trans.searchsorted(stamps, side='right') - 1
if _pos.dtype != np.int64:
_pos = _pos.astype(np.int64)
pos = _pos
pos = trans.searchsorted(stamps, side='right') - 1

# statictzinfo
if typ not in ['pytz', 'dateutil']:
Expand Down
5 changes: 1 addition & 4 deletions pandas/_libs/tslibs/resolution.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,7 @@ cdef _reso_local(ndarray[int64_t] stamps, object tz):
# Adjust datetime64 timestamp, recompute datetimestruct
trans, deltas, typ = get_dst_info(tz)

_pos = trans.searchsorted(stamps, side='right') - 1
if _pos.dtype != np.int64:
_pos = _pos.astype(np.int64)
pos = _pos
pos = trans.searchsorted(stamps, side='right') - 1

# statictzinfo
if typ not in ['pytz', 'dateutil']:
Expand Down
4 changes: 3 additions & 1 deletion pandas/_libs/tslibs/timezones.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ cdef object get_utc_trans_times_from_dateutil_tz(object tz):
return new_trans


cpdef ndarray unbox_utcoffsets(object transinfo):
cpdef ndarray[int64_t, ndim=1] unbox_utcoffsets(object transinfo):
cdef:
Py_ssize_t i, sz
ndarray[int64_t] arr
Expand Down Expand Up @@ -216,6 +216,8 @@ cdef object get_dst_info(object tz):
"""
cache_key = tz_cache_key(tz)
if cache_key is None:
# e.g. pytz.FixedOffset, matplotlib.dates._UTC,
# psycopg2.tz.FixedOffsetTimezone
num = int(get_utcoffset(tz, None).total_seconds()) * 1000000000
return (np.array([NPY_NAT + 1], dtype=np.int64),
np.array([num], dtype=np.int64),
Expand Down