Skip to content

Commit 72ed6f7

Browse files
Fix: Prevent redundant OUT column creation and add null check
- Moved OUT column creation outside inner loop to avoid redundant operations - Added null check for pdb_records[r] to prevent processing undefined records - Combined performance optimization with robustness improvement
1 parent 04daaa6 commit 72ed6f7

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

biopandas/pdb/pandas_pdb.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -715,9 +715,10 @@ def to_pdb(self, path, records=None, gz=False, append_newline=True):
715715
dfs = {r: self.df[r].copy() for r in records if not self.df[r].empty}
716716

717717
for r in dfs:
718-
for col in pdb_records[r]:
719-
dfs[r][col["id"]] = dfs[r][col["id"]].apply(col["strf"])
718+
if pdb_records[r]:
720719
dfs[r]["OUT"] = pd.Series("", index=dfs[r].index)
720+
for col in pdb_records[r]:
721+
dfs[r][col["id"]] = dfs[r][col["id"]].apply(col["strf"])
721722

722723
for c in dfs[r].columns:
723724
# fix issue where coordinates with four or more digits would
@@ -946,9 +947,10 @@ def to_pdb_stream(self, records: tuple[str] = ("ATOM", "HETATM")) -> StringIO:
946947
dfs = {r: df.loc[df.record_name == r] for r in records}
947948

948949
for r in dfs:
949-
for col in pdb_records[r]:
950-
dfs[r][col["id"]] = dfs[r][col["id"]].apply(col["strf"])
950+
if pdb_records[r]:
951951
dfs[r]["OUT"] = pd.Series("", index=dfs[r].index)
952+
for col in pdb_records[r]:
953+
dfs[r][col["id"]] = dfs[r][col["id"]].apply(col["strf"])
952954

953955
for c in dfs[r].columns:
954956
# fix issue where coordinates with four or more digits would

0 commit comments

Comments
 (0)