Skip to content
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

remove unused time conversion funcs #17711

Merged
merged 4 commits into from
Oct 1, 2017
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
31 changes: 2 additions & 29 deletions pandas/_libs/index.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,19 @@ cimport util

import numpy as np

cimport tslib
from tslib cimport _to_i8

from hashtable cimport HashTable

from tslibs.timezones cimport is_utc, get_utcoffset
from pandas._libs import tslib, algos, hashtable as _hash
from pandas._libs import algos, hashtable as _hash
from pandas._libs.tslib import Timestamp, Timedelta
from datetime import datetime, timedelta

from datetime cimport (get_datetime64_value, _pydatetime_to_dts,
pandas_datetimestruct)

from cpython cimport PyTuple_Check, PyList_Check

cdef extern from "datetime.h":
bint PyDateTime_Check(object o)
void PyDateTime_IMPORT()

cdef int64_t iNaT = util.get_nat()


PyDateTime_IMPORT

cdef extern from "Python.h":
int PySlice_Check(object)

Expand Down Expand Up @@ -540,23 +530,6 @@ cpdef convert_scalar(ndarray arr, object value):

return value

cdef inline _to_i8(object val):
cdef pandas_datetimestruct dts
try:
return val.value
except AttributeError:
if util.is_datetime64_object(val):
return get_datetime64_value(val)
elif PyDateTime_Check(val):
tzinfo = getattr(val, 'tzinfo', None)
# Save the original date value so we can get the utcoffset from it.
ival = _pydatetime_to_dts(val, &dts)
if tzinfo is not None and not is_utc(tzinfo):
offset = get_utcoffset(tzinfo, val)
ival -= tslib._delta_to_nanoseconds(offset)
return ival
return val


cdef class MultiIndexObjectEngine(ObjectEngine):
"""
Expand Down
63 changes: 0 additions & 63 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,11 @@ cdef double NaN = <double> np.NaN
cdef double nan = NaN
cdef double NAN = nan

from datetime import datetime as pydatetime

# this is our tseries.pxd
from datetime cimport (
get_timedelta64_value, get_datetime64_value,
npy_timedelta, npy_datetime,
PyDateTime_Check, PyDate_Check, PyTime_Check, PyDelta_Check,
PyDateTime_GET_YEAR,
PyDateTime_GET_MONTH,
PyDateTime_GET_DAY,
PyDateTime_DATE_GET_HOUR,
PyDateTime_DATE_GET_MINUTE,
PyDateTime_DATE_GET_SECOND,
PyDateTime_IMPORT)


Expand Down Expand Up @@ -132,61 +124,6 @@ def memory_usage_of_objects(ndarray[object, ndim=1] arr):
s += arr[i].__sizeof__()
return s

#----------------------------------------------------------------------
# datetime / io related

cdef int _EPOCH_ORD = 719163

from datetime import date as pydate

cdef inline int64_t gmtime(object date):
cdef int y, m, d, h, mn, s, days

y = PyDateTime_GET_YEAR(date)
m = PyDateTime_GET_MONTH(date)
d = PyDateTime_GET_DAY(date)
h = PyDateTime_DATE_GET_HOUR(date)
mn = PyDateTime_DATE_GET_MINUTE(date)
s = PyDateTime_DATE_GET_SECOND(date)

days = pydate(y, m, 1).toordinal() - _EPOCH_ORD + d - 1
return ((<int64_t> (((days * 24 + h) * 60 + mn))) * 60 + s) * 1000


cpdef object to_datetime(int64_t timestamp):
return pydatetime.utcfromtimestamp(timestamp / 1000.0)


cpdef object to_timestamp(object dt):
return gmtime(dt)


def array_to_timestamp(ndarray[object, ndim=1] arr):
cdef int i, n
cdef ndarray[int64_t, ndim=1] result

n = len(arr)
result = np.empty(n, dtype=np.int64)

for i from 0 <= i < n:
result[i] = gmtime(arr[i])

return result


def time64_to_datetime(ndarray[int64_t, ndim=1] arr):
cdef int i, n
cdef ndarray[object, ndim=1] result

n = len(arr)
result = np.empty(n, dtype=object)

for i from 0 <= i < n:
result[i] = to_datetime(arr[i])

return result


#----------------------------------------------------------------------
# isnull / notnull related

Expand Down
2 changes: 2 additions & 0 deletions pandas/_libs/tslib.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ cdef convert_to_tsobject(object, object, object, bint, bint)
cpdef convert_to_timedelta64(object, object)
cdef bint _nat_scalar_rules[6]
cdef bint _check_all_nulls(obj)

cdef _to_i8(object val)
13 changes: 12 additions & 1 deletion pandas/_libs/tslib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3436,7 +3436,18 @@ def cast_to_nanoseconds(ndarray arr):
return result


def pydt_to_i8(object pydt):
cdef inline _to_i8(object val):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need a return type (int64_t)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are cases in which it returns the object input.

cdef pandas_datetimestruct dts
try:
return val.value
except AttributeError:
if is_datetime64_object(val):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just Timestamp(val).value is prob enough here, this looks like really old conversion code

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did u fix for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@jreback jreback Oct 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so just change this to (to remove all of this code)

try
    return val.value
except AttributeError:
      if not is_string_object(val):
         return Timestamp(val).value
   return val

of maybe is isinstance(val, (np.datetime64, datetime))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented this and it doesn't appear to have broken anything.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great, ok merging this. would still like to remove this routing (in favor of pydt_to_i8 but can do that in another PR.

return get_datetime64_value(val)
elif PyDateTime_Check(val):
return Timestamp(val).value
return val

cpdef pydt_to_i8(object pydt):
"""
Convert to int64 representation compatible with numpy datetime64; converts
to UTC
Expand Down
4 changes: 2 additions & 2 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import numpy as np
from pandas import (Series, DataFrame, Panel, Panel4D, Index,
MultiIndex, Int64Index, isna, concat,
MultiIndex, Int64Index, isna, concat, to_datetime,
SparseSeries, SparseDataFrame, PeriodIndex,
DatetimeIndex, TimedeltaIndex)
from pandas.core import config
Expand Down Expand Up @@ -4529,7 +4529,7 @@ def _unconvert_index(data, kind, encoding=None):
def _unconvert_index_legacy(data, kind, legacy=False, encoding=None):
kind = _ensure_decoded(kind)
if kind == u('datetime'):
index = lib.time64_to_datetime(data)
index = to_datetime(data)
elif kind in (u('integer')):
index = np.asarray(data, dtype=object)
elif kind in (u('string')):
Expand Down