def test_unordered_categorical_disallowed(self): """Test which verifies rt.nanmax raises an exception if called with an unordered Categorical.""" # Create an unordered Categorical. cat = rt.Categorical(["PA", "NY", "NY", "AL", "LA", "PA", "CA", "IL", "IL", "FL", "FL", "LA"], ordered=False) assert not cat.ordered with pytest.raises(ValueError): rt.nanmax(cat)
def test_nan_awareness(self, arr, func_type): """ Check how :func:`rt.nanmax` handles NaN values by comparing it against :func:`rt.max`. Call :func:`rt.nanmax` with an array, then remove any NaNs from the array and call :func:`np.max` with the 'clean' array. The results should match. """ # Get the function implementation based on how we want to call it. if func_type == 'module': test_func = lambda x: rt.nanmax(x) elif func_type == 'member': test_func = lambda x: x.nanmax() else: raise ValueError( f"Unhandled value '{func_type}' specified for the function type." ) # Get the nan-unaware version of the function. nan_unaware_func = lambda x: rt.max(x) # Wrap the input as a FastArray to ensure we'll get the riptable implementation of the function. arr = rt.FA(arr) # Call the test implementation. NanAwareTestImpl.test_nan_awareness(test_func, nan_unaware_func, arr)
def test_allnans(self, arg): # Call rt.nanmax with the test input. # It should raise a RuntimeWarning when given an input which # has all NaNs **on the specified axis**. with pytest.warns(RuntimeWarning): result = rt.nanmax(arg) # If given a scalar or 1D array (or some collection converted to such) # the result should be a NaN; for higher-rank arrays, the result should # be an array where one of the dimensions was collapsed and if there were # all NaNs along the selected axis there'll be a NaN there in the result. # TODO: Need to fix this to assert correctly for when rt.nanmax called with a higher-rank array. assert rt.isnan(result)
def test_ordered_categorical_returns_scalar(self): """ Test which verifies rt.nanmax returns a scalar (Python object or numpy scalar) representing the max Category given an ordered Categorical. """ # Create an ordered Categorical (aka 'ordinal'). cat = rt.Categorical( ["PA", "NY", "", "NY", "AL", "LA", "PA", "", "CA", "IL", "IL", "FL", "FL", "LA"], ordered=True) assert cat.ordered result = rt.nanmax(cat) # The result should either be a Python string, a numpy string scalar, or a Categorical scalar (if we implement one). is_py_str = isinstance(result, (bytes, str)) is_np_scalar = isinstance(result, np.str) is_rt_cat = isinstance(result, rt.Categorical) assert is_py_str or is_np_scalar or is_rt_cat # Check the result is correct. assert result == "FL"
def test_empty(self, arg): # Call rt.nanmax with an empty input -- it should raise a ValueError. with pytest.raises(ValueError): rt.nanmax(arg)