Skip to content

Commit f1a93d1

Browse files
committed
BUG: pandas.Timedelta min and max limits
*Problem* Pandas Timedelta derives from `datetime.timedelta` and increases the resolution of the timedeltas to nanoseconds. As such Pandas.Timedelta has a smaller range of values. *Solution* This change modifies the advertised `min` and `max` timedeltas.
1 parent 5870731 commit f1a93d1

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

doc/source/whatsnew/v0.18.1.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ Bug Fixes
129129

130130

131131
- Bug in ``Timestamp.__repr__`` that caused ``pprint`` to fail in nested structures (:issue:`12622`)
132+
- Bug in ``Timedelta.min`` and ``Timedelta.max``, the properties now report the true minimum/maximum timedeltas as recognized by Pandas. (:issue:`12727`)
132133

133134

134135

pandas/tseries/tests/test_timedeltas.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,35 @@ def test_timedelta_hash_equality(self):
10831083
ns_td = Timedelta(1, 'ns')
10841084
self.assertNotEqual(hash(ns_td), hash(ns_td.to_pytimedelta()))
10851085

1086+
def test_implementation_limits(self):
1087+
min_td = Timedelta(Timedelta.min)
1088+
max_td = Timedelta(Timedelta.max)
1089+
1090+
# GH 12727
1091+
# timedelta limits correspond to int64 boundaries
1092+
self.assertTrue(min_td.value == np.iinfo(np.int64).min + 1)
1093+
self.assertTrue(max_td.value == np.iinfo(np.int64).max)
1094+
1095+
# Beyond lower limit, a NAT before the Overflow
1096+
self.assertIsInstance(min_td - Timedelta(1, 'ns'),
1097+
pd.tslib.NaTType)
1098+
1099+
with tm.assertRaises(OverflowError):
1100+
min_td - Timedelta(2, 'ns')
1101+
1102+
with tm.assertRaises(OverflowError):
1103+
max_td + Timedelta(1, 'ns')
1104+
1105+
# Same tests using the internal nanosecond values
1106+
td = Timedelta(min_td.value - 1, 'ns')
1107+
self.assertIsInstance(td, pd.tslib.NaTType)
1108+
1109+
with tm.assertRaises(OverflowError):
1110+
Timedelta(min_td.value - 2, 'ns')
1111+
1112+
with tm.assertRaises(OverflowError):
1113+
Timedelta(max_td.value + 1, 'ns')
1114+
10861115

10871116
class TestTimedeltaIndex(tm.TestCase):
10881117
_multiprocess_can_split_ = True

pandas/tslib.pyx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2722,6 +2722,11 @@ class Timedelta(_Timedelta):
27222722
__pos__ = _op_unary_method(lambda x: x, '__pos__')
27232723
__abs__ = _op_unary_method(lambda x: abs(x), '__abs__')
27242724

2725+
2726+
# Resolution is in nanoseconds
2727+
Timedelta.min = Timedelta(np.iinfo(np.int64).min+1, 'ns')
2728+
Timedelta.max = Timedelta(np.iinfo(np.int64).max, 'ns')
2729+
27252730
cdef PyTypeObject* td_type = <PyTypeObject*> Timedelta
27262731

27272732
cdef inline bint is_timedelta(object o):

0 commit comments

Comments
 (0)