question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Testing NaN does not work

See original GitHub issue

I have a numpy array that I have to test with inside a NaN that has to be there. Version 2.2.0 works, while version 2.3.0 does not work.

output of 2.2.0:

bertossa@pippo:~/analisi/tests$ pytest -sv . -k gk
WARNING: cannot import thermocepstrum
cite as:
Riccardo Bertossa, analisi
https://github.com/rikigigi/analisi
(c) 2017-2021
=========
COMPILED AT Jan 17 2022 14:47:14 by /usr/bin/c++ whith flags (Release) -O3 -DNDEBUG -O3 (Debug)  -g -DEIGEN_INITIALIZE_MATRICES_BY_NAN    -fsanitize=leak -fsanitize=undefined (build type was Release) on a Linux-5.4.0-94-generic whith processor x86_64
With python support: /usr/lib/x86_64-linux-gnu/libpython3.8.so
With gromacs XDR file conversion support
v0.3.4
============================================================================ test session starts ============================================================================
platform linux -- Python 3.8.10, pytest-6.2.5, py-1.11.0, pluggy-0.13.0 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /home/bertossa/analisi/tests
plugins: datadir-1.3.1, regressions-2.2.0
collected 6 items / 5 deselected / 1 selected                                                                                                                               

test_gk.py::test_gk [[ 1.15338509e+05 -3.54402664e+01 -3.54402664e+01 ...  0.00000000e+00
   0.00000000e+00  0.00000000e+00]
 [ 1.13409212e+05 -3.51673102e+01 -3.49856870e+01 ...  3.53293021e-02
   7.91862554e+04             nan]
 [ 1.07866235e+05 -3.41664742e+01 -3.38088613e+01 ...  5.21211914e-02
   1.55216495e+05  3.95931277e+04]
 ...
 [-2.73309429e+03 -2.66983885e-01  1.39421901e+00 ... -2.67101979e-02
   1.62004622e+05  5.34430500e+05]
 [-2.58976152e+03 -1.40244210e-01  1.24827661e+00 ... -2.65180986e-02
   1.59872994e+05  5.34257456e+05]
 [-2.37993149e+03 -2.79107527e-02  1.07349726e+00 ... -2.63087491e-02
   1.57897803e+05  5.34083800e+05]]
PASSED

============================================================================= warnings summary ==============================================================================
../../../../usr/lib/python3/dist-packages/ipywidgets/widgets/widget_selection.py:9
  /usr/lib/python3/dist-packages/ipywidgets/widgets/widget_selection.py:9: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.10 it will stop working
    from collections import Mapping

../../../../usr/lib/python3/dist-packages/ipywidgets/widgets/interaction.py:29
  /usr/lib/python3/dist-packages/ipywidgets/widgets/interaction.py:29: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.10 it will stop working
    from collections import Iterable, Mapping

../../../../usr/lib/python3/dist-packages/ipywidgets/widgets/widget_link.py:19
../../../../usr/lib/python3/dist-packages/ipywidgets/widgets/widget_link.py:19
  /usr/lib/python3/dist-packages/ipywidgets/widgets/widget_link.py:19: DeprecationWarning: Traits should be given as instances, not types (for example, `Int()`, not `Int`) Passing types is deprecated in traitlets 4.1.
    super(WidgetTraitTuple, self).__init__(Instance(Widget), Unicode, **kwargs)

test_gk.py::test_gk
test_gk.py::test_gk
  /home/bertossa/.local/lib/python3.8/site-packages/pytest_regressions/dataframe_regression.py:126: DeprecationWarning: `np.float` is a deprecated alias for the builtin `float`. To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.float64` here.
  Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
    if data_type in [float, np.float, np.float16, np.float32, np.float64]:

-- Docs: https://docs.pytest.org/en/stable/warnings.html
================================================================ 1 passed, 5 deselected, 6 warnings in 1.66s ================================================================
bertossa@pippo:~/analisi/tests$ 


output of 2.3.0:

bertossa@pippo:~/analisi/tests$ pytest -sv . -k gk
WARNING: cannot import thermocepstrum
cite as:
Riccardo Bertossa, analisi
https://github.com/rikigigi/analisi
(c) 2017-2021
=========
COMPILED AT Jan 17 2022 14:47:14 by /usr/bin/c++ whith flags (Release) -O3 -DNDEBUG -O3 (Debug)  -g -DEIGEN_INITIALIZE_MATRICES_BY_NAN    -fsanitize=leak -fsanitize=undefined (build type was Release) on a Linux-5.4.0-94-generic whith processor x86_64
With python support: /usr/lib/x86_64-linux-gnu/libpython3.8.so
With gromacs XDR file conversion support
v0.3.4
============================================================================ test session starts ============================================================================
platform linux -- Python 3.8.10, pytest-6.2.5, py-1.11.0, pluggy-0.13.0 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /home/bertossa/analisi/tests
plugins: datadir-1.3.1, regressions-2.3.0
collected 6 items / 5 deselected / 1 selected                                                                                                                               

test_gk.py::test_gk [[ 1.15338509e+05 -3.54402664e+01 -3.54402664e+01 ...  0.00000000e+00
   0.00000000e+00  0.00000000e+00]
 [ 1.13409212e+05 -3.51673102e+01 -3.49856870e+01 ...  3.53293021e-02
   7.91862554e+04             nan]
 [ 1.07866235e+05 -3.41664742e+01 -3.38088613e+01 ...  5.21211914e-02
   1.55216495e+05  3.95931277e+04]
 ...
 [-2.73309429e+03 -2.66983885e-01  1.39421901e+00 ... -2.67101979e-02
   1.62004622e+05  5.34430500e+05]
 [-2.58976152e+03 -1.40244210e-01  1.24827661e+00 ... -2.65180986e-02
   1.59872994e+05  5.34257456e+05]
 [-2.37993149e+03 -2.79107527e-02  1.07349726e+00 ... -2.63087491e-02
   1.57897803e+05  5.34083800e+05]]
FAILED

================================================================================= FAILURES ==================================================================================
__________________________________________________________________________________ test_gk __________________________________________________________________________________

left = array(['115338.50895090569', '-35.440266364698509', '-35.440266364698509',
       ..., '-0.026308749054506145', '157897.80272819981',
       '534083.79967621446'], dtype=object)
right = array(['115338.50895090567', '-35.440266364698509', '-35.440266364698509',
       ..., '-0.02630874905450617', '157897.80272819955',
       '534083.79967621388'], dtype=object)
op = <built-in function sub>, is_cmp = False

    def _na_arithmetic_op(left, right, op, is_cmp: bool = False):
        """
        Return the result of evaluating op on the passed in values.
    
        If native types are not compatible, try coercion to object dtype.
    
        Parameters
        ----------
        left : np.ndarray
        right : np.ndarray or scalar
        is_cmp : bool, default False
            If this a comparison operation.
    
        Returns
        -------
        array-like
    
        Raises
        ------
        TypeError : invalid operation
        """
        if isinstance(right, str):
            # can never use numexpr
            func = op
        else:
            func = partial(expressions.evaluate, op)
    
        try:
>           result = func(left, right)

../../.local/lib/python3.8/site-packages/pandas/core/ops/array_ops.py:166: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

op = <built-in function sub>
a = array(['115338.50895090569', '-35.440266364698509', '-35.440266364698509',
       ..., '-0.026308749054506145', '157897.80272819981',
       '534083.79967621446'], dtype=object)
b = array(['115338.50895090567', '-35.440266364698509', '-35.440266364698509',
       ..., '-0.02630874905450617', '157897.80272819955',
       '534083.79967621388'], dtype=object)
use_numexpr = True

    def evaluate(op, a, b, use_numexpr: bool = True):
        """
        Evaluate and return the expression of the op on a and b.
    
        Parameters
        ----------
        op : the actual operand
        a : left operand
        b : right operand
        use_numexpr : bool, default True
            Whether to try to use numexpr.
        """
        op_str = _op_str_mapping[op]
        if op_str is not None:
            if use_numexpr:
                # error: "None" not callable
>               return _evaluate(op, op_str, a, b)  # type: ignore[misc]

../../.local/lib/python3.8/site-packages/pandas/core/computation/expressions.py:239: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

op = <built-in function sub>, op_str = '-'
a = array(['115338.50895090569', '-35.440266364698509', '-35.440266364698509',
       ..., '-0.026308749054506145', '157897.80272819981',
       '534083.79967621446'], dtype=object)
b = array(['115338.50895090567', '-35.440266364698509', '-35.440266364698509',
       ..., '-0.02630874905450617', '157897.80272819955',
       '534083.79967621388'], dtype=object)

    def _evaluate_standard(op, op_str, a, b):
        """
        Standard evaluation.
        """
        if _TEST_MODE:
            _store_test_result(False)
>       return op(a, b)
E       TypeError: unsupported operand type(s) for -: 'str' and 'str'

../../.local/lib/python3.8/site-packages/pandas/core/computation/expressions.py:69: TypeError

During handling of the above exception, another exception occurred:

analisi_log = <pyanalisi.pyanalisi.ReadLog object at 0x7fdb87a47470>, num_regression = <pytest_regressions.num_regression.NumericRegressionFixture object at 0x7fdb87a28130>

    def test_gk(analisi_log,num_regression):
        import pyanalisi
        gk=pyanalisi.GreenKubo(analisi_log,'',1,['c_flux[1]','c_vcm[1][1]'], False, 2000, 2,False,0,4,False,1,100)
        gk.reset(analisi_log.getNtimesteps()-2000)
        gk.calculate(0)
        m=np.array(gk,copy=False)
        print(m)
>       num_regression.check({'gk':m.flatten()})

test_gk.py:11: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
../../.local/lib/python3.8/site-packages/pandas/core/ops/common.py:69: in new_method
    return method(self, other)
../../.local/lib/python3.8/site-packages/pandas/core/arraylike.py:100: in __sub__
    return self._arith_method(other, operator.sub)
../../.local/lib/python3.8/site-packages/pandas/core/series.py:5526: in _arith_method
    result = ops.arithmetic_op(lvalues, rvalues, op)
../../.local/lib/python3.8/site-packages/pandas/core/ops/array_ops.py:224: in arithmetic_op
    res_values = _na_arithmetic_op(left, right, op)
../../.local/lib/python3.8/site-packages/pandas/core/ops/array_ops.py:173: in _na_arithmetic_op
    result = _masked_arith_op(left, right, op)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

x = array(['115338.50895090569', '-35.440266364698509', '-35.440266364698509',
       ..., '-0.026308749054506145', '157897.80272819981',
       '534083.79967621446'], dtype=object)
y = array(['115338.50895090567', '-35.440266364698509', '-35.440266364698509',
       ..., '-0.02630874905450617', '157897.80272819955',
       '534083.79967621388'], dtype=object)
op = <built-in function sub>

    def _masked_arith_op(x: np.ndarray, y, op):
        """
        If the given arithmetic operation fails, attempt it again on
        only the non-null elements of the input array(s).
    
        Parameters
        ----------
        x : np.ndarray
        y : np.ndarray, Series, Index
        op : binary operator
        """
        # For Series `x` is 1D so ravel() is a no-op; calling it anyway makes
        # the logic valid for both Series and DataFrame ops.
        xrav = x.ravel()
        assert isinstance(x, np.ndarray), type(x)
        if isinstance(y, np.ndarray):
            dtype = find_common_type([x.dtype, y.dtype])
            # error: Argument "dtype" to "empty" has incompatible type
            # "Union[dtype, ExtensionDtype]"; expected "Union[dtype, None, type,
            # _SupportsDtype, str, Tuple[Any, int], Tuple[Any, Union[int,
            # Sequence[int]]], List[Any], _DtypeDict, Tuple[Any, Any]]"
            result = np.empty(x.size, dtype=dtype)  # type: ignore[arg-type]
    
            if len(x) != len(y):
                raise ValueError(x.shape, y.shape)
            else:
                ymask = notna(y)
    
            # NB: ravel() is only safe since y is ndarray; for e.g. PeriodIndex
            #  we would get int64 dtype, see GH#19956
            yrav = y.ravel()
            mask = notna(xrav) & ymask.ravel()
    
            # See GH#5284, GH#5035, GH#19448 for historical reference
            if mask.any():
>               result[mask] = op(xrav[mask], yrav[mask])
E               TypeError: unsupported operand type(s) for -: 'str' and 'str'

../../.local/lib/python3.8/site-packages/pandas/core/ops/array_ops.py:112: TypeError
============================================================================= warnings summary ==============================================================================
../../../../usr/lib/python3/dist-packages/ipywidgets/widgets/widget_selection.py:9
  /usr/lib/python3/dist-packages/ipywidgets/widgets/widget_selection.py:9: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.10 it will stop working
    from collections import Mapping

../../../../usr/lib/python3/dist-packages/ipywidgets/widgets/interaction.py:29
  /usr/lib/python3/dist-packages/ipywidgets/widgets/interaction.py:29: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3, and in 3.10 it will stop working
    from collections import Iterable, Mapping

../../../../usr/lib/python3/dist-packages/ipywidgets/widgets/widget_link.py:19
../../../../usr/lib/python3/dist-packages/ipywidgets/widgets/widget_link.py:19
  /usr/lib/python3/dist-packages/ipywidgets/widgets/widget_link.py:19: DeprecationWarning: Traits should be given as instances, not types (for example, `Int()`, not `Int`) Passing types is deprecated in traitlets 4.1.
    super(WidgetTraitTuple, self).__init__(Instance(Widget), Unicode, **kwargs)

-- Docs: https://docs.pytest.org/en/stable/warnings.html
========================================================================== short test summary info ==========================================================================
FAILED test_gk.py::test_gk - TypeError: unsupported operand type(s) for -: 'str' and 'str'
================================================================ 1 failed, 5 deselected, 4 warnings in 1.94s ================================================================
bertossa@pippo:~/analisi/tests$ 


Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
rikigigicommented, Jan 18, 2022

@tarcisiofischer I confirm that on my side everything is working as expected. you are the fastest issue closer in the west 🥇

1reaction
tarcisiofischercommented, Jan 18, 2022

@rikigigi Do you think it would be too much work for you to grab my branch, install it and check if this solves your issue? I’m almost certain that it will, but would be nice to make sure before merging the fix.

FYI, running your example on https://github.com/rikigigi/regression is passing with my fix, on my machine.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I check for NaN values? - python
Here are three ways where you can test a variable is "NaN" or not. import pandas as pd import numpy as np import...
Read more >
5 Methods to Check for NaN values in in Python
NaN stands for Not A Number and is one of the common ways to represent the missing value in the data. It is...
Read more >
Python math.isnan() Method
The math.isnan() method checks whether a value is NaN (Not a Number), or not. This method returns True if the specified value is...
Read more >
The Problem with Testing for NaN in JavaScript
As a result, JavaScript developers often need to test a result variable to see whether it contains NaN or whether it is a...
Read more >
Check for NaN values in Python
A simple solution to check for a NaN in Python is using the mathematical function math.isnan() . It returns True if the specified...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found