From 184e681c66f98abc287d96daf2105a5bb1e5a5de Mon Sep 17 00:00:00 2001 From: Shengpu Tang Date: Sat, 25 Aug 2018 20:49:57 -0400 Subject: [PATCH 1/5] throws OverflowError --- pandas/core/arrays/datetimes.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 1dd34cdf73ab5..484eb430c82b1 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -459,7 +459,8 @@ def _sub_datelike_dti(self, other): self_i8 = self.asi8 other_i8 = other.asi8 - new_values = self_i8 - other_i8 + new_values = checked_add_with_arr(self_i8, -other_i8, + arr_mask=self._isnan) if self.hasnans or other.hasnans: mask = (self._isnan) | (other._isnan) new_values[mask] = iNaT From e4a6927a8d88cb5aa9729dd18965d47cf68e809a Mon Sep 17 00:00:00 2001 From: Shengpu Tang Date: Tue, 28 Aug 2018 10:04:46 -0400 Subject: [PATCH 2/5] add test and whatsnew --- doc/source/whatsnew/v0.24.0.txt | 1 + pandas/tests/arithmetic/test_datetime64.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 3e22084d98234..eb812636ab2d4 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -582,6 +582,7 @@ Datetimelike - Bug in :class:`DataFrame` comparisons against ``Timestamp``-like objects failing to raise ``TypeError`` for inequality checks with mismatched types (:issue:`8932`,:issue:`22163`) - Bug in :class:`DataFrame` with mixed dtypes including ``datetime64[ns]`` incorrectly raising ``TypeError`` on equality comparisons (:issue:`13128`,:issue:`22163`) - Bug in :meth:`DataFrame.eq` comparison against ``NaT`` incorrectly returning ``True`` or ``NaN`` (:issue:`15697`,:issue:`22163`) +- Bug in :class:`DatetimeIndex` subtraction that incorrectly failed to raise `OverflowError` (:issue:22492, :issue:22508) Timedelta ^^^^^^^^^ diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index 879a4e1b4af1a..ec45e7a71e25c 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -1570,6 +1570,27 @@ def test_datetimeindex_sub_timestamp_overflow(self): with pytest.raises(OverflowError): dtimin - variant + def test_datetimeindex_sub_datetimeindex_overflow(self): + dtimax = pd.to_datetime(['now', pd.Timestamp.max]) + dtimin = pd.to_datetime(['now', pd.Timestamp.min]) + + ts_neg = pd.to_datetime(['1950-01-01', '1950-01-01']) + ts_pos = pd.to_datetime(['1980-01-01', '1980-01-01']) + + with pytest.raises(OverflowError): + dtimax - ts_neg + + expected = pd.Timestamp.max.value - ts_pos[1].value + res = dtimax - ts_pos + assert res[1].value == expected + + expected = pd.Timestamp.min.value - ts_neg[1].value + res = dtimin - ts_neg + assert res[1].value == expected + + with pytest.raises(OverflowError): + dtimin - ts_pos + @pytest.mark.parametrize('names', [('foo', None, None), ('baz', 'bar', None), ('bar', 'bar', 'bar')]) From 9c559c545112117ca155318ed45ce9ede3a35f0a Mon Sep 17 00:00:00 2001 From: Shengpu Tang Date: Tue, 28 Aug 2018 10:06:41 -0400 Subject: [PATCH 3/5] style... --- pandas/tests/arithmetic/test_datetime64.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index ec45e7a71e25c..1d08eb521fd1b 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -1590,7 +1590,7 @@ def test_datetimeindex_sub_datetimeindex_overflow(self): with pytest.raises(OverflowError): dtimin - ts_pos - + @pytest.mark.parametrize('names', [('foo', None, None), ('baz', 'bar', None), ('bar', 'bar', 'bar')]) From 7ef7c05a9b461f61ed19ce5e21ddf998b1770925 Mon Sep 17 00:00:00 2001 From: Shengpu Tang Date: Wed, 29 Aug 2018 18:10:40 -0400 Subject: [PATCH 4/5] updated --- doc/source/whatsnew/v0.24.0.txt | 2 +- pandas/tests/arithmetic/test_datetime64.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index eb812636ab2d4..c94751c8e7d9c 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -582,7 +582,7 @@ Datetimelike - Bug in :class:`DataFrame` comparisons against ``Timestamp``-like objects failing to raise ``TypeError`` for inequality checks with mismatched types (:issue:`8932`,:issue:`22163`) - Bug in :class:`DataFrame` with mixed dtypes including ``datetime64[ns]`` incorrectly raising ``TypeError`` on equality comparisons (:issue:`13128`,:issue:`22163`) - Bug in :meth:`DataFrame.eq` comparison against ``NaT`` incorrectly returning ``True`` or ``NaN`` (:issue:`15697`,:issue:`22163`) -- Bug in :class:`DatetimeIndex` subtraction that incorrectly failed to raise `OverflowError` (:issue:22492, :issue:22508) +- Bug in :class:`DatetimeIndex` subtraction that incorrectly failed to raise `OverflowError` when the difference between :class:`Timestamp`s (a :class:`Timedelta` object) exceeds `int64` limit (:issue:`22492`, :issue:`22508`) Timedelta ^^^^^^^^^ diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index 1d08eb521fd1b..b7c0d9a540655 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -1571,6 +1571,7 @@ def test_datetimeindex_sub_timestamp_overflow(self): dtimin - variant def test_datetimeindex_sub_datetimeindex_overflow(self): + # GH#22492, GH#22508 dtimax = pd.to_datetime(['now', pd.Timestamp.max]) dtimin = pd.to_datetime(['now', pd.Timestamp.min]) @@ -1591,6 +1592,16 @@ def test_datetimeindex_sub_datetimeindex_overflow(self): with pytest.raises(OverflowError): dtimin - ts_pos + tmin = pd.to_datetime([pd.Timestamp.min]) + t1 = tmin + pd.Timedelta.max + pd.Timedelta('1us') + with pytest.raises(OverflowError): + t1 - tmin + + tmax = pd.to_datetime([pd.Timestamp.max]) + t2 = tmax + pd.Timedelta.min - pd.Timedelta('1us') + with pytest.raises(OverflowError): + tmax - t2 + @pytest.mark.parametrize('names', [('foo', None, None), ('baz', 'bar', None), ('bar', 'bar', 'bar')]) From cfaf42bccac93e9e18f61c05d46d9a9fccafb63c Mon Sep 17 00:00:00 2001 From: Shengpu Tang Date: Thu, 30 Aug 2018 11:56:22 -0400 Subject: [PATCH 5/5] updated again --- doc/source/whatsnew/v0.24.0.txt | 2 +- pandas/tests/arithmetic/test_datetime64.py | 16 +++++++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index c94751c8e7d9c..21e45294c87a3 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -582,7 +582,7 @@ Datetimelike - Bug in :class:`DataFrame` comparisons against ``Timestamp``-like objects failing to raise ``TypeError`` for inequality checks with mismatched types (:issue:`8932`,:issue:`22163`) - Bug in :class:`DataFrame` with mixed dtypes including ``datetime64[ns]`` incorrectly raising ``TypeError`` on equality comparisons (:issue:`13128`,:issue:`22163`) - Bug in :meth:`DataFrame.eq` comparison against ``NaT`` incorrectly returning ``True`` or ``NaN`` (:issue:`15697`,:issue:`22163`) -- Bug in :class:`DatetimeIndex` subtraction that incorrectly failed to raise `OverflowError` when the difference between :class:`Timestamp`s (a :class:`Timedelta` object) exceeds `int64` limit (:issue:`22492`, :issue:`22508`) +- Bug in :class:`DatetimeIndex` subtraction that incorrectly failed to raise `OverflowError` (:issue:`22492`, :issue:`22508`) Timedelta ^^^^^^^^^ diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index b7c0d9a540655..d597ea834f097 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -1578,20 +1578,22 @@ def test_datetimeindex_sub_datetimeindex_overflow(self): ts_neg = pd.to_datetime(['1950-01-01', '1950-01-01']) ts_pos = pd.to_datetime(['1980-01-01', '1980-01-01']) - with pytest.raises(OverflowError): - dtimax - ts_neg - + # General tests expected = pd.Timestamp.max.value - ts_pos[1].value - res = dtimax - ts_pos - assert res[1].value == expected + result = dtimax - ts_pos + assert result[1].value == expected expected = pd.Timestamp.min.value - ts_neg[1].value - res = dtimin - ts_neg - assert res[1].value == expected + result = dtimin - ts_neg + assert result[1].value == expected + + with pytest.raises(OverflowError): + dtimax - ts_neg with pytest.raises(OverflowError): dtimin - ts_pos + # Edge cases tmin = pd.to_datetime([pd.Timestamp.min]) t1 = tmin + pd.Timedelta.max + pd.Timedelta('1us') with pytest.raises(OverflowError):