From 83a42fb7d8b5c9533a98961ef5d2f32dea6e638e Mon Sep 17 00:00:00 2001 From: liaoaoyuan97 Date: Wed, 18 Nov 2020 21:03:54 -0800 Subject: [PATCH 1/3] TST: add error message match for raise in test_datetimelike.py GH30999 --- pandas/tests/arrays/test_datetimelike.py | 26 +++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/pandas/tests/arrays/test_datetimelike.py b/pandas/tests/arrays/test_datetimelike.py index 94a5406eb1f8f..ad2a9a72ad2fb 100644 --- a/pandas/tests/arrays/test_datetimelike.py +++ b/pandas/tests/arrays/test_datetimelike.py @@ -10,6 +10,7 @@ import pandas as pd import pandas._testing as tm from pandas.core.arrays import DatetimeArray, PeriodArray, TimedeltaArray +from pandas.core.construction import array, extract_array from pandas.core.indexes.datetimes import DatetimeIndex from pandas.core.indexes.period import Period, PeriodIndex from pandas.core.indexes.timedeltas import TimedeltaIndex @@ -302,11 +303,26 @@ def test_searchsorted_castable_strings(self, arr1d, box): expected = np.array([1, 2], dtype=np.intp) tm.assert_numpy_array_equal(result, expected) - with pytest.raises(TypeError): - arr.searchsorted("foo") - - with pytest.raises(TypeError): - arr.searchsorted([str(arr[1]), "baz"]) + string = "foo" + with pytest.raises( + TypeError, + match=( + f"value should be a '{arr1d._scalar_type.__name__}', 'NaT', " + f"or array of those. Got '{type(string).__name__}' instead." + ), + ): + arr.searchsorted(string) + + str_arr = [str(arr[1]), "baz"] + with pytest.raises( + TypeError, + match=( + f"value should be a '{arr1d._scalar_type.__name__}', 'NaT', " + f"or array of those." + f"Got '{type(extract_array(array(str_arr))).__name__}' instead." + ), + ): + arr.searchsorted(str_arr) def test_getitem_2d(self, arr1d): # 2d slicing on a 1D array From 095769f174875db6a563f2aad23d17a92531a96a Mon Sep 17 00:00:00 2001 From: liaoaoyuan97 Date: Wed, 18 Nov 2020 21:08:26 -0800 Subject: [PATCH 2/3] fix missing space at the end of error msg --- pandas/tests/arrays/test_datetimelike.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/arrays/test_datetimelike.py b/pandas/tests/arrays/test_datetimelike.py index ad2a9a72ad2fb..9f7b17b34a9e5 100644 --- a/pandas/tests/arrays/test_datetimelike.py +++ b/pandas/tests/arrays/test_datetimelike.py @@ -318,7 +318,7 @@ def test_searchsorted_castable_strings(self, arr1d, box): TypeError, match=( f"value should be a '{arr1d._scalar_type.__name__}', 'NaT', " - f"or array of those." + f"or array of those. " f"Got '{type(extract_array(array(str_arr))).__name__}' instead." ), ): From 3e7e512d419d9bf85f692e06f223a8c320fa7b0b Mon Sep 17 00:00:00 2001 From: liaoaoyuan97 Date: Thu, 19 Nov 2020 12:38:12 -0800 Subject: [PATCH 3/3] fix comment --- pandas/tests/arrays/test_datetimelike.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/pandas/tests/arrays/test_datetimelike.py b/pandas/tests/arrays/test_datetimelike.py index 9f7b17b34a9e5..159f52a4c7c25 100644 --- a/pandas/tests/arrays/test_datetimelike.py +++ b/pandas/tests/arrays/test_datetimelike.py @@ -1,3 +1,4 @@ +import re from typing import Type, Union import numpy as np @@ -10,7 +11,6 @@ import pandas as pd import pandas._testing as tm from pandas.core.arrays import DatetimeArray, PeriodArray, TimedeltaArray -from pandas.core.construction import array, extract_array from pandas.core.indexes.datetimes import DatetimeIndex from pandas.core.indexes.period import Period, PeriodIndex from pandas.core.indexes.timedeltas import TimedeltaIndex @@ -303,26 +303,23 @@ def test_searchsorted_castable_strings(self, arr1d, box): expected = np.array([1, 2], dtype=np.intp) tm.assert_numpy_array_equal(result, expected) - string = "foo" with pytest.raises( TypeError, - match=( + match=re.escape( f"value should be a '{arr1d._scalar_type.__name__}', 'NaT', " - f"or array of those. Got '{type(string).__name__}' instead." + "or array of those. Got 'str' instead." ), ): - arr.searchsorted(string) + arr.searchsorted("foo") - str_arr = [str(arr[1]), "baz"] with pytest.raises( TypeError, - match=( + match=re.escape( f"value should be a '{arr1d._scalar_type.__name__}', 'NaT', " - f"or array of those. " - f"Got '{type(extract_array(array(str_arr))).__name__}' instead." + "or array of those. Got 'StringArray' instead." ), ): - arr.searchsorted(str_arr) + arr.searchsorted([str(arr[1]), "baz"]) def test_getitem_2d(self, arr1d): # 2d slicing on a 1D array