From 67088cf141f122eed537d92e50b764158c995fab Mon Sep 17 00:00:00 2001 From: dannyi96 Date: Mon, 25 Jul 2022 22:58:37 +0530 Subject: [PATCH 01/14] Revert "addressing reviews CL#1" This reverts commit 69dc2d43875c5ffc307b829397612a66b0549316. --- pandas/_libs/tslibs/parsing.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_libs/tslibs/parsing.pyx b/pandas/_libs/tslibs/parsing.pyx index 5cb11436f6f45..266ab63d957e0 100644 --- a/pandas/_libs/tslibs/parsing.pyx +++ b/pandas/_libs/tslibs/parsing.pyx @@ -282,7 +282,7 @@ def parse_datetime_string( pass try: - dt = du_parse(date_string, default=_DEFAULT_DATETIME, + dt = du_parse(date_string, default=_DEFAULT_DATETIME, dayfirst=dayfirst, yearfirst=yearfirst, **kwargs) except TypeError: # following may be raised from dateutil From 893fceb52af889e9ca2aac962dd08be3608aaf77 Mon Sep 17 00:00:00 2001 From: dannyi96 Date: Tue, 26 Jul 2022 00:35:35 +0530 Subject: [PATCH 02/14] improving OutOfBoundsDatetime exception messages --- pandas/_libs/tslib.pyx | 4 ++-- pandas/tests/tools/test_to_datetime.py | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index 9c7f35d240f96..b6a393c7e0bd4 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -664,11 +664,11 @@ cpdef array_to_datetime( # Still raise OutOfBoundsDatetime, # as error message is informative. - raise + raise OutOfBoundsDatetime(f"Cannot convert \"{val}\" at position {i} to datetime") assert is_ignore return values, tz_out - raise + raise OutOfBoundsDatetime(f"Cannot convert \"{val}\" at position {i} to datetime") except OutOfBoundsDatetime: if is_raise: diff --git a/pandas/tests/tools/test_to_datetime.py b/pandas/tests/tools/test_to_datetime.py index afa06bf1a79af..cc02502656e95 100644 --- a/pandas/tests/tools/test_to_datetime.py +++ b/pandas/tests/tools/test_to_datetime.py @@ -688,9 +688,10 @@ def test_to_datetime_dt64s(self, cache, dt): "dt", [np.datetime64("1000-01-01"), np.datetime64("5000-01-02")] ) def test_to_datetime_dt64s_out_of_bounds(self, cache, dt): - msg = f"Out of bounds nanosecond timestamp: {dt}" + msg = f"Cannot convert \"{dt}\" at position 0 to datetime" with pytest.raises(OutOfBoundsDatetime, match=msg): to_datetime(dt, errors="raise") + msg = f"Out of bounds nanosecond timestamp: {dt}" with pytest.raises(OutOfBoundsDatetime, match=msg): Timestamp(dt) assert to_datetime(dt, errors="coerce", cache=cache) is NaT @@ -973,13 +974,13 @@ def test_datetime_outofbounds_scalar(self, value, format, infer): assert res is NaT if format is not None: - msg = "is a bad directive in format|Out of bounds nanosecond timestamp" + msg = f"is a bad directive in format|Cannot convert \"{value}\" at position 0 to datetime" with pytest.raises(ValueError, match=msg): to_datetime( value, errors="raise", format=format, infer_datetime_format=infer ) else: - msg = "Out of bounds nanosecond timestamp" + msg = f"Cannot convert \"{value}\" at position 0 to datetime" with pytest.raises(OutOfBoundsDatetime, match=msg): to_datetime( value, errors="raise", format=format, infer_datetime_format=infer @@ -1700,7 +1701,7 @@ def test_to_datetime_barely_out_of_bounds(self): # in an in-bounds datetime arr = np.array(["2262-04-11 23:47:16.854775808"], dtype=object) - msg = "Out of bounds nanosecond timestamp" + msg = f"Cannot convert \"2262-04-11 23:47:16.854775808\" at position 0 to datetime" with pytest.raises(OutOfBoundsDatetime, match=msg): to_datetime(arr) @@ -2593,7 +2594,7 @@ def test_invalid_origins_tzinfo(self): @pytest.mark.parametrize("format", [None, "%Y-%m-%d %H:%M:%S"]) def test_to_datetime_out_of_bounds_with_format_arg(self, format): # see gh-23830 - msg = "Out of bounds nanosecond timestamp" + msg = f"Cannot convert \"2417-10-27 00:00:00\" at position 0 to datetime" with pytest.raises(OutOfBoundsDatetime, match=msg): to_datetime("2417-10-27 00:00:00", format=format) From 682e4bbde13d9c7b75bff5653c53cd33a0576c5b Mon Sep 17 00:00:00 2001 From: dannyi96 Date: Tue, 26 Jul 2022 00:53:15 +0530 Subject: [PATCH 03/14] improving OutOfBoundsDatetime exception messages --- pandas/_libs/tslibs/parsing.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_libs/tslibs/parsing.pyx b/pandas/_libs/tslibs/parsing.pyx index 266ab63d957e0..5cb11436f6f45 100644 --- a/pandas/_libs/tslibs/parsing.pyx +++ b/pandas/_libs/tslibs/parsing.pyx @@ -282,7 +282,7 @@ def parse_datetime_string( pass try: - dt = du_parse(date_string, default=_DEFAULT_DATETIME, + dt = du_parse(date_string, default=_DEFAULT_DATETIME, dayfirst=dayfirst, yearfirst=yearfirst, **kwargs) except TypeError: # following may be raised from dateutil From 26f7d1fdf880771086feabba67def9633171b49a Mon Sep 17 00:00:00 2001 From: dannyi96 Date: Tue, 26 Jul 2022 11:48:01 +0530 Subject: [PATCH 04/14] improving OutOfBoundsDatetime exception messages --- pandas/tests/arrays/categorical/test_take.py | 2 +- pandas/tests/base/test_constructors.py | 3 ++- pandas/tests/indexes/datetimes/test_constructors.py | 2 +- .../tests/indexes/datetimes/test_scalar_compat.py | 2 +- pandas/tests/tools/test_to_datetime.py | 13 ++++++++----- pandas/tests/tslibs/test_array_to_datetime.py | 4 ++-- 6 files changed, 15 insertions(+), 11 deletions(-) diff --git a/pandas/tests/arrays/categorical/test_take.py b/pandas/tests/arrays/categorical/test_take.py index fbdbea1dae3b2..8d1727b59dc5b 100644 --- a/pandas/tests/arrays/categorical/test_take.py +++ b/pandas/tests/arrays/categorical/test_take.py @@ -29,7 +29,7 @@ def test_take_bounds(self, allow_fill): if allow_fill: msg = "indices are out-of-bounds" else: - msg = "index 4 is out of bounds for( axis 0 with)? size 3" + msg = "index 4 is for( axis 0 with)? size 3" with pytest.raises(IndexError, match=msg): cat.take([4, 5], allow_fill=allow_fill) diff --git a/pandas/tests/base/test_constructors.py b/pandas/tests/base/test_constructors.py index 44d6bc57b0431..adad960b67128 100644 --- a/pandas/tests/base/test_constructors.py +++ b/pandas/tests/base/test_constructors.py @@ -157,7 +157,8 @@ def test_constructor_datetime_outofbound(self, a, klass): # Explicit dtype specified # Forced conversion fails for all -> all cases raise error - msg = "Out of bounds" + msg = "Out of bounds|" \ + f"Cannot convert \"{a[0]}\" at position 0 to datetime" with pytest.raises(pd.errors.OutOfBoundsDatetime, match=msg): klass(a, dtype="datetime64[ns]") diff --git a/pandas/tests/indexes/datetimes/test_constructors.py b/pandas/tests/indexes/datetimes/test_constructors.py index e971e311e7d20..449bf75ae46fe 100644 --- a/pandas/tests/indexes/datetimes/test_constructors.py +++ b/pandas/tests/indexes/datetimes/test_constructors.py @@ -521,7 +521,7 @@ def test_construction_outofbounds(self): # coerces to object tm.assert_index_equal(Index(dates), exp) - msg = "Out of bounds nanosecond timestamp" + msg = f'Cannot convert "{dates[0]}" at position 0 to datetime' with pytest.raises(OutOfBoundsDatetime, match=msg): # can't create DatetimeIndex DatetimeIndex(dates) diff --git a/pandas/tests/indexes/datetimes/test_scalar_compat.py b/pandas/tests/indexes/datetimes/test_scalar_compat.py index c60e56875bfcd..50901f9ac3ac5 100644 --- a/pandas/tests/indexes/datetimes/test_scalar_compat.py +++ b/pandas/tests/indexes/datetimes/test_scalar_compat.py @@ -38,7 +38,7 @@ def test_dti_date(self): @pytest.mark.parametrize("data", [["1400-01-01"], [datetime(1400, 1, 1)]]) def test_dti_date_out_of_range(self, data): # GH#1475 - msg = "Out of bounds nanosecond timestamp: 1400-01-01 00:00:00" + msg = f'Cannot convert "{data[0]}" at position 0 to datetime' with pytest.raises(OutOfBoundsDatetime, match=msg): DatetimeIndex(data) diff --git a/pandas/tests/tools/test_to_datetime.py b/pandas/tests/tools/test_to_datetime.py index cc02502656e95..ba3ba087ef346 100644 --- a/pandas/tests/tools/test_to_datetime.py +++ b/pandas/tests/tools/test_to_datetime.py @@ -688,7 +688,7 @@ def test_to_datetime_dt64s(self, cache, dt): "dt", [np.datetime64("1000-01-01"), np.datetime64("5000-01-02")] ) def test_to_datetime_dt64s_out_of_bounds(self, cache, dt): - msg = f"Cannot convert \"{dt}\" at position 0 to datetime" + msg = f'Cannot convert "{dt}" at position 0 to datetime' with pytest.raises(OutOfBoundsDatetime, match=msg): to_datetime(dt, errors="raise") msg = f"Out of bounds nanosecond timestamp: {dt}" @@ -974,13 +974,16 @@ def test_datetime_outofbounds_scalar(self, value, format, infer): assert res is NaT if format is not None: - msg = f"is a bad directive in format|Cannot convert \"{value}\" at position 0 to datetime" + msg = ( + "is a bad directive in format|" + f'Cannot convert "{value}" at position 0 to datetime' + ) with pytest.raises(ValueError, match=msg): to_datetime( value, errors="raise", format=format, infer_datetime_format=infer ) else: - msg = f"Cannot convert \"{value}\" at position 0 to datetime" + msg = f'Cannot convert "{value}" at position 0 to datetime' with pytest.raises(OutOfBoundsDatetime, match=msg): to_datetime( value, errors="raise", format=format, infer_datetime_format=infer @@ -1701,7 +1704,7 @@ def test_to_datetime_barely_out_of_bounds(self): # in an in-bounds datetime arr = np.array(["2262-04-11 23:47:16.854775808"], dtype=object) - msg = f"Cannot convert \"2262-04-11 23:47:16.854775808\" at position 0 to datetime" + msg = 'Cannot convert "2262-04-11 23:47:16.854775808" at position 0 to datetime' with pytest.raises(OutOfBoundsDatetime, match=msg): to_datetime(arr) @@ -2594,7 +2597,7 @@ def test_invalid_origins_tzinfo(self): @pytest.mark.parametrize("format", [None, "%Y-%m-%d %H:%M:%S"]) def test_to_datetime_out_of_bounds_with_format_arg(self, format): # see gh-23830 - msg = f"Cannot convert \"2417-10-27 00:00:00\" at position 0 to datetime" + msg = 'Cannot convert "2417-10-27 00:00:00" at position 0 to datetime' with pytest.raises(OutOfBoundsDatetime, match=msg): to_datetime("2417-10-27 00:00:00", format=format) diff --git a/pandas/tests/tslibs/test_array_to_datetime.py b/pandas/tests/tslibs/test_array_to_datetime.py index a0fafc227e001..fa3e08ba761fc 100644 --- a/pandas/tests/tslibs/test_array_to_datetime.py +++ b/pandas/tests/tslibs/test_array_to_datetime.py @@ -125,7 +125,7 @@ def test_coerce_outside_ns_bounds(invalid_date, errors): kwargs = {"values": arr, "errors": errors} if errors == "raise": - msg = "Out of bounds nanosecond timestamp" + msg = f'Cannot convert "{arr[0]}" at position 0 to datetime' with pytest.raises(ValueError, match=msg): tslib.array_to_datetime(**kwargs) @@ -170,7 +170,7 @@ def test_to_datetime_barely_out_of_bounds(): # Close enough to bounds that dropping nanos # would result in an in-bounds datetime. arr = np.array(["2262-04-11 23:47:16.854775808"], dtype=object) - msg = "Out of bounds nanosecond timestamp: 2262-04-11 23:47:16" + msg = f'Cannot convert "{arr[0]}" at position 0 to datetime' with pytest.raises(tslib.OutOfBoundsDatetime, match=msg): tslib.array_to_datetime(arr) From e27dc9a4ae61d1df4dc7cc1e0dd882aaa4f70e06 Mon Sep 17 00:00:00 2001 From: dannyi96 Date: Tue, 26 Jul 2022 11:52:57 +0530 Subject: [PATCH 05/14] improving OutOfBoundsDatetime exception messages --- pandas/tests/base/test_constructors.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/tests/base/test_constructors.py b/pandas/tests/base/test_constructors.py index adad960b67128..a607bc413159e 100644 --- a/pandas/tests/base/test_constructors.py +++ b/pandas/tests/base/test_constructors.py @@ -157,8 +157,7 @@ def test_constructor_datetime_outofbound(self, a, klass): # Explicit dtype specified # Forced conversion fails for all -> all cases raise error - msg = "Out of bounds|" \ - f"Cannot convert \"{a[0]}\" at position 0 to datetime" + msg = f'Out of bounds|Cannot convert "{a[0]}" at position 0 to datetime' with pytest.raises(pd.errors.OutOfBoundsDatetime, match=msg): klass(a, dtype="datetime64[ns]") From e6435fb63277c00b055870f00a68e16fe34ec874 Mon Sep 17 00:00:00 2001 From: dannyi96 Date: Tue, 26 Jul 2022 12:36:34 +0530 Subject: [PATCH 06/14] improving OutOfBoundsDatetime exception messages --- pandas/tests/arrays/categorical/test_take.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/arrays/categorical/test_take.py b/pandas/tests/arrays/categorical/test_take.py index 8d1727b59dc5b..0b4f54487ec79 100644 --- a/pandas/tests/arrays/categorical/test_take.py +++ b/pandas/tests/arrays/categorical/test_take.py @@ -29,7 +29,7 @@ def test_take_bounds(self, allow_fill): if allow_fill: msg = "indices are out-of-bounds" else: - msg = "index 4 is for( axis 0 with)? size 3" + msg = "index 4 is out of bounds for axis 0 with size 3" with pytest.raises(IndexError, match=msg): cat.take([4, 5], allow_fill=allow_fill) From fbb3e37cd13ba458a0dd5206ab78766207b38a4b Mon Sep 17 00:00:00 2001 From: dannyi96 Date: Tue, 26 Jul 2022 14:12:34 +0530 Subject: [PATCH 07/14] testcase update --- pandas/tests/arrays/categorical/test_take.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/arrays/categorical/test_take.py b/pandas/tests/arrays/categorical/test_take.py index 0b4f54487ec79..fbdbea1dae3b2 100644 --- a/pandas/tests/arrays/categorical/test_take.py +++ b/pandas/tests/arrays/categorical/test_take.py @@ -29,7 +29,7 @@ def test_take_bounds(self, allow_fill): if allow_fill: msg = "indices are out-of-bounds" else: - msg = "index 4 is out of bounds for axis 0 with size 3" + msg = "index 4 is out of bounds for( axis 0 with)? size 3" with pytest.raises(IndexError, match=msg): cat.take([4, 5], allow_fill=allow_fill) From 2a969918626078887dbe1250b8cd227b264aaa35 Mon Sep 17 00:00:00 2001 From: dannyi96 Date: Wed, 27 Jul 2022 04:01:44 +0530 Subject: [PATCH 08/14] improving OutOfBoundsDatetime exception messages --- pandas/_libs/tslib.pyx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index b6a393c7e0bd4..ec6ebdd3b01cc 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -652,7 +652,7 @@ cpdef array_to_datetime( else: raise TypeError(f"{type(val)} is not convertible to datetime") - except OutOfBoundsDatetime: + except OutOfBoundsDatetime as ex: if is_coerce: iresult[i] = NPY_NAT continue @@ -664,11 +664,11 @@ cpdef array_to_datetime( # Still raise OutOfBoundsDatetime, # as error message is informative. - raise OutOfBoundsDatetime(f"Cannot convert \"{val}\" at position {i} to datetime") + raise OutOfBoundsDatetime(f"Cannot convert \"{val}\" at position {i} to datetime") from ex assert is_ignore return values, tz_out - raise OutOfBoundsDatetime(f"Cannot convert \"{val}\" at position {i} to datetime") + raise OutOfBoundsDatetime(f"Cannot convert \"{val}\" at position {i} to datetime") from ex except OutOfBoundsDatetime: if is_raise: From 765049b513fe96d95ac6fd47c4fabe5965c0ed50 Mon Sep 17 00:00:00 2001 From: dannyi96 Date: Thu, 28 Jul 2022 16:48:24 +0530 Subject: [PATCH 09/14] review comments improving OutOfBoundsDatetime exception messages --- pandas/_libs/tslib.pyx | 5 +++-- pandas/tests/base/test_constructors.py | 2 +- pandas/tests/indexes/datetimes/test_constructors.py | 2 +- pandas/tests/indexes/datetimes/test_scalar_compat.py | 2 +- pandas/tests/tools/test_to_datetime.py | 10 +++++----- pandas/tests/tslibs/test_array_to_datetime.py | 4 ++-- 6 files changed, 13 insertions(+), 12 deletions(-) diff --git a/pandas/_libs/tslib.pyx b/pandas/_libs/tslib.pyx index ec6ebdd3b01cc..b3e51191e8efa 100644 --- a/pandas/_libs/tslib.pyx +++ b/pandas/_libs/tslib.pyx @@ -653,6 +653,7 @@ cpdef array_to_datetime( raise TypeError(f"{type(val)} is not convertible to datetime") except OutOfBoundsDatetime as ex: + ex.args = (str(ex) + f" present at position {i}", ) if is_coerce: iresult[i] = NPY_NAT continue @@ -664,11 +665,11 @@ cpdef array_to_datetime( # Still raise OutOfBoundsDatetime, # as error message is informative. - raise OutOfBoundsDatetime(f"Cannot convert \"{val}\" at position {i} to datetime") from ex + raise assert is_ignore return values, tz_out - raise OutOfBoundsDatetime(f"Cannot convert \"{val}\" at position {i} to datetime") from ex + raise except OutOfBoundsDatetime: if is_raise: diff --git a/pandas/tests/base/test_constructors.py b/pandas/tests/base/test_constructors.py index a607bc413159e..858eaacd67ec2 100644 --- a/pandas/tests/base/test_constructors.py +++ b/pandas/tests/base/test_constructors.py @@ -157,7 +157,7 @@ def test_constructor_datetime_outofbound(self, a, klass): # Explicit dtype specified # Forced conversion fails for all -> all cases raise error - msg = f'Out of bounds|Cannot convert "{a[0]}" at position 0 to datetime' + msg = "Out of bounds|Out of bounds .* present at position 0" with pytest.raises(pd.errors.OutOfBoundsDatetime, match=msg): klass(a, dtype="datetime64[ns]") diff --git a/pandas/tests/indexes/datetimes/test_constructors.py b/pandas/tests/indexes/datetimes/test_constructors.py index 449bf75ae46fe..086cb18dbe463 100644 --- a/pandas/tests/indexes/datetimes/test_constructors.py +++ b/pandas/tests/indexes/datetimes/test_constructors.py @@ -521,7 +521,7 @@ def test_construction_outofbounds(self): # coerces to object tm.assert_index_equal(Index(dates), exp) - msg = f'Cannot convert "{dates[0]}" at position 0 to datetime' + msg = "Out of bounds .* present at position 0" with pytest.raises(OutOfBoundsDatetime, match=msg): # can't create DatetimeIndex DatetimeIndex(dates) diff --git a/pandas/tests/indexes/datetimes/test_scalar_compat.py b/pandas/tests/indexes/datetimes/test_scalar_compat.py index 50901f9ac3ac5..890590094094a 100644 --- a/pandas/tests/indexes/datetimes/test_scalar_compat.py +++ b/pandas/tests/indexes/datetimes/test_scalar_compat.py @@ -38,7 +38,7 @@ def test_dti_date(self): @pytest.mark.parametrize("data", [["1400-01-01"], [datetime(1400, 1, 1)]]) def test_dti_date_out_of_range(self, data): # GH#1475 - msg = f'Cannot convert "{data[0]}" at position 0 to datetime' + msg = "Out of bounds .* present at position 0" with pytest.raises(OutOfBoundsDatetime, match=msg): DatetimeIndex(data) diff --git a/pandas/tests/tools/test_to_datetime.py b/pandas/tests/tools/test_to_datetime.py index ba3ba087ef346..ea51bbd6e9ac1 100644 --- a/pandas/tests/tools/test_to_datetime.py +++ b/pandas/tests/tools/test_to_datetime.py @@ -688,7 +688,7 @@ def test_to_datetime_dt64s(self, cache, dt): "dt", [np.datetime64("1000-01-01"), np.datetime64("5000-01-02")] ) def test_to_datetime_dt64s_out_of_bounds(self, cache, dt): - msg = f'Cannot convert "{dt}" at position 0 to datetime' + msg = "Out of bounds .* present at position 0" with pytest.raises(OutOfBoundsDatetime, match=msg): to_datetime(dt, errors="raise") msg = f"Out of bounds nanosecond timestamp: {dt}" @@ -976,14 +976,14 @@ def test_datetime_outofbounds_scalar(self, value, format, infer): if format is not None: msg = ( "is a bad directive in format|" - f'Cannot convert "{value}" at position 0 to datetime' + "Out of bounds .* present at position 0" ) with pytest.raises(ValueError, match=msg): to_datetime( value, errors="raise", format=format, infer_datetime_format=infer ) else: - msg = f'Cannot convert "{value}" at position 0 to datetime' + msg = "Out of bounds .* present at position 0" with pytest.raises(OutOfBoundsDatetime, match=msg): to_datetime( value, errors="raise", format=format, infer_datetime_format=infer @@ -1704,7 +1704,7 @@ def test_to_datetime_barely_out_of_bounds(self): # in an in-bounds datetime arr = np.array(["2262-04-11 23:47:16.854775808"], dtype=object) - msg = 'Cannot convert "2262-04-11 23:47:16.854775808" at position 0 to datetime' + msg = "Out of bounds .* present at position 0" with pytest.raises(OutOfBoundsDatetime, match=msg): to_datetime(arr) @@ -2597,7 +2597,7 @@ def test_invalid_origins_tzinfo(self): @pytest.mark.parametrize("format", [None, "%Y-%m-%d %H:%M:%S"]) def test_to_datetime_out_of_bounds_with_format_arg(self, format): # see gh-23830 - msg = 'Cannot convert "2417-10-27 00:00:00" at position 0 to datetime' + msg = "Out of bounds nanosecond timestamp: 2417-10-27 00:00:00 present at position 0" with pytest.raises(OutOfBoundsDatetime, match=msg): to_datetime("2417-10-27 00:00:00", format=format) diff --git a/pandas/tests/tslibs/test_array_to_datetime.py b/pandas/tests/tslibs/test_array_to_datetime.py index fa3e08ba761fc..3df65619d8449 100644 --- a/pandas/tests/tslibs/test_array_to_datetime.py +++ b/pandas/tests/tslibs/test_array_to_datetime.py @@ -125,7 +125,7 @@ def test_coerce_outside_ns_bounds(invalid_date, errors): kwargs = {"values": arr, "errors": errors} if errors == "raise": - msg = f'Cannot convert "{arr[0]}" at position 0 to datetime' + msg = "Out of bounds .* present at position 0" with pytest.raises(ValueError, match=msg): tslib.array_to_datetime(**kwargs) @@ -170,7 +170,7 @@ def test_to_datetime_barely_out_of_bounds(): # Close enough to bounds that dropping nanos # would result in an in-bounds datetime. arr = np.array(["2262-04-11 23:47:16.854775808"], dtype=object) - msg = f'Cannot convert "{arr[0]}" at position 0 to datetime' + msg = "Out of bounds .* present at position 0" with pytest.raises(tslib.OutOfBoundsDatetime, match=msg): tslib.array_to_datetime(arr) From 6da730db81c0def9abc566f7320dd7f53dee0c2c Mon Sep 17 00:00:00 2001 From: dannyi96 Date: Thu, 28 Jul 2022 20:16:32 +0530 Subject: [PATCH 10/14] testcase update --- pandas/tests/tools/test_to_datetime.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pandas/tests/tools/test_to_datetime.py b/pandas/tests/tools/test_to_datetime.py index ea51bbd6e9ac1..974f3ad9e253d 100644 --- a/pandas/tests/tools/test_to_datetime.py +++ b/pandas/tests/tools/test_to_datetime.py @@ -976,7 +976,7 @@ def test_datetime_outofbounds_scalar(self, value, format, infer): if format is not None: msg = ( "is a bad directive in format|" - "Out of bounds .* present at position 0" + + "Out of bounds .* present at position 0" ) with pytest.raises(ValueError, match=msg): to_datetime( @@ -2597,7 +2597,10 @@ def test_invalid_origins_tzinfo(self): @pytest.mark.parametrize("format", [None, "%Y-%m-%d %H:%M:%S"]) def test_to_datetime_out_of_bounds_with_format_arg(self, format): # see gh-23830 - msg = "Out of bounds nanosecond timestamp: 2417-10-27 00:00:00 present at position 0" + msg = ( + "Out of bounds nanosecond timestamp: 2417-10-27 00:00:00 " + + "present at position 0" + ) with pytest.raises(OutOfBoundsDatetime, match=msg): to_datetime("2417-10-27 00:00:00", format=format) From 649c48b63a7863bc4ffa502375cd0b83d80bc029 Mon Sep 17 00:00:00 2001 From: dannyi96 Date: Mon, 1 Aug 2022 18:46:15 +0530 Subject: [PATCH 11/14] addressed testcase review comments --- pandas/tests/tslibs/test_array_to_datetime.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/tslibs/test_array_to_datetime.py b/pandas/tests/tslibs/test_array_to_datetime.py index 3df65619d8449..7ca7984230609 100644 --- a/pandas/tests/tslibs/test_array_to_datetime.py +++ b/pandas/tests/tslibs/test_array_to_datetime.py @@ -170,7 +170,7 @@ def test_to_datetime_barely_out_of_bounds(): # Close enough to bounds that dropping nanos # would result in an in-bounds datetime. arr = np.array(["2262-04-11 23:47:16.854775808"], dtype=object) - msg = "Out of bounds .* present at position 0" + msg = "Out of bounds nanosecond timestamp: 2262-04-11 23:47:16 present at position 0" with pytest.raises(tslib.OutOfBoundsDatetime, match=msg): tslib.array_to_datetime(arr) From 08860d40e18905670bea4f5d586248f5c4eb9956 Mon Sep 17 00:00:00 2001 From: dannyi96 Date: Mon, 1 Aug 2022 19:48:12 +0530 Subject: [PATCH 12/14] Revert "testcase update" This reverts commit 6da730db81c0def9abc566f7320dd7f53dee0c2c. --- pandas/tests/tools/test_to_datetime.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/pandas/tests/tools/test_to_datetime.py b/pandas/tests/tools/test_to_datetime.py index 974f3ad9e253d..ea51bbd6e9ac1 100644 --- a/pandas/tests/tools/test_to_datetime.py +++ b/pandas/tests/tools/test_to_datetime.py @@ -976,7 +976,7 @@ def test_datetime_outofbounds_scalar(self, value, format, infer): if format is not None: msg = ( "is a bad directive in format|" - + "Out of bounds .* present at position 0" + "Out of bounds .* present at position 0" ) with pytest.raises(ValueError, match=msg): to_datetime( @@ -2597,10 +2597,7 @@ def test_invalid_origins_tzinfo(self): @pytest.mark.parametrize("format", [None, "%Y-%m-%d %H:%M:%S"]) def test_to_datetime_out_of_bounds_with_format_arg(self, format): # see gh-23830 - msg = ( - "Out of bounds nanosecond timestamp: 2417-10-27 00:00:00 " - + "present at position 0" - ) + msg = "Out of bounds nanosecond timestamp: 2417-10-27 00:00:00 present at position 0" with pytest.raises(OutOfBoundsDatetime, match=msg): to_datetime("2417-10-27 00:00:00", format=format) From 5ab25f2496f2c05d741854fb12dd3606ca24e7ce Mon Sep 17 00:00:00 2001 From: dannyi96 Date: Mon, 1 Aug 2022 21:17:43 +0530 Subject: [PATCH 13/14] precommit code format changes --- pandas/tests/tools/test_to_datetime.py | 3 +-- pandas/tests/tslibs/test_array_to_datetime.py | 4 +++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pandas/tests/tools/test_to_datetime.py b/pandas/tests/tools/test_to_datetime.py index ea51bbd6e9ac1..e31c15321d214 100644 --- a/pandas/tests/tools/test_to_datetime.py +++ b/pandas/tests/tools/test_to_datetime.py @@ -975,8 +975,7 @@ def test_datetime_outofbounds_scalar(self, value, format, infer): if format is not None: msg = ( - "is a bad directive in format|" - "Out of bounds .* present at position 0" + "is a bad directive in format|" "Out of bounds .* present at position 0" ) with pytest.raises(ValueError, match=msg): to_datetime( diff --git a/pandas/tests/tslibs/test_array_to_datetime.py b/pandas/tests/tslibs/test_array_to_datetime.py index 7ca7984230609..64a45f6507810 100644 --- a/pandas/tests/tslibs/test_array_to_datetime.py +++ b/pandas/tests/tslibs/test_array_to_datetime.py @@ -170,7 +170,9 @@ def test_to_datetime_barely_out_of_bounds(): # Close enough to bounds that dropping nanos # would result in an in-bounds datetime. arr = np.array(["2262-04-11 23:47:16.854775808"], dtype=object) - msg = "Out of bounds nanosecond timestamp: 2262-04-11 23:47:16 present at position 0" + msg = ( + "Out of bounds nanosecond timestamp: 2262-04-11 23:47:16 present at position 0" + ) with pytest.raises(tslib.OutOfBoundsDatetime, match=msg): tslib.array_to_datetime(arr) From 057e04735902df8940c49247b9264c824084ebc5 Mon Sep 17 00:00:00 2001 From: dannyi96 Date: Mon, 1 Aug 2022 21:34:15 +0530 Subject: [PATCH 14/14] precommit code format changes --- pandas/tests/tools/test_to_datetime.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pandas/tests/tools/test_to_datetime.py b/pandas/tests/tools/test_to_datetime.py index e31c15321d214..b128838318ac0 100644 --- a/pandas/tests/tools/test_to_datetime.py +++ b/pandas/tests/tools/test_to_datetime.py @@ -974,9 +974,7 @@ def test_datetime_outofbounds_scalar(self, value, format, infer): assert res is NaT if format is not None: - msg = ( - "is a bad directive in format|" "Out of bounds .* present at position 0" - ) + msg = "is a bad directive in format|Out of bounds .* present at position 0" with pytest.raises(ValueError, match=msg): to_datetime( value, errors="raise", format=format, infer_datetime_format=infer @@ -2596,7 +2594,10 @@ def test_invalid_origins_tzinfo(self): @pytest.mark.parametrize("format", [None, "%Y-%m-%d %H:%M:%S"]) def test_to_datetime_out_of_bounds_with_format_arg(self, format): # see gh-23830 - msg = "Out of bounds nanosecond timestamp: 2417-10-27 00:00:00 present at position 0" + msg = ( + "Out of bounds nanosecond timestamp: 2417-10-27 00:00:00 " + "present at position 0" + ) with pytest.raises(OutOfBoundsDatetime, match=msg): to_datetime("2417-10-27 00:00:00", format=format)