Skip to content

Commit 492a762

Browse files
committed
convert int to float64
1 parent 7841bcb commit 492a762

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

python/acom_music_box/conditions_manager.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,10 @@ def _read_csv(self, file_path: str):
542542
file_path: Path to the CSV file.
543543
"""
544544
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)
545549
self.add_from_dataframe(df)
546550

547551
def _load_inline_data(self, data_block: dict):
@@ -561,6 +565,10 @@ def _load_inline_data(self, data_block: dict):
561565
rows = data_block["rows"]
562566

563567
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)
564572
self.add_from_dataframe(df)
565573

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

python/tests/unit/test_conditions_manager.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,47 @@ def test_surf_rate_parameter_format(self):
313313
assert "SURF.surface.particle number concentration.# m-3" in conds["rate_parameters"]
314314
assert conds["rate_parameters"]["SURF.surface.particle number concentration.# m-3"] == 1e12
315315

316+
def test_integer_values_converted_to_float64(self):
317+
"""Test that integer values (like 0) are converted to np.float64."""
318+
import numpy as np
319+
320+
manager = ConditionsManager()
321+
322+
# Test with inline data containing integer 0
323+
data_block = {
324+
"headers": ["time.s", "ENV.temperature.K", "PHOTO.rate1.s-1"],
325+
"rows": [[0, 300.0, 0]] # Integer 0 values
326+
}
327+
manager._load_inline_data(data_block)
328+
329+
# Check that values are stored as float64
330+
df = manager._df
331+
assert df["time.s"].dtype == np.float64
332+
assert df["ENV.temperature.K"].dtype == np.float64
333+
assert df["PHOTO.rate1.s-1"].dtype == np.float64
334+
335+
# Test with CSV-like data
336+
with tempfile.TemporaryDirectory() as tmpdir:
337+
csv_content = """time.s,ENV.temperature.K,PHOTO.rate2.s-1
338+
0,298.15,0
339+
3600,310.0,1e-5
340+
"""
341+
csv_path = os.path.join(tmpdir, "test.csv")
342+
with open(csv_path, 'w') as f:
343+
f.write(csv_content)
344+
345+
manager2 = ConditionsManager()
346+
manager2._read_csv(csv_path)
347+
348+
df2 = manager2._df
349+
assert df2["time.s"].dtype == np.float64
350+
assert df2["ENV.temperature.K"].dtype == np.float64
351+
assert df2["PHOTO.rate2.s-1"].dtype == np.float64
352+
353+
# Verify the values are correct
354+
assert df2.loc[0, "time.s"] == 0.0
355+
assert df2.loc[0, "PHOTO.rate2.s-1"] == 0.0
356+
316357

317358
if __name__ == "__main__":
318359
pytest.main([__file__, "-v"])

0 commit comments

Comments
 (0)