|
34 | 34 | import functools
|
35 | 35 | import warnings
|
36 | 36 |
|
| 37 | +from typing import Any |
| 38 | + |
37 | 39 | import numpy as np
|
38 | 40 | import pandas as pd
|
39 | 41 |
|
@@ -5673,7 +5675,7 @@ def equals(self, other, rtol=0, atol=0, nans_equal=False, check_axes=False):
|
5673 | 5675 |
|
5674 | 5676 | See Also
|
5675 | 5677 | --------
|
5676 |
| - Array.eq |
| 5678 | + Array.eq, Array.allclose |
5677 | 5679 |
|
5678 | 5680 | Notes
|
5679 | 5681 | -----
|
@@ -5775,6 +5777,79 @@ def equals(self, other, rtol=0, atol=0, nans_equal=False, check_axes=False):
|
5775 | 5777 | except ValueError:
|
5776 | 5778 | return False
|
5777 | 5779 |
|
| 5780 | + def allclose(self, other: Any, rtol: float = 1e-05, atol: float = 1e-08, nans_equal: bool = True, |
| 5781 | + check_axes: bool = False) -> bool: |
| 5782 | + """ |
| 5783 | + Compares self with another array and returns True if they are element-wise equal within a tolerance. |
| 5784 | +
|
| 5785 | + The tolerance values are positive, typically very small numbers. |
| 5786 | + The relative difference (rtol * abs(other)) and the absolute difference atol are added together to compare |
| 5787 | + against the absolute difference between self and other. |
| 5788 | +
|
| 5789 | + NaN values are treated as equal if they are in the same place and if `nans_equal=True`. |
| 5790 | +
|
| 5791 | + Parameters |
| 5792 | + ---------- |
| 5793 | + other : Array-like |
| 5794 | + Input array. asarray() is used on a non-Array input. |
| 5795 | + rtol : float or int, optional |
| 5796 | + The relative tolerance parameter (see Notes). Defaults to 1e-05. |
| 5797 | + atol : float or int, optional |
| 5798 | + The absolute tolerance parameter (see Notes). Defaults to 1e-08. |
| 5799 | + nans_equal : boolean, optional |
| 5800 | + Whether or not to consider NaN values at the same positions in the two arrays as equal. |
| 5801 | + By default, an array containing NaN values is never equal to another array, even if that other array |
| 5802 | + also contains NaN values at the same positions. The reason is that a NaN value is different from |
| 5803 | + *anything*, including itself. Defaults to True. |
| 5804 | + check_axes : boolean, optional |
| 5805 | + Whether or not to check that the set of axes and their order is the same on both sides. Defaults to False. |
| 5806 | + If False, two arrays with compatible axes (and the same data) will compare equal, even if some axis is |
| 5807 | + missing on either side or if the axes are in a different order. |
| 5808 | +
|
| 5809 | + Returns |
| 5810 | + ------- |
| 5811 | + bool |
| 5812 | + Returns True if the two arrays are equal within the given tolerance; False otherwise. |
| 5813 | +
|
| 5814 | + See Also |
| 5815 | + -------- |
| 5816 | + Array.equals |
| 5817 | +
|
| 5818 | + Notes |
| 5819 | + ----- |
| 5820 | + If the following equation is element-wise True, then `allclose` returns True. |
| 5821 | +
|
| 5822 | + absolute(array1 - array2) <= (atol + rtol * absolute(array2)) |
| 5823 | +
|
| 5824 | + The above equation is not symmetric in array1 and array2, so that array1.allclose(array2) might be different |
| 5825 | + from array2.allclose(array1) in some rare cases. |
| 5826 | +
|
| 5827 | + Examples |
| 5828 | + -------- |
| 5829 | + >>> arr1 = Array([1e10, 1e-7], "a=a0,a1") |
| 5830 | + >>> arr2 = Array([1.00001e10, 1e-8], "a=a0,a1") |
| 5831 | + >>> arr1.allclose(arr2) |
| 5832 | + False |
| 5833 | +
|
| 5834 | + >>> arr1 = Array([1e10, 1e-8], "a=a0,a1") |
| 5835 | + >>> arr2 = Array([1.00001e10, 1e-9], "a=a0,a1") |
| 5836 | + >>> arr1.allclose(arr2) |
| 5837 | + True |
| 5838 | +
|
| 5839 | + >>> arr1 = Array([1e10, 1e-8], "a=a0,a1") |
| 5840 | + >>> arr2 = Array([1.0001e10, 1e-9], "a=a0,a1") |
| 5841 | + >>> arr1.allclose(arr2) |
| 5842 | + False |
| 5843 | +
|
| 5844 | + >>> arr1 = Array([1.0, nan], "a=a0,a1") |
| 5845 | + >>> arr2 = Array([1.0, nan], "a=a0,a1") |
| 5846 | + >>> arr1.allclose(arr2) |
| 5847 | + True |
| 5848 | + >>> arr1.allclose(arr2, nans_equal=False) |
| 5849 | + False |
| 5850 | + """ |
| 5851 | + return self.equals(other=other, rtol=rtol, atol=atol, nans_equal=nans_equal, check_axes=check_axes) |
| 5852 | + |
5778 | 5853 | @deprecate_kwarg('nan_equals', 'nans_equal')
|
5779 | 5854 | def eq(self, other, rtol=0, atol=0, nans_equal=False):
|
5780 | 5855 | """
|
@@ -5803,7 +5878,7 @@ def eq(self, other, rtol=0, atol=0, nans_equal=False):
|
5803 | 5878 |
|
5804 | 5879 | See Also
|
5805 | 5880 | --------
|
5806 |
| - Array.equals |
| 5881 | + Array.equals, Array.isclose |
5807 | 5882 |
|
5808 | 5883 | Notes
|
5809 | 5884 | -----
|
|
0 commit comments