-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DEPR: Deprecate Timestamp.to_datetime #14101
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
# cython: profile=False | ||
|
||
import warnings | ||
|
||
cimport numpy as np | ||
from numpy cimport (int8_t, int32_t, int64_t, import_array, ndarray, | ||
NPY_INT64, NPY_DATETIME, NPY_TIMEDELTA) | ||
|
@@ -637,22 +639,6 @@ class Timestamp(_Timestamp): | |
return Timestamp(datetime.replace(self, **kwds), | ||
freq=self.freq) | ||
|
||
def to_pydatetime(self, warn=True): | ||
""" | ||
If warn=True, issue warning if nanoseconds is nonzero | ||
""" | ||
cdef: | ||
pandas_datetimestruct dts | ||
_TSObject ts | ||
|
||
if self.nanosecond != 0 and warn: | ||
print 'Warning: discarding nonzero nanoseconds' | ||
ts = convert_to_tsobject(self, self.tzinfo, None, 0, 0) | ||
|
||
return datetime(ts.dts.year, ts.dts.month, ts.dts.day, | ||
ts.dts.hour, ts.dts.min, ts.dts.sec, | ||
ts.dts.us, ts.tzinfo) | ||
|
||
def isoformat(self, sep='T'): | ||
base = super(_Timestamp, self).isoformat(sep=sep) | ||
if self.nanosecond == 0: | ||
|
@@ -805,11 +791,11 @@ def _make_nan_func(func_name): | |
f.__name__ = func_name | ||
return f | ||
|
||
_nat_methods = ['date', 'now', 'replace', 'to_datetime', 'today'] | ||
_nat_methods = ['date', 'now', 'replace', 'to_pydatetime', 'today'] | ||
|
||
_nan_methods = ['weekday', 'isoweekday', 'total_seconds'] | ||
|
||
_implemented_methods = ['to_datetime64', 'isoformat'] | ||
_implemented_methods = ['to_datetime', 'to_datetime64', 'isoformat'] | ||
_implemented_methods.extend(_nat_methods) | ||
_implemented_methods.extend(_nan_methods) | ||
|
||
|
@@ -986,7 +972,7 @@ cdef class _Timestamp(datetime): | |
ots = other | ||
elif isinstance(other, datetime): | ||
if self.nanosecond == 0: | ||
val = self.to_datetime() | ||
val = self.to_pydatetime() | ||
return PyObject_RichCompareBool(val, other, op) | ||
|
||
try: | ||
|
@@ -1048,7 +1034,7 @@ cdef class _Timestamp(datetime): | |
|
||
cdef bint _compare_outside_nanorange(_Timestamp self, datetime other, | ||
int op) except -1: | ||
cdef datetime dtval = self.to_datetime() | ||
cdef datetime dtval = self.to_pydatetime() | ||
|
||
self._assert_tzawareness_compat(other) | ||
|
||
|
@@ -1078,9 +1064,28 @@ cdef class _Timestamp(datetime): | |
raise TypeError('Cannot compare tz-naive and tz-aware timestamps') | ||
|
||
cpdef datetime to_datetime(_Timestamp self): | ||
""" | ||
DEPRECATED: use :meth:`to_pydatetime` instead. | ||
|
||
Convert a Timestamp object to a native Python datetime object. | ||
""" | ||
warnings.warn("to_datetime is deprecated. Use self.to_pydatetime()", | ||
FutureWarning, stacklevel=2) | ||
return self.to_pydatetime(warn=False) | ||
|
||
cpdef datetime to_pydatetime(_Timestamp self, warn=True): | ||
""" | ||
Convert a Timestamp object to a native Python datetime object. | ||
|
||
If warn=True, issue a warning if nanoseconds is nonzero. | ||
""" | ||
cdef: | ||
pandas_datetimestruct dts | ||
_TSObject ts | ||
|
||
if self.nanosecond != 0 and warn: | ||
warnings.warn("Discarding nonzero nanoseconds in conversion", | ||
UserWarning, stacklevel=2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm maybe RuntimeWarning is better? not sure when that is typically used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think user warning makes more sense here because this is converting user-provided data. |
||
ts = convert_to_tsobject(self, self.tzinfo, None, 0, 0) | ||
dts = ts.dts | ||
return datetime(dts.year, dts.month, dts.day, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add entry about the new warning
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.