From 999b3ee21117c21f2ed167592bf78ac7bef19812 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sun, 8 Sep 2019 11:16:28 -0700 Subject: [PATCH 1/2] CLN/BUG: array_equivalent on nested objects --- ci/code_checks.sh | 6 ++++++ pandas/_libs/testing.pyx | 8 +++----- pandas/core/dtypes/missing.py | 2 +- pandas/tests/dtypes/test_missing.py | 14 ++++++++++++++ 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/ci/code_checks.sh b/ci/code_checks.sh index f839d86318e2e..9f9db0741d36e 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -125,6 +125,12 @@ if [[ -z "$CHECK" || "$CHECK" == "patterns" ]]; then # invgrep -R --include="*.py*" -E "from numpy import nan " pandas # GH#24822 not yet implemented since the offending imports have not all been removed RET=$(($RET + $?)) ; echo $MSG "DONE" + # flake8 check will find bare except E722 in .py files, need to grep + # to find these in cython files + MSG='Check for bare except statements' ; echo $MSG + invgrep -R --include="*.py*" -E "except:" pandas + RET=$(($RET + $?)) ; echo $MSG "DONE" + MSG='Check for pytest warns' ; echo $MSG invgrep -r -E --include '*.py' 'pytest\.warns' pandas/tests/ RET=$(($RET + $?)) ; echo $MSG "DONE" diff --git a/pandas/_libs/testing.pyx b/pandas/_libs/testing.pyx index ae0d3ac1a61ca..8602db9e5bb1d 100644 --- a/pandas/_libs/testing.pyx +++ b/pandas/_libs/testing.pyx @@ -143,11 +143,9 @@ cpdef assert_almost_equal(a, b, from pandas.util.testing import assert_attr_equal assert_attr_equal('dtype', a, b, obj=obj) - try: - if array_equivalent(a, b, strict_nan=True): - return True - except: - pass + if array_equivalent(a, b, strict_nan=True): + return True + else: na, nb = len(a), len(b) diff --git a/pandas/core/dtypes/missing.py b/pandas/core/dtypes/missing.py index 056cd2222af3c..6dd032b9248ed 100644 --- a/pandas/core/dtypes/missing.py +++ b/pandas/core/dtypes/missing.py @@ -445,7 +445,7 @@ def array_equivalent(left, right, strict_nan=False): if not isinstance(right_value, float) or not np.isnan(right_value): return False else: - if left_value != right_value: + if np.any(left_value != right_value): return False return True diff --git a/pandas/tests/dtypes/test_missing.py b/pandas/tests/dtypes/test_missing.py index bbc485ecf94f2..1a292d5bfcbb6 100644 --- a/pandas/tests/dtypes/test_missing.py +++ b/pandas/tests/dtypes/test_missing.py @@ -360,6 +360,20 @@ def test_array_equivalent_str(): ) +def test_array_equivalent_nested(): + # reached in groupby aggregations, make sure we use np.any when checking + # if the comparison is truthy + left = np.array([np.array([50, 70, 90]), np.array([20, 30, 40])], dtype=object) + right = np.array([np.array([50, 70, 90]), np.array([20, 30, 40])], dtype=object) + + assert array_equivalent(left, right, strict_nan=True) + assert not array_equivalent(left, right[::-1], strict_nan=True) + + left = np.array([np.array([50, 50, 50]), np.array([40, 40, 40])], dtype=object) + right = np.array([50, 40]) + assert not array_equivalent(left, right, strict_nan=True) + + @pytest.mark.parametrize( "dtype, na_value", [ From 6b0833f9447a1f0d80ed84b462751427fbbf8cb8 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 13 Sep 2019 12:09:15 -0700 Subject: [PATCH 2/2] revert code check --- ci/code_checks.sh | 6 ------ 1 file changed, 6 deletions(-) diff --git a/ci/code_checks.sh b/ci/code_checks.sh index 9f9db0741d36e..f839d86318e2e 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -125,12 +125,6 @@ if [[ -z "$CHECK" || "$CHECK" == "patterns" ]]; then # invgrep -R --include="*.py*" -E "from numpy import nan " pandas # GH#24822 not yet implemented since the offending imports have not all been removed RET=$(($RET + $?)) ; echo $MSG "DONE" - # flake8 check will find bare except E722 in .py files, need to grep - # to find these in cython files - MSG='Check for bare except statements' ; echo $MSG - invgrep -R --include="*.py*" -E "except:" pandas - RET=$(($RET + $?)) ; echo $MSG "DONE" - MSG='Check for pytest warns' ; echo $MSG invgrep -r -E --include '*.py' 'pytest\.warns' pandas/tests/ RET=$(($RET + $?)) ; echo $MSG "DONE"