def test_order_columns_dict(): d = {"c": [1, 2, 3], "b": [4, 5, 6], "a": [7, 8, 9]} expected = OrderedDict(a=[7, 8, 9], b=[4, 5, 6], c=[1, 2, 3]) output = r.order_columns(d) assert output == expected assert list(output.keys()) == list(expected.keys()) assert isinstance(output, type(expected))
def test__check_ordered_columns(caplog): from mllaunchpad.resource import order_columns dummy_config = {"model": {}} mock_wrapper = MockModelClass() ma._check_ordered_columns(dummy_config, mock_wrapper, "never_ordered") assert "never_ordered".lower() not in caplog.text.lower() mock_wrapper.have_columns_been_ordered = True ma._check_ordered_columns(dummy_config, mock_wrapper, "ordered_only_in_train") assert "ordered_only_in_train does not call".lower() in caplog.text.lower() order_columns({"a": 1}) ma._check_ordered_columns(dummy_config, mock_wrapper, "ordered_in_train_and_now") assert "ordered_in_train_and_now".lower() not in caplog.text.lower()
def test_order_columns_np(): a = np.array( [(1, 4, 7), (2, 5, 8), (3, 6, 9)], dtype=[("c", "i4"), ("b", "i4"), ("a", "i4")], ) expected = np.array( [(7, 4, 1), (8, 5, 2), (9, 6, 3)], dtype=[("a", "i4"), ("b", "i4"), ("c", "i4")], ) # ordinary structured array output = r.order_columns(a) pd.testing.assert_frame_equal(pd.DataFrame(output), pd.DataFrame(expected)) # record array a_r = np.rec.array(a) expected_r = np.rec.array(expected) output_r = r.order_columns(a_r) pd.testing.assert_frame_equal(pd.DataFrame(output_r), pd.DataFrame(expected_r))
def test_order_columns_unsupported(): with pytest.raises(TypeError): r.order_columns(["Hello", "there"])
def test_order_columns_np_not_structured(): a = np.array([(1, 4, 7), (2, 5, 8), (3, 6, 9)]) with pytest.raises(TypeError): r.order_columns(a)
def test_order_columns_df(): df = pd.DataFrame(OrderedDict(c=[1, 2, 3], b=[4, 5, 6], a=[7, 8, 9])) expected = pd.DataFrame(OrderedDict(a=[7, 8, 9], b=[4, 5, 6], c=[1, 2, 3])) output = r.order_columns(df) pd.testing.assert_frame_equal(output, expected)