From 092f41e32622a18d0a358702f9cf81be33f8ec6e Mon Sep 17 00:00:00 2001 From: Kay Date: Tue, 8 Nov 2022 08:58:31 +0800 Subject: [PATCH] v0.4.4 handle null in array when adding list-like attributes --- CHANGELOG.md | 5 +++++ bigtree/tree/construct.py | 2 +- docs/source/bigtree/node/dagnode.rst | 6 ++++++ docs/source/node.rst | 1 + tests/tree/test_construct.py | 20 +++++++++++++++++++- 5 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 docs/source/bigtree/node/dagnode.rst diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b3919d7..d7d4ec9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Work In Progress - Node: WeightedNode for weighted edge tree implementation. +## [0.4.4] - 2022-11-08 +### Fixed +- Tree Constructors: Handle adding attributes that are array-like - add array even when one of the items is null + ## [0.4.3] - 2022-11-08 ### Added - Node: Print format for BaseNode. @@ -67,6 +71,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Utility Iterators: Tree traversal methods. - Workflow To Do App: Tree use case with to-do list implementation. +[0.4.3]: https://github.com/kayjan/bigtree/compare/v0.4.3...v0.4.4 [0.4.3]: https://github.com/kayjan/bigtree/compare/v0.4.2...v0.4.3 [0.4.2]: https://github.com/kayjan/bigtree/compare/v0.4.1...v0.4.2 [0.4.1]: https://github.com/kayjan/bigtree/compare/v0.4.0...v0.4.1 diff --git a/bigtree/tree/construct.py b/bigtree/tree/construct.py index fe2f5ef9..2a2663fc 100644 --- a/bigtree/tree/construct.py +++ b/bigtree/tree/construct.py @@ -300,7 +300,7 @@ def add_dataframe_to_tree_by_path( for row in data.to_dict(orient="index").values(): node_attrs = row.copy() del node_attrs[path_col] - node_attrs = {k: v for k, v in node_attrs.items() if not np.any(pd.isnull(v))} + node_attrs = {k: v for k, v in node_attrs.items() if not np.all(pd.isnull(v))} add_path_to_tree( tree_root, row[path_col], diff --git a/docs/source/bigtree/node/dagnode.rst b/docs/source/bigtree/node/dagnode.rst new file mode 100644 index 00000000..929d3d9f --- /dev/null +++ b/docs/source/bigtree/node/dagnode.rst @@ -0,0 +1,6 @@ +DAGNode +===================== + +.. automodule:: bigtree.node.dagnode + :members: + :show-inheritance: diff --git a/docs/source/node.rst b/docs/source/node.rst index 2724c5ce..bbe5b6fa 100644 --- a/docs/source/node.rst +++ b/docs/source/node.rst @@ -6,3 +6,4 @@ Node bigtree/node/basenode bigtree/node/node + bigtree/node/dagnode diff --git a/tests/tree/test_construct.py b/tests/tree/test_construct.py index 7baa07ce..7817ae45 100644 --- a/tests/tree/test_construct.py +++ b/tests/tree/test_construct.py @@ -3,7 +3,6 @@ import pandas as pd import pytest -from bigtree import find_names from bigtree.node.node import Node from bigtree.tree.construct import ( add_dataframe_to_tree_by_name, @@ -17,6 +16,7 @@ list_to_tree_tuples, nested_dict_to_tree, ) +from bigtree.tree.search import find_name, find_names from bigtree.utils.exceptions import DuplicatedNodeError, TreeError from tests.node.test_basenode import ( assert_tree_structure_basenode_root_attr, @@ -362,6 +362,24 @@ def test_add_dict_to_tree_by_name(self): assert_tree_structure_basenode_root_attr(root) assert_tree_structure_node_root_generic(root) + def test_add_dict_to_tree_by_name_different_dtype(self): + name_dict = { + "a": {"random": [1]}, + "b": {"random": [1, 2]}, + "c": {"random": [1, None]}, + "d": {"random": [None]}, + "e": {"random": None}, + "f": {"random": 0}, + "g": {"random": -1}, + "h": {"random": [-1]}, + } + root = add_dict_to_tree_by_name(self.root, name_dict) + nodes = ["a", "b", "c", "d", "e", "f", "g", "h"] + expected_list = [[1], [1, 2], [1, None], None, None, 0, -1, [-1]] + for node_name, expected in zip(nodes, expected_list): + actual = find_name(root, node_name).get_attr("random") + assert expected == actual, f"Expected\n{expected}\nReceived\n{actual}" + def test_add_dict_to_tree_by_name_empty(self): with pytest.raises(ValueError): add_dict_to_tree_by_name(self.root, {})