Python Forum
Python Anytree - Is not of type 'NodeMixin' error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Anytree - Is not of type 'NodeMixin' error
#1
I am trying to build a Hierarchy from the below csv data:

Child,Parent,IsActive
11074165,11018432,NO
11094122,11033371,NO
11020499,11018277,NO
11018432,11020499,NO

Below is the code snippet

nodes = {}
with open("SampleHierarchy.csv", "r") as f:
    csv_reader = csv.DictReader(f)
    for row in csv_reader:
        #print(row['Code'], row['Parent'],row['IsActive'])
        childItem=row['Code']
        parentItem=row['Parent']
        nodes[childItem] = Node(childItem, parent=parentItem)
But getting the Error:

Error:
*Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile execfile(filename, namespace) File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "CounterpartyHierarchyTree.py", line 37, in <module> Node(childItem, parent=parentItem) File "C:\ProgramData\Anaconda3\lib\site-packages\anytree\node\node.py", line 77, in __init__ self.parent = parent File "C:\ProgramData\Anaconda3\lib\site-packages\anytree\node\nodemixin.py", line 127, in parent raise TreeError(msg) anytree.node.exceptions.TreeError: Parent node '11018432' is not of type 'NodeMixin'.*
Larz60+ write May-05-2022, 10:36 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use BBCode tags on future posts.
Reply
#2
With your code, parentItem is a string read in the csv file. But the Node() constructor obviously expects parent to have a subtype of NodeMixin. It is probable that None is accepted too as parent node.

I suggest sorting the nodes topologically before instanciating the Nodes, unless you can update the parent node after creation, in which case you could create all the nodes without parent and update their parents afterwards.
Reply
#3
(May-05-2022, 11:55 AM)Gribouillis Wrote: With your code, parentItem is a string read in the csv file. But the Node() constructor obviously expects parent to have a subtype of NodeMixin. It is probable that None is accepted too as parent node.

I suggest sorting the nodes topologically before instanciating the Nodes, unless you can update the parent node after creation, in which case you could create all the nodes without parent and update their parents afterwards.

Thanks for the update .Will sort the data as you mentioned.
On Node() constructor expectation - will you be able to share few lines of code that will handle the issue

Thanks
Reply
#4
georgebijum Wrote:will you be able to share few lines of code that will handle the issue

The anytree documentation about the «parent» attribute suggests that you can set the attribute after creation, so I suggest something like

nodes = {}
with open("SampleHierarchy.csv", "r") as f:
    # creation step
    csv_reader = csv.DictReader(f)
    for row in csv_reader:
        for item in (row['Code'], row['Parent']):
            if item and (item not in nodes):
                nodes[item] = Node(item)
    # parent setting step
    f.seek(0)
    csv_reader = csv.DictReader(f)
    for row in csv_reader:
        c, p = row['Code'], row['Parent']
        if c and p:
            nodes[c].parent = nodes[p]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Wrong type error rowan_bradley 6 1,231 Aug-07-2023, 10:44 AM
Last Post: rowan_bradley
  Type Error: Unsupported Operand jhancock 2 1,205 Jul-22-2023, 11:33 PM
Last Post: jhancock
  Incorrect Type Error milkycow 4 2,916 Jun-25-2021, 06:04 AM
Last Post: milkycow
Star Type Error: 'in' object is not callable nman52 3 3,418 May-01-2021, 11:03 PM
Last Post: nman52
  Error : "can't multiply sequence by non-int of type 'float' " Ala 3 3,098 Apr-13-2021, 10:33 AM
Last Post: deanhystad
  Type Error in Python MarcusB 3 2,579 Mar-30-2021, 06:34 PM
Last Post: buran
  unsupported operand type(s) for /: 'str' and 'int' Error for boxplot soft 1 3,071 Feb-09-2021, 05:40 PM
Last Post: soft
  Type Error or Value Error? spalisetty06 3 2,402 Jul-21-2020, 04:56 AM
Last Post: deanhystad
  anytree implicit node? gw1500se 0 1,715 Jun-07-2020, 08:42 PM
Last Post: gw1500se
  Modifying anytree Nodes gw1500se 1 2,658 Jun-05-2020, 03:44 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020