Skip to content

Commit

Permalink
Fixed: dataframe_to_tree method from issue 58
Browse files Browse the repository at this point in the history
  • Loading branch information
kayjan committed Jun 17, 2023
1 parent 6f97722 commit 47190d9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions bigtree/tree/construct.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
39 changes: 39 additions & 0 deletions tests/tree/test_construct.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 47190d9

Please sign in to comment.