def test_topn_big_N(self): """ When calling topn where N is greater than the number of non-nan values. This can happen if you're tracking a Frame of returns where not all series start at the same time. It's possible that in the begining or end, or anytime for that matter, you might not have enough values. This screws up the logic. """ # test data arr = np.random.randn(100) arr[5:] = np.nan # only first four are non-na s = pd.Series(arr) # top bn_res = topper.bn_topn(arr, 10) assert bn_res[0] == max(arr) # sanity check pd_res = s.order(ascending=False)[:10].dropna() tm.assert_almost_equal(bn_res, pd_res.values) # bottom bn_res = topper.bn_topn(arr, -10) assert bn_res[0] == min(arr) # sanity check pd_res = s.order()[:10].dropna() # grab from end since we reversed tm.assert_almost_equal(bn_res, pd_res.values)
def test_default_ascending(self): """ Changed ascending to change based on N More intuitive, by default you'd expect the greatest or lowest value would be first, depending on which side you are looking for """ # top should default to asc=False bn_res = topper.bn_topn(arr, 10) pd_res = s.order(ascending=False)[:10] tm.assert_almost_equal(bn_res, pd_res.values) # make sure ascending is still respected bn_res = topper.bn_topn(arr, 10, ascending=True) pd_res = s.order(ascending=True)[-10:] tm.assert_almost_equal(bn_res, pd_res.values) # bottom defaults asc=True bn_res = topper.bn_topn(arr, -10) pd_res = s.order()[:10] tm.assert_almost_equal(bn_res, pd_res.values) # make sure ascending is still respected bn_res = topper.bn_topn(arr, -10, ascending=False) pd_res = s.order()[:10][::-1] tm.assert_almost_equal(bn_res, pd_res.values)
def test_topn_big_N(self): """ When calling topn where N is greater than the number of non-nan values. This can happen if you're tracking a Frame of returns where not all series start at the same time. It's possible that in the begining or end, or anytime for that matter, you might not have enough values. This screws up the logic. """ # test data arr = np.random.randn(100) arr[5:] = np.nan # only first four are non-na s = pd.Series(arr) # top bn_res = topper.bn_topn(arr, 10) assert bn_res[0] == max(arr) # sanity check pd_res = s.order(ascending=False)[:10].dropna() tm.assert_almost_equal(bn_res, pd_res) # bottom bn_res = topper.bn_topn(arr, -10) assert bn_res[0] == min(arr) # sanity check pd_res = s.order()[:10].dropna() # grab from end since we reversed tm.assert_almost_equal(bn_res, pd_res)
def test_default_ascending(self): """ Changed ascending to change based on N More intuitive, by default you'd expect the greatest or lowest value would be first, depending on which side you are looking for """ # top should default to asc=False bn_res = topper.bn_topn(arr, 10) pd_res = s.order(ascending=False)[:10] tm.assert_almost_equal(bn_res, pd_res) # make sure ascending is still respected bn_res = topper.bn_topn(arr, 10, ascending=True) pd_res = s.order(ascending=True)[-10:] tm.assert_almost_equal(bn_res, pd_res) # bottom defaults asc=True bn_res = topper.bn_topn(arr, -10) pd_res = s.order()[:10] tm.assert_almost_equal(bn_res, pd_res) # make sure ascending is still respected bn_res = topper.bn_topn(arr, -10, ascending=False) pd_res = s.order()[:10][::-1] tm.assert_almost_equal(bn_res, pd_res)
def test_top_smallest(self): # get the nsmallest bn_res = topper.bn_topn(arr, -10) assert bn_res[0] == min(arr) # sanity check pd_res = s.order()[:10] tm.assert_almost_equal(bn_res, pd_res) # change ordering bn_res = topper.bn_topn(arr, -10, ascending=False) assert bn_res[-1] == min(arr) # sanity check pd_res = s.order(ascending=False)[-10:] # grab from end since we reversed tm.assert_almost_equal(bn_res, pd_res)
def test_topn_largest(self): # get the n largest bn_res = topper.bn_topn(arr, 10) assert bn_res[0] == max(arr) # sanity check pd_res = s.order(ascending=False)[:10] tm.assert_almost_equal(bn_res, pd_res) # change result to biggest to smallest bn_res = topper.bn_topn(arr, 10, ascending=True) assert bn_res[-1] == max(arr) # sanity check pd_res = s.order(ascending=True)[-10:] # grab from end since we reversed tm.assert_almost_equal(bn_res, pd_res)
def test_top_smallest(self): # get the nsmallest bn_res = topper.bn_topn(arr, -10) assert bn_res[0] == min(arr) # sanity check pd_res = s.order()[:10] tm.assert_almost_equal(bn_res, pd_res.values) # change ordering bn_res = topper.bn_topn(arr, -10, ascending=False) assert bn_res[-1] == min(arr) # sanity check pd_res = s.order( ascending=False)[-10:] # grab from end since we reversed tm.assert_almost_equal(bn_res, pd_res.values)
def test_topn_largest(self): # get the n largest bn_res = topper.bn_topn(arr, 10) assert bn_res[0] == max(arr) # sanity check pd_res = s.order(ascending=False)[:10] np.testing.assert_almost_equal(bn_res, pd_res) # change result to biggest to smallest bn_res = topper.bn_topn(arr, 10, ascending=True) assert bn_res[-1] == max(arr) # sanity check pd_res = s.order( ascending=True)[-10:] # grab from end since we reversed np.testing.assert_almost_equal(bn_res, pd_res)
def test_top_arg(self): # get the nlargest bn_res = topper.bn_topn(arr, 10) bn_args = topper.bn_topargn(arr, 10) arg_res = arr[bn_args] tm.assert_almost_equal(bn_res, arg_res) # get the nsmallest bn_res = topper.bn_topn(arr, -10) bn_args = topper.bn_topargn(arr, -10) arg_res = arr[bn_args] tm.assert_almost_equal(bn_res, arg_res) # get the nsmallest bn_res = topper.bn_topn(arr, -10, ascending=False) bn_args = topper.bn_topargn(arr, -10, ascending=False) arg_res = arr[bn_args] tm.assert_almost_equal(bn_res, arg_res)