Skip to content

Commit 6524f56

Browse files
committed
Refactor replace into its own function
1 parent 780f90e commit 6524f56

5 files changed

Lines changed: 10 additions & 8 deletions

File tree

src/nuclearmasses/io/ame_mass_parse.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ def read_file(self) -> pd.DataFrame:
9797
)
9898
# We use the NUBASE data to define whether or not an isotope is experimentally measured,
9999
# so for this data we'll just drop any and all '#' characters
100-
str_cols = df.select_dtypes(include=["object", "string"]).columns
101-
df[str_cols] = df[str_cols].astype(str).apply(lambda s: s.str.replace("#", "", regex=False))
100+
df = self.strip_char_from_string_columns(df, "#")
102101

103102
if self.year == 1983:
104103
# The column headers and units are repeated in the 1983 table

src/nuclearmasses/io/ame_reaction_1_parse.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ def read_file(self) -> pd.DataFrame:
9595
)
9696
# We use the NUBASE data to define whether or not an isotope is experimentally measured,
9797
# so for this data we'll just drop any and all '#' characters
98-
str_cols = df.select_dtypes(include=["object", "string"]).columns
99-
df[str_cols] = df[str_cols].astype(str).apply(lambda s: s.str.replace("#", "", regex=False))
98+
df = self.strip_char_from_string_columns(df, "#")
10099

101100
if self.year == 1983:
102101
# The column headers and units are repeated in the 1983 table

src/nuclearmasses/io/ame_reaction_2_parse.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ def read_file(self) -> pd.DataFrame:
9595
)
9696
# We use the NUBASE data to define whether or not an isotope is experimentally measured,
9797
# so for this data we'll just drop any and all '#' characters
98-
str_cols = df.select_dtypes(include=["object", "string"]).columns
99-
df[str_cols] = df[str_cols].astype(str).apply(lambda s: s.str.replace("#", "", regex=False))
98+
df = self.strip_char_from_string_columns(df, "#")
10099

101100
if self.year == 1983:
102101
# The column headers and units are repeated in the 1983 table

src/nuclearmasses/io/nubase_parse.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,7 @@ def read_file(self) -> pd.DataFrame:
185185
# We use the NUBASE data to define whether or not an isotope is experimentally measured,
186186
df["Experimental"] = ~df["NUBASEMassExcess"].astype("string").str.contains("#", na=False)
187187
# Once we have used the '#' to determine if it's experimental or not, we can remove all instances of it
188-
str_cols = df.select_dtypes(include=["object", "string"]).columns
189-
df[str_cols] = df[str_cols].astype(str).apply(lambda s: s.str.replace("#", "", regex=False))
188+
df = self.strip_char_from_string_columns(df, "#")
190189

191190
df = self.parse_half_life(df)
192191
df = self.calculate_relative_error(df)

src/nuclearmasses/utils/converter.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,9 @@ def read_fwf(base: DataInput, **kwargs):
134134

135135
# Filesystem path
136136
return pd.read_fwf(base, **kwargs)
137+
138+
@staticmethod
139+
def strip_char_from_string_columns(df: pd.DataFrame, char: str) -> pd.DataFrame:
140+
cols = df.select_dtypes(include=["object", "string"]).columns
141+
df[cols] = df[cols].apply(lambda s: s.str.replace(char, "", regex=False))
142+
return df

0 commit comments

Comments
 (0)