1

First Question on StackOverflow. Haven't been able to find other instances of this problem which leads me to believe I'm doing something incorrectly.

I'm attempting to create a tree from Bill of Material (BOM) data. To start with I am only trying to get basic parent>child nodes set up.

from anytree import Node, RenderTree, find_by_attr, NodeMixin
import pandas as pd

# Create Dataframe from csv file
df1 = pd.read_csv(r'filepath') # filepath only used for example


nodes = {} # Store created nodes

root = Node('root') # Setup root node

# Iterate over df and create parent/child nodes
for index, row in df1.iterrows():
    pitem = row['EBPART'].strip()  # get parent item number
    citem = row['EBCOMP'].strip() # get child item number
    nodes[pitem] = Node(pitem,parent=root) # create child node
    nodes[citem] = Node(citem,parent=pitem) # create parent node

Basically during iteration I'm attempting to create the parent node (which does not throw an error) and then create a child node using the previously created node as it's parent. But, I continually get this error:

anytree.node.exceptions.TreeError: Parent node 'Product1' is not of type 'NodeMixin'.

Here is the result of rendertree(root):

print(RenderTree(root))
Node('/root')
└── Node('/root/322601101')

Here is a simplified version of the csv I'm reading:

Sample csv structure

Any help is greatly appreciated!

shreve57
  • 11
  • 2

2 Answers2

0

From this Github Issue it seems you need to explicitly set parent=None in the root node. And also it seems AnyNode expects **kwargs. (I haven't read the documentation so I cant concur) Therefore you should also modify your pitem and citem.

Ahmet
  • 434
  • 4
  • 13
  • Thanks for the reply! I should have mentioned I had tried explicitly setting the parent to None previously. It did not seem to make a difference but I have left in in just in case. If it's not too much trouble could you elaborate on what you mean about modifying the pitem and citem? I've been attempting to use the docs (linked below) and create my nodes using the same manner. I've attempted using 'name' and 'id' kwargs on node creation but am still getting the same error. It's possible I've misunderstood though. https://rb.gy/uobewi – shreve57 Jul 04 '20 at 18:26
0

Following line looks good:

Where the node.name: str and node.parent: Node

nodes[pitem] = Node(pitem,parent=root) # create child node

This one not:

nodes[citem] = Node(citem,parent=pitem)

Here node.name: str but node.parent: str, and should be of type Node

Additionally setting node.parent = None, or not is exactly the same. Same happens with **kwargs, it's just additional attributes you can set to your node, and has nothing to do with the error you are facing.

In deed you need to declare the root Node first, but you are doing that, the error is not because of that