diff --git a/CHANGELOG.md b/CHANGELOG.md index cdd9c70e..5a205454 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Tree Exporter: `tree_to_dot` to handle cases when not all nodes have `edge_attr`. - DAG Exporter: `dag_to_dot` to perform dictionary copy to prevent style from being overridden for child nodes. +- Tree Constructor: `dataframe_to_tree` to handle case when path column is not the first column. ## [0.9.3] - 2023-05-28 ### Changed diff --git a/bigtree/tree/construct.py b/bigtree/tree/construct.py index a6e99344..728c267c 100644 --- a/bigtree/tree/construct.py +++ b/bigtree/tree/construct.py @@ -803,6 +803,8 @@ def dataframe_to_tree( add_dataframe_to_tree_by_path( root_node, data, + path_col=path_col, + attribute_cols=attribute_cols, sep=sep, duplicate_name_allowed=duplicate_name_allowed, ) diff --git a/tests/tree/test_construct.py b/tests/tree/test_construct.py index 09b971bb..0f23553a 100644 --- a/tests/tree/test_construct.py +++ b/tests/tree/test_construct.py @@ -529,6 +529,17 @@ def test_add_dataframe_to_tree_by_path_col_name(self): assert_tree_structure_basenode_root_attr(self.root) assert_tree_structure_node_root(self.root) + def test_add_dataframe_to_tree_by_path_col_name_reverse(self): + add_dataframe_to_tree_by_path( + self.root, + self.data[["age", "PATH"]], + path_col="PATH", + attribute_cols=["age"], + ) + assert_tree_structure_basenode_root(self.root) + assert_tree_structure_basenode_root_attr(self.root) + assert_tree_structure_node_root(self.root) + def test_add_dataframe_to_tree_by_path_empty_error(self): with pytest.raises(ValueError) as exc_info: add_dataframe_to_tree_by_path(self.root, pd.DataFrame()) @@ -804,6 +815,17 @@ def test_add_dataframe_to_tree_by_name_col_name(self): assert_tree_structure_basenode_root_attr(root) assert_tree_structure_node_root(root) + def test_add_dataframe_to_tree_by_name_col_name_reverse(self): + root = add_dataframe_to_tree_by_name( + self.root, + self.data[["age", "NAME"]], + name_col="NAME", + attribute_cols=["age"], + ) + assert_tree_structure_basenode_root(root) + assert_tree_structure_basenode_root_attr(root) + assert_tree_structure_node_root(root) + def test_add_dataframe_to_tree_by_name_empty_error(self): with pytest.raises(ValueError) as exc_info: add_dataframe_to_tree_by_name(self.root, pd.DataFrame()) @@ -1524,6 +1546,12 @@ def test_dataframe_to_tree_col_name(self): assert_tree_structure_basenode_root_attr(root) assert_tree_structure_node_root(root) + def test_dataframe_to_tree_col_name_reverse(self): + root = dataframe_to_tree(self.path_data[["age", "PATH"]], path_col="PATH") + assert_tree_structure_basenode_root(root) + assert_tree_structure_basenode_root_attr(root) + assert_tree_structure_node_root(root) + @staticmethod def test_dataframe_to_tree_no_attribute(): path_data = pd.DataFrame( @@ -1775,6 +1803,17 @@ def test_dataframe_to_tree_by_relation_col_name(self): assert_tree_structure_basenode_root_attr(root) assert_tree_structure_node_root(root) + def test_dataframe_to_tree_by_relation_col_name_reverse(self): + root = dataframe_to_tree_by_relation( + self.relation_data[["age", "parent", "child"]], + child_col="child", + parent_col="parent", + attribute_cols=["age"], + ) + assert_tree_structure_basenode_root(root) + assert_tree_structure_basenode_root_attr(root) + assert_tree_structure_node_root(root) + def test_dataframe_to_tree_by_relation_empty_row_error(self): relation_data = pd.DataFrame(columns=["child", "parent"]) with pytest.raises(ValueError) as exc_info: