From ed26a4a61c083b300242e853b96408590aeba6fc Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 29 Oct 2025 02:32:50 +0000 Subject: [PATCH 1/4] Fix CSV column header spacing in FlightDataExporter (Issue #864) Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com> --- rocketpy/simulation/flight_data_exporter.py | 2 +- tests/unit/test_flight_data_exporter.py | 60 +++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/rocketpy/simulation/flight_data_exporter.py b/rocketpy/simulation/flight_data_exporter.py index d73c0897a..8bdc53c17 100644 --- a/rocketpy/simulation/flight_data_exporter.py +++ b/rocketpy/simulation/flight_data_exporter.py @@ -156,7 +156,7 @@ class attributes which are instances of the Function class. Usage ) from exc variable_points = variable_function(time_points) exported_matrix += [variable_points] - exported_header += f", {variable_function.__outputs__[0]}" + exported_header += f",{variable_function.__outputs__[0]}" exported_matrix = np.array(exported_matrix).T # Fix matrix orientation diff --git a/tests/unit/test_flight_data_exporter.py b/tests/unit/test_flight_data_exporter.py index b7f8f4e9d..87dcf7210 100644 --- a/tests/unit/test_flight_data_exporter.py +++ b/tests/unit/test_flight_data_exporter.py @@ -192,3 +192,63 @@ def test_export_kml_trajectory(flight_calisto_robust, tmp_path): assert np.allclose(flight_calisto_robust.latitude[:, 1], lat, atol=1e-3) assert np.allclose(flight_calisto_robust.longitude[:, 1], lon, atol=1e-3) assert np.allclose(flight_calisto_robust.z[:, 1], z, atol=1e-3) + + +def test_export_data_csv_column_names_no_leading_spaces(flight_calisto, tmp_path): + """Test that CSV column headers have no leading spaces after commas. + + This validates that exported CSV files can be easily read with pandas + without requiring leading spaces in column names (Issue #864). + + When reading CSVs with pandas, column names should be accessible as + 'Vz (m/s)' not ' Vz (m/s)' (with leading space). + + Parameters + ---------- + flight_calisto : rocketpy.Flight + Flight object to be tested. + tmp_path : pathlib.Path + Pytest fixture for temporary directories. + """ + file_name = tmp_path / "flight_data_columns.csv" + FlightDataExporter(flight_calisto).export_data( + str(file_name), "z", "vz", "altitude" + ) + + # Read the header line directly + with open(file_name, "r") as f: + header_line = f.readline().strip() + + # Verify header format - should have no spaces after commas + # Format should be: # Time (s),Z (m),Vz (m/s),Altitude AGL (m) + assert header_line.startswith("# Time (s),") + assert ", " not in header_line, "Header should not contain ', ' (comma-space)" + + # Verify with pandas that columns are accessible without leading spaces + try: + import pandas as pd + + df = pd.read_csv(file_name) + columns = df.columns.tolist() + + # First column should be '# Time (s)' + assert columns[0] == "# Time (s)" + + # Other columns should NOT have leading spaces + for col in columns[1:]: + assert not col.startswith(" "), f"Column '{col}' has leading space" + + # Verify columns are accessible with expected names (no leading spaces) + assert "Z (m)" in columns + assert "Vz (m/s)" in columns + assert "Altitude AGL (m)" in columns + + # Verify we can access data using column names without spaces + _ = df["# Time (s)"] + _ = df["Z (m)"] + _ = df["Vz (m/s)"] + _ = df["Altitude AGL (m)"] + + except ImportError: + # pandas not available, skip pandas-specific test + pass From 2f93c3452388c9cc7806d5aac26e6894ce9a7d48 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 29 Oct 2025 02:36:01 +0000 Subject: [PATCH 2/4] Improve test to use pytest.importorskip for pandas Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com> --- tests/unit/test_flight_data_exporter.py | 50 ++++++++++++------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/tests/unit/test_flight_data_exporter.py b/tests/unit/test_flight_data_exporter.py index 87dcf7210..b5d85ac57 100644 --- a/tests/unit/test_flight_data_exporter.py +++ b/tests/unit/test_flight_data_exporter.py @@ -7,6 +7,7 @@ import json import numpy as np +import pytest from rocketpy.simulation import FlightDataExporter @@ -225,30 +226,25 @@ def test_export_data_csv_column_names_no_leading_spaces(flight_calisto, tmp_path assert ", " not in header_line, "Header should not contain ', ' (comma-space)" # Verify with pandas that columns are accessible without leading spaces - try: - import pandas as pd - - df = pd.read_csv(file_name) - columns = df.columns.tolist() - - # First column should be '# Time (s)' - assert columns[0] == "# Time (s)" - - # Other columns should NOT have leading spaces - for col in columns[1:]: - assert not col.startswith(" "), f"Column '{col}' has leading space" - - # Verify columns are accessible with expected names (no leading spaces) - assert "Z (m)" in columns - assert "Vz (m/s)" in columns - assert "Altitude AGL (m)" in columns - - # Verify we can access data using column names without spaces - _ = df["# Time (s)"] - _ = df["Z (m)"] - _ = df["Vz (m/s)"] - _ = df["Altitude AGL (m)"] - - except ImportError: - # pandas not available, skip pandas-specific test - pass + pd = pytest.importorskip("pandas") + + df = pd.read_csv(file_name) + columns = df.columns.tolist() + + # First column should be '# Time (s)' + assert columns[0] == "# Time (s)" + + # Other columns should NOT have leading spaces + for col in columns[1:]: + assert not col.startswith(" "), f"Column '{col}' has leading space" + + # Verify columns are accessible with expected names (no leading spaces) + assert "Z (m)" in columns + assert "Vz (m/s)" in columns + assert "Altitude AGL (m)" in columns + + # Verify we can access data using column names without spaces + _ = df["# Time (s)"] + _ = df["Z (m)"] + _ = df["Vz (m/s)"] + _ = df["Altitude AGL (m)"] From b4aef19adb1a56a7f2034af7c069f7afed250230 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 29 Oct 2025 02:37:31 +0000 Subject: [PATCH 3/4] Remove unnecessary blank line in test Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com> --- tests/unit/test_flight_data_exporter.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit/test_flight_data_exporter.py b/tests/unit/test_flight_data_exporter.py index b5d85ac57..6a8e89253 100644 --- a/tests/unit/test_flight_data_exporter.py +++ b/tests/unit/test_flight_data_exporter.py @@ -227,7 +227,6 @@ def test_export_data_csv_column_names_no_leading_spaces(flight_calisto, tmp_path # Verify with pandas that columns are accessible without leading spaces pd = pytest.importorskip("pandas") - df = pd.read_csv(file_name) columns = df.columns.tolist() From cae85542e01f65e68d1a997a84894f1fae20ecf7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 21 Nov 2025 12:22:21 +0000 Subject: [PATCH 4/4] Update CHANGELOG.md for CSV header spacing fix Co-authored-by: Gui-FernandesBR <63590233+Gui-FernandesBR@users.noreply.github.com> --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7090973ad..db6bd8a73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,8 @@ Attention: The newest changes should be on top --> ### Fixed +- BUG: Fix CSV column header spacing in FlightDataExporter [#864](https://github.com/RocketPy-Team/RocketPy/issues/864) + ## [v1.11.0] - 2025-11-01