@@ -1735,38 +1735,12 @@ def _write_to_group(
1735
1735
errors : str = "strict" ,
1736
1736
track_times : bool = True ,
1737
1737
):
1738
- group = self .get_node (key )
1739
-
1740
- # we make this assertion for mypy; the get_node call will already
1741
- # have raised if this is incorrect
1742
- assert self ._handle is not None
1743
-
1744
- # remove the node if we are not appending
1745
- if group is not None and not append :
1746
- self ._handle .remove_node (group , recursive = True )
1747
- group = None
1748
-
1749
1738
# we don't want to store a table node at all if our object is 0-len
1750
1739
# as there are not dtypes
1751
1740
if getattr (value , "empty" , None ) and (format == "table" or append ):
1752
1741
return
1753
1742
1754
- if group is None :
1755
- paths = key .split ("/" )
1756
-
1757
- # recursively create the groups
1758
- path = "/"
1759
- for p in paths :
1760
- if not len (p ):
1761
- continue
1762
- new_path = path
1763
- if not path .endswith ("/" ):
1764
- new_path += "/"
1765
- new_path += p
1766
- group = self .get_node (new_path )
1767
- if group is None :
1768
- group = self ._handle .create_group (path , p )
1769
- path = new_path
1743
+ group = self ._identify_group (key , append )
1770
1744
1771
1745
s = self ._create_storer (group , format , value , encoding = encoding , errors = errors )
1772
1746
if append :
@@ -1807,6 +1781,45 @@ def _read_group(self, group: "Node"):
1807
1781
s .infer_axes ()
1808
1782
return s .read ()
1809
1783
1784
+ def _identify_group (self , key : str , append : bool ) -> "Node" :
1785
+ """Identify HDF5 group based on key, delete/create group if needed."""
1786
+ group = self .get_node (key )
1787
+
1788
+ # we make this assertion for mypy; the get_node call will already
1789
+ # have raised if this is incorrect
1790
+ assert self ._handle is not None
1791
+
1792
+ # remove the node if we are not appending
1793
+ if group is not None and not append :
1794
+ self ._handle .remove_node (group , recursive = True )
1795
+ group = None
1796
+
1797
+ if group is None :
1798
+ group = self ._create_nodes_and_group (key )
1799
+
1800
+ return group
1801
+
1802
+ def _create_nodes_and_group (self , key : str ) -> "Node" :
1803
+ """Create nodes from key and return group name."""
1804
+ # assertion for mypy
1805
+ assert self ._handle is not None
1806
+
1807
+ paths = key .split ("/" )
1808
+ # recursively create the groups
1809
+ path = "/"
1810
+ for p in paths :
1811
+ if not len (p ):
1812
+ continue
1813
+ new_path = path
1814
+ if not path .endswith ("/" ):
1815
+ new_path += "/"
1816
+ new_path += p
1817
+ group = self .get_node (new_path )
1818
+ if group is None :
1819
+ group = self ._handle .create_group (path , p )
1820
+ path = new_path
1821
+ return group
1822
+
1810
1823
1811
1824
class TableIterator :
1812
1825
"""
0 commit comments