Skip to content

Commit c535db3

Browse files
committed
reduce copy
1 parent 492a762 commit c535db3

1 file changed

Lines changed: 19 additions & 8 deletions

File tree

python/acom_music_box/conditions_manager.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,23 @@ def _load_from_conditions_section(self, path_to_json: str, conditions_config: di
534534
for data_block in conditions_config["data"]:
535535
self._load_inline_data(data_block)
536536

537+
538+
def _ensure_numeric_float64(self, df: pd.DataFrame) -> pd.DataFrame:
539+
"""
540+
Convert all numeric columns to float64 to handle integer values (e.g., 0).
541+
Uses vectorized operation for performance with wide/large DataFrames.
542+
543+
Args:
544+
df: DataFrame to convert.
545+
546+
Returns:
547+
DataFrame with numeric columns as float64.
548+
"""
549+
numeric_cols = df.select_dtypes(include=['number']).columns
550+
if len(numeric_cols) > 0:
551+
df[numeric_cols] = df[numeric_cols].astype(np.float64)
552+
return df
553+
537554
def _read_csv(self, file_path: str):
538555
"""
539556
Load conditions from a CSV file.
@@ -542,10 +559,7 @@ def _read_csv(self, file_path: str):
542559
file_path: Path to the CSV file.
543560
"""
544561
df = pd.read_csv(file_path, skipinitialspace=True)
545-
# Convert all numeric columns to float64 to handle integer values like 0
546-
for col in df.columns:
547-
if pd.api.types.is_numeric_dtype(df[col]):
548-
df[col] = df[col].astype(np.float64)
562+
df = self._ensure_numeric_float64(df)
549563
self.add_from_dataframe(df)
550564

551565
def _load_inline_data(self, data_block: dict):
@@ -565,10 +579,7 @@ def _load_inline_data(self, data_block: dict):
565579
rows = data_block["rows"]
566580

567581
df = pd.DataFrame(rows, columns=headers)
568-
# Convert all numeric columns to float64 to handle integer values like 0
569-
for col in df.columns:
570-
if pd.api.types.is_numeric_dtype(df[col]):
571-
df[col] = df[col].astype(np.float64)
582+
df = self._ensure_numeric_float64(df)
572583
self.add_from_dataframe(df)
573584

574585
def get_conditions_at_time(self, time: float) -> dict:

0 commit comments

Comments
 (0)