Exemplo n.º 1
0
    def test_accum_cols_multikey(self):
        num_rows = 12
        data = rt.Dataset({
            'Symb':
            rt.Cat(['A', 'B'] * int(num_rows / 2)),
            'Exch':
            rt.Cat(['X', 'Y', 'Y', 'X'] * int(num_rows / 4)),
            'Count':
            rt.full(num_rows, 1.0),
            'PlusMinus': [1.0, -1.0] * int(num_rows / 2),
        })
        data.MultiKeyCat = rt.Cat([data.Symb, data.Exch])

        accum = rt.accum_cols(data.MultiKeyCat, [data.Count, data.PlusMinus],
                              ['Count', 'PlusMinus'])
        accum_expected = rt.Dataset({
            'Symb': ['A', 'B', 'A', 'B'],
            'Exch': ['X', 'Y', 'Y', 'X'],
            'Count': [3.0, 3.0, 3.0, 3.0],
            'PlusMinus': [3.0, -3.0, 3.0, -3.0],
        })
        accum_expected.footer_set_values('Total', {
            'Exch': 'Total',
            'Count': 12.0,
            'PlusMinus': 0.0
        })

        self.assertTrue((accum == accum_expected).all(axis=None))
Exemplo n.º 2
0
    def test_accum_cols_noncat(self):
        num_rows = 10
        pointer = rt.FA([0, 1] * int(num_rows / 2))
        count = rt.full(num_rows, 1.0)

        accum = rt.accum_cols(pointer, count)
        accum_expected = rt.Dataset({'YLabel': [0, 1], 'col0': [5.0, 5.0]})
        accum_expected.footer_set_values('Total', {
            'YLabel': 'Total',
            'col0': 10.0
        })

        self.assertTrue((accum == accum_expected).all(axis=None))
Exemplo n.º 3
0
    def test_accum_cols(self):

        num_rows = 10
        data = rt.Dataset({
            'Symb': rt.Cat(['A', 'B'] * int(num_rows / 2)),
            'Count': rt.full(num_rows, 1.0),
            'PlusMinus': [1.0, -1.0] *
            int(num_rows / 2),  # Added to handle edge case of zero footer
        })

        accum = rt.accum_cols(data.Symb, [data.Count, data.PlusMinus],
                              ['Count', 'PlusMinus'])
        accum_expected = rt.Dataset({
            'Symb': ['A', 'B'],
            'Count': [5.0, 5.0],
            'PlusMinus': [5.0, -5.0]
        })
        accum_expected.footer_set_values('Total', {
            'Symb': 'Total',
            'Count': 10.0,
            'PlusMinus': 0.0
        })
        self.assertTrue((accum == accum_expected).all(axis=None))
Exemplo n.º 4
0
class TestNanmin:
    """Tests for the rt.nanmin function."""

    @pytest.mark.parametrize(
        "arg",
        [
            pytest.param(math.nan, id='scalar'),
            pytest.param([math.nan, math.nan, math.nan, math.nan, math.nan], id='list-float'),
            pytest.param({np.nan}, id='set-float', marks=pytest.mark.skip("Broken in both numpy and riptable.")),
            pytest.param(np.full(100, np.nan, dtype=np.float32), id='ndarray-float'),
            pytest.param(rt.full(100, np.nan, dtype=np.float32), id='FastArray-float')
            # TODO: Ordered Categorical with all invalids
        ]
    )
    def test_allnans(self, arg):
        # Call rt.nanmin 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.nanmin(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.nanmin called with a higher-rank array.
        assert rt.isnan(result)

    @pytest.mark.parametrize(
        "arg",
        [
            pytest.param([], id='list-float'),
            pytest.param({}, id='set-float', marks=pytest.mark.skip("Broken in both numpy and riptable.")),
            pytest.param(np.array([], dtype=np.float32), id='ndarray-float'),
            pytest.param(
                rt.FastArray([], dtype=np.float32), id='FastArray-float',
                marks=pytest.mark.xfail(
                    reason="RIP-417: The call to riptide_cpp via the ledger returns None, which then causes the isnan() to raise a TypeError. This needs to be fixed so we raise an error like numpy (either by checking for this and raising the exception, or fixing the way the function punts to numpy."))
            # TODO: Empty ordered Categorical (create with some categories but an empty backing array).
            # TODO: Empty Date array (representing a FastArray subclass)
        ]
    )
    def test_empty(self, arg):
        # Call rt.nanmin with an empty input -- it should raise a ValueError.
        with pytest.raises(ValueError):
            rt.nanmin(arg)

    def test_unordered_categorical_disallowed(self):
        """Test which verifies rt.nanmin 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.nanmin(cat)

    @pytest.mark.xfail(reason="RIP-417: nanmin does not (yet) support returning a category scalar.")
    def test_ordered_categorical_returns_scalar(self):
        """
        Test which verifies rt.nanmin returns a scalar (Python object or numpy scalar) representing the min 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.nanmin(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 == "PA"
Exemplo n.º 5
0
    def test_accum_ratiop(self):

        num_rows = 12
        data = rt.Dataset({
            'Symb':
            rt.Cat(['A', 'A', 'A', 'B'] * int(num_rows / 4)),
            'Exch':
            rt.Cat(['Z', 'Z', 'X', 'X'] * int(num_rows / 4)),
            'Count':
            rt.full(num_rows, 1.0),
        })

        # Invalid input
        with self.assertRaises(
                ValueError,
                msg=f'Failed to raise an error when passing invalid norm_by arg'
        ):
            rt.accum_ratiop(data.Symb, data.Exch, data.Count, norm_by='z')

        # Ratio within total
        accum = rt.accum_ratiop(data.Symb, data.Exch, data.Count, norm_by='T')
        accum_expected = rt.Dataset({
            'Symb': ['A', 'B'],
            'X': [25.0, 25.0],
            'Z': [50.0, 0.0],
            'TotalRatio': [75.0, 25.0],
            'Total': [9.0, 3.0],
        })
        accum_expected.footer_set_values(
            'TotalRatio',
            {
                'Symb': 'TotalRatio',
                'X': 50.0,
                'Z': 50.0,
                'TotalRatio': 100.0
            },
        )
        accum_expected.footer_set_values('Total', {
            'Symb': 'Total',
            'X': 6.0,
            'Z': 6.0,
            'Total': 12.0
        })
        self.assertTrue((accum == accum_expected).all(axis=None))

        # Ratio within columns
        accum = rt.accum_ratiop(data.Symb, data.Exch, data.Count, norm_by='c')
        accum_expected = rt.Dataset({
            'Symb': ['A', 'B'],
            'X': [50.0, 50.0],
            'Z': [100.0, 0.0],
            'TotalRatio': [75.0, 25.0],
            'Total': [9.0, 3.0],
        })
        accum_expected.footer_set_values(
            'TotalRatio',
            {
                'Symb': 'TotalRatio',
                'X': 100.0,
                'Z': 100.0,
                'TotalRatio': 100.0
            },
        )
        accum_expected.footer_set_values('Total', {
            'Symb': 'Total',
            'X': 6.0,
            'Z': 6.0,
            'Total': 12.0
        })
        self.assertTrue((accum == accum_expected).all(axis=None))