Skip to content

Commit 7b6d836

Browse files
jbrockmendelalanbato
authored andcommitted
Replace usage of total_seconds compat func with timedelta method (pandas-dev#17289)
1 parent 53b39bb commit 7b6d836

File tree

7 files changed

+38
-65
lines changed

7 files changed

+38
-65
lines changed

pandas/_libs/period.pyx

+2-5
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ from numpy cimport (int8_t, int32_t, int64_t, import_array, ndarray,
1010
NPY_INT64, NPY_DATETIME, NPY_TIMEDELTA)
1111
import numpy as np
1212

13-
cdef extern from "datetime_helper.h":
14-
double total_seconds(object)
15-
1613
from libc.stdlib cimport free
1714

1815
from pandas import compat
@@ -552,7 +549,7 @@ cdef _reso_local(ndarray[int64_t] stamps, object tz):
552549
&dts)
553550
dt = datetime(dts.year, dts.month, dts.day, dts.hour,
554551
dts.min, dts.sec, dts.us, tz)
555-
delta = int(total_seconds(_get_utcoffset(tz, dt))) * 1000000000
552+
delta = int(_get_utcoffset(tz, dt).total_seconds()) * 1000000000
556553
pandas_datetime_to_datetimestruct(stamps[i] + delta,
557554
PANDAS_FR_ns, &dts)
558555
curr_reso = _reso_stamp(&dts)
@@ -619,7 +616,7 @@ cdef ndarray[int64_t] localize_dt64arr_to_period(ndarray[int64_t] stamps,
619616
&dts)
620617
dt = datetime(dts.year, dts.month, dts.day, dts.hour,
621618
dts.min, dts.sec, dts.us, tz)
622-
delta = int(total_seconds(_get_utcoffset(tz, dt))) * 1000000000
619+
delta = int(_get_utcoffset(tz, dt).total_seconds()) * 1000000000
623620
pandas_datetime_to_datetimestruct(stamps[i] + delta,
624621
PANDAS_FR_ns, &dts)
625622
result[i] = get_period_ordinal(dts.year, dts.month, dts.day,

pandas/_libs/src/datetime_helper.h

-36
This file was deleted.

pandas/_libs/src/ujson/python/objToJSON.c

+21-1
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ Numeric decoder derived from from TCL library
4747
#include <numpy_helper.h> // NOLINT(build/include_order)
4848
#include <stdio.h> // NOLINT(build/include_order)
4949
#include <ultrajson.h> // NOLINT(build/include_order)
50-
#include <datetime_helper.h> // NOLINT(build/include_order)
5150
#include <np_datetime.h> // NOLINT(build/include_order)
5251
#include <np_datetime_strings.h> // NOLINT(build/include_order)
52+
#include "datetime.h"
5353

5454
static PyObject *type_decimal;
5555

@@ -329,6 +329,26 @@ static Py_ssize_t get_attr_length(PyObject *obj, char *attr) {
329329
return ret;
330330
}
331331

332+
npy_int64 get_long_attr(PyObject *o, const char *attr) {
333+
npy_int64 long_val;
334+
PyObject *value = PyObject_GetAttrString(o, attr);
335+
long_val = (PyLong_Check(value) ?
336+
PyLong_AsLongLong(value) : PyInt_AS_LONG(value));
337+
Py_DECREF(value);
338+
return long_val;
339+
}
340+
341+
npy_float64 total_seconds(PyObject *td) {
342+
// Python 2.6 compat
343+
// TODO(anyone): remove this legacy workaround with a more
344+
// direct td.total_seconds()
345+
npy_int64 microseconds = get_long_attr(td, "microseconds");
346+
npy_int64 seconds = get_long_attr(td, "seconds");
347+
npy_int64 days = get_long_attr(td, "days");
348+
npy_int64 days_in_seconds = days * 24LL * 3600LL;
349+
return (microseconds + (seconds + days_in_seconds) * 1000000.0) / 1000000.0;
350+
}
351+
332352
static PyObject *get_item(PyObject *obj, Py_ssize_t i) {
333353
PyObject *tmp = PyInt_FromSsize_t(i);
334354
PyObject *ret;

pandas/_libs/tslib.pyx

+11-17
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ from cpython cimport (
2626
cdef extern from "Python.h":
2727
cdef PyTypeObject *Py_TYPE(object)
2828

29-
cdef extern from "datetime_helper.h":
30-
double total_seconds(object)
31-
3229
# this is our datetime.pxd
3330
from libc.stdlib cimport free
3431

@@ -1639,7 +1636,7 @@ cdef inline void _localize_tso(_TSObject obj, object tz):
16391636
pandas_datetime_to_datetimestruct(obj.value, PANDAS_FR_ns, &obj.dts)
16401637
dt = datetime(obj.dts.year, obj.dts.month, obj.dts.day, obj.dts.hour,
16411638
obj.dts.min, obj.dts.sec, obj.dts.us, tz)
1642-
delta = int(total_seconds(_get_utcoffset(tz, dt))) * 1000000000
1639+
delta = int(_get_utcoffset(tz, dt).total_seconds()) * 1000000000
16431640
if obj.value != NPY_NAT:
16441641
pandas_datetime_to_datetimestruct(obj.value + delta,
16451642
PANDAS_FR_ns, &obj.dts)
@@ -4136,7 +4133,7 @@ def tz_convert(ndarray[int64_t] vals, object tz1, object tz2):
41364133
pandas_datetime_to_datetimestruct(v, PANDAS_FR_ns, &dts)
41374134
dt = datetime(dts.year, dts.month, dts.day, dts.hour,
41384135
dts.min, dts.sec, dts.us, tz1)
4139-
delta = (int(total_seconds(_get_utcoffset(tz1, dt)))
4136+
delta = (int(_get_utcoffset(tz1, dt).total_seconds())
41404137
* 1000000000)
41414138
utc_dates[i] = v - delta
41424139
else:
@@ -4176,8 +4173,8 @@ def tz_convert(ndarray[int64_t] vals, object tz1, object tz2):
41764173
pandas_datetime_to_datetimestruct(v, PANDAS_FR_ns, &dts)
41774174
dt = datetime(dts.year, dts.month, dts.day, dts.hour,
41784175
dts.min, dts.sec, dts.us, tz2)
4179-
delta = int(total_seconds(
4180-
_get_utcoffset(tz2, dt))) * 1000000000
4176+
delta = (int(_get_utcoffset(tz2, dt).total_seconds())
4177+
* 1000000000)
41814178
result[i] = v + delta
41824179
return result
41834180

@@ -4243,7 +4240,7 @@ def tz_convert_single(int64_t val, object tz1, object tz2):
42434240
pandas_datetime_to_datetimestruct(val, PANDAS_FR_ns, &dts)
42444241
dt = datetime(dts.year, dts.month, dts.day, dts.hour,
42454242
dts.min, dts.sec, dts.us, tz1)
4246-
delta = int(total_seconds(_get_utcoffset(tz1, dt))) * 1000000000
4243+
delta = int(_get_utcoffset(tz1, dt).total_seconds()) * 1000000000
42474244
utc_date = val - delta
42484245
elif _get_zone(tz1) != 'UTC':
42494246
trans, deltas, typ = _get_dst_info(tz1)
@@ -4261,7 +4258,7 @@ def tz_convert_single(int64_t val, object tz1, object tz2):
42614258
pandas_datetime_to_datetimestruct(val, PANDAS_FR_ns, &dts)
42624259
dt = datetime(dts.year, dts.month, dts.day, dts.hour,
42634260
dts.min, dts.sec, dts.us, tz2)
4264-
delta = int(total_seconds(_get_utcoffset(tz2, dt))) * 1000000000
4261+
delta = int(_get_utcoffset(tz2, dt).total_seconds()) * 1000000000
42654262
return utc_date + delta
42664263

42674264
# Convert UTC to other timezone
@@ -4333,7 +4330,7 @@ cdef object _get_dst_info(object tz):
43334330
"""
43344331
cache_key = _tz_cache_key(tz)
43354332
if cache_key is None:
4336-
num = int(total_seconds(_get_utcoffset(tz, None))) * 1000000000
4333+
num = int(_get_utcoffset(tz, None).total_seconds()) * 1000000000
43374334
return (np.array([NPY_NAT + 1], dtype=np.int64),
43384335
np.array([num], dtype=np.int64),
43394336
None)
@@ -4380,7 +4377,7 @@ cdef object _get_dst_info(object tz):
43804377
else:
43814378
# static tzinfo
43824379
trans = np.array([NPY_NAT + 1], dtype=np.int64)
4383-
num = int(total_seconds(_get_utcoffset(tz, None))) * 1000000000
4380+
num = int(_get_utcoffset(tz, None).total_seconds()) * 1000000000
43844381
deltas = np.array([num], dtype=np.int64)
43854382
typ = 'static'
43864383

@@ -4403,9 +4400,6 @@ cdef object _get_utc_trans_times_from_dateutil_tz(object tz):
44034400
return new_trans
44044401

44054402

4406-
def tot_seconds(td):
4407-
return total_seconds(td)
4408-
44094403
cpdef ndarray _unbox_utcoffsets(object transinfo):
44104404
cdef:
44114405
Py_ssize_t i, sz
@@ -4415,7 +4409,7 @@ cpdef ndarray _unbox_utcoffsets(object transinfo):
44154409
arr = np.empty(sz, dtype='i8')
44164410

44174411
for i in range(sz):
4418-
arr[i] = int(total_seconds(transinfo[i][0])) * 1000000000
4412+
arr[i] = int(transinfo[i][0].total_seconds()) * 1000000000
44194413

44204414
return arr
44214415

@@ -4458,7 +4452,7 @@ def tz_localize_to_utc(ndarray[int64_t] vals, object tz, object ambiguous=None,
44584452
pandas_datetime_to_datetimestruct(v, PANDAS_FR_ns, &dts)
44594453
dt = datetime(dts.year, dts.month, dts.day, dts.hour,
44604454
dts.min, dts.sec, dts.us, tz)
4461-
delta = int(total_seconds(_get_utcoffset(tz, dt))) * 1000000000
4455+
delta = int(_get_utcoffset(tz, dt).total_seconds()) * 1000000000
44624456
result[i] = v - delta
44634457
return result
44644458

@@ -5181,7 +5175,7 @@ cdef _normalize_local(ndarray[int64_t] stamps, object tz):
51815175
pandas_datetime_to_datetimestruct(stamps[i], PANDAS_FR_ns, &dts)
51825176
dt = datetime(dts.year, dts.month, dts.day, dts.hour,
51835177
dts.min, dts.sec, dts.us, tz)
5184-
delta = int(total_seconds(_get_utcoffset(tz, dt))) * 1000000000
5178+
delta = int(_get_utcoffset(tz, dt).total_seconds()) * 1000000000
51855179
pandas_datetime_to_datetimestruct(stamps[i] + delta,
51865180
PANDAS_FR_ns, &dts)
51875181
result[i] = _normalized_stamp(&dts)

pandas/io/pytables.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4381,7 +4381,7 @@ def _get_tz(tz):
43814381
""" for a tz-aware type, return an encoded zone """
43824382
zone = tslib.get_timezone(tz)
43834383
if zone is None:
4384-
zone = tslib.tot_seconds(tz.utcoffset())
4384+
zone = tz.utcoffset().total_seconds()
43854385
return zone
43864386

43874387

pandas/tseries/offsets.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -778,12 +778,12 @@ def _get_business_hours_by_sec(self):
778778
# create dummy datetime to calcurate businesshours in a day
779779
dtstart = datetime(2014, 4, 1, self.start.hour, self.start.minute)
780780
until = datetime(2014, 4, 1, self.end.hour, self.end.minute)
781-
return tslib.tot_seconds(until - dtstart)
781+
return (until - dtstart).total_seconds()
782782
else:
783783
self.daytime = False
784784
dtstart = datetime(2014, 4, 1, self.start.hour, self.start.minute)
785785
until = datetime(2014, 4, 2, self.end.hour, self.end.minute)
786-
return tslib.tot_seconds(until - dtstart)
786+
return (until - dtstart).total_seconds()
787787

788788
@apply_wraps
789789
def rollback(self, dt):
@@ -907,7 +907,7 @@ def _onOffset(self, dt, businesshours):
907907
op = self._prev_opening_time(dt)
908908
else:
909909
op = self._next_opening_time(dt)
910-
span = tslib.tot_seconds(dt - op)
910+
span = (dt - op).total_seconds()
911911
if span <= businesshours:
912912
return True
913913
else:

setup.py

-2
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,6 @@ def pxd(name):
467467

468468
tseries_depends = ['pandas/_libs/src/datetime/np_datetime.h',
469469
'pandas/_libs/src/datetime/np_datetime_strings.h',
470-
'pandas/_libs/src/datetime_helper.h',
471470
'pandas/_libs/src/period_helper.h',
472471
'pandas/_libs/src/datetime.pxd']
473472

@@ -597,7 +596,6 @@ def pxd(name):
597596

598597
ujson_ext = Extension('pandas._libs.json',
599598
depends=['pandas/_libs/src/ujson/lib/ultrajson.h',
600-
'pandas/_libs/src/datetime_helper.h',
601599
'pandas/_libs/src/numpy_helper.h'],
602600
sources=['pandas/_libs/src/ujson/python/ujson.c',
603601
'pandas/_libs/src/ujson/python/objToJSON.c',

0 commit comments

Comments
 (0)