title: KD Tree construction Pyhton
body: <p>I am trying to build a KD Tree in Python, I've created this class</p>
  <pre><code>class KD_Tree:
  
  def __init__(self,data):
      self.data = data
      self.tree = None
      
  def _build(self,points,depth):
      k = len(points.columns)
      _axis = depth % k
      _column = points.columns[_axis]
  
      if len(points) == 0:
          return None
  
      objects_list = points.sort_values(by = [_column], ascending = True)
  
      if len(objects_list) % 2 == 0:
  
          median_idx = int ( ( len(objects_list)/2) )
      else:
          median_idx = floor ( ( len(objects_list)/2) )
  
      node = Node( round(objects_list.iloc[median_idx][_column],3))
      node.left = self._build(objects_list.iloc[0:median_idx],depth+1)
      node.right = self._build(objects_list.iloc[median_idx+1:],depth+1)
      
      return node
  
  def build(self):
      self.tree = self._build(self.data,depth=0)
  
      
  </code></pre>
  <p>this is the way I tested it, but  I keep getting the same error</p>
  <pre><code>test_df =  pd.DataFrame( data = [ (2,3),(5,4),(9,6),(4,7),(8,1),(7,2)],columns = [&quot;X&quot;, &quot;Y&quot;])
  KD = KD_Tree(test_df)
  KD.build()
  </code></pre>
  <p>I get this error</p>
  <pre><code> NodeValueError                            Traceback (most recent call last)
  ~\AppData\Local\Temp/ipykernel_3900/1487032925.py in &lt;module&gt;
        1 KD = KD_Tree(test_df)
  ----&gt; 2 KD.build()
  
  ~\AppData\Local\Temp/ipykernel_3900/1303367285.py in build(self)
       28 
       29     def build(self):
  ---&gt; 30         self.tree = self._build(self.data,depth=0)
       31 
       32 
  
  ~\AppData\Local\Temp/ipykernel_3900/1303367285.py in _build(self, points, depth)
       21             median_idx = floor ( ( len(objects_list)/2) )
       22 
  ---&gt; 23         node = Node( round(objects_list.iloc[median_idx][_column],3))
       24         node.left = self._build(objects_list.iloc[0:median_idx],depth+1)
       25         node.right = self._build(objects_list.iloc[median_idx+1:],depth+1)
  
  c:\users\gabri\appdata\local\programs\python\python39\lib\site-packages\binarytree\__init__.py in __init__(self, value, left, right)
      101         right: Optional[&quot;Node&quot;] = None,
      102     ) -&gt; None:
  --&gt; 103         self.value = self.val = value
      104         self.left = left
      105         self.right = right
  
  c:\users\gabri\appdata\local\programs\python\python39\lib\site-packages\binarytree\__init__.py in __setattr__(self, attr, obj)
      204         elif attr == _ATTR_VALUE:
      205             if not isinstance(obj, (float, int)):
  --&gt; 206                 raise NodeValueError(&quot;node value must be a float or int&quot;)
      207             object.__setattr__(self, _ATTR_VAL, obj)
      208 
  
  NodeValueError: node value must be a float or int
  </code></pre>
  <p>Any idea why?</p>