Skip to content

Commit 1a959e6

Browse files
committed
REF: extract method _identify_group
1 parent 40b2aa5 commit 1a959e6

File tree

1 file changed

+40
-27
lines changed

1 file changed

+40
-27
lines changed

pandas/io/pytables.py

+40-27
Original file line numberDiff line numberDiff line change
@@ -1735,38 +1735,12 @@ def _write_to_group(
17351735
errors: str = "strict",
17361736
track_times: bool = True,
17371737
):
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-
17491738
# we don't want to store a table node at all if our object is 0-len
17501739
# as there are not dtypes
17511740
if getattr(value, "empty", None) and (format == "table" or append):
17521741
return
17531742

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)
17701744

17711745
s = self._create_storer(group, format, value, encoding=encoding, errors=errors)
17721746
if append:
@@ -1807,6 +1781,45 @@ def _read_group(self, group: "Node"):
18071781
s.infer_axes()
18081782
return s.read()
18091783

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+
18101823

18111824
class TableIterator:
18121825
"""

0 commit comments

Comments
 (0)