Skip to content

Commit b451c38

Browse files
Fix CSV column header spacing in FlightDataExporter (#865)
Co-authored-by: Gui-FernandesBR <[email protected]>
1 parent 170e89c commit b451c38

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ Attention: The newest changes should be on top -->
3636

3737
### Fixed
3838

39+
- BUG: Fix CSV column header spacing in FlightDataExporter [#864](https://github.com/RocketPy-Team/RocketPy/issues/864)
40+
3941

4042
## [v1.11.0] - 2025-11-01
4143

rocketpy/simulation/flight_data_exporter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class attributes which are instances of the Function class. Usage
156156
) from exc
157157
variable_points = variable_function(time_points)
158158
exported_matrix += [variable_points]
159-
exported_header += f", {variable_function.__outputs__[0]}"
159+
exported_header += f",{variable_function.__outputs__[0]}"
160160

161161
exported_matrix = np.array(exported_matrix).T # Fix matrix orientation
162162

tests/unit/test_flight_data_exporter.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import json
88

99
import numpy as np
10+
import pytest
1011

1112
from rocketpy.simulation import FlightDataExporter
1213

@@ -192,3 +193,57 @@ def test_export_kml_trajectory(flight_calisto_robust, tmp_path):
192193
assert np.allclose(flight_calisto_robust.latitude[:, 1], lat, atol=1e-3)
193194
assert np.allclose(flight_calisto_robust.longitude[:, 1], lon, atol=1e-3)
194195
assert np.allclose(flight_calisto_robust.z[:, 1], z, atol=1e-3)
196+
197+
198+
def test_export_data_csv_column_names_no_leading_spaces(flight_calisto, tmp_path):
199+
"""Test that CSV column headers have no leading spaces after commas.
200+
201+
This validates that exported CSV files can be easily read with pandas
202+
without requiring leading spaces in column names (Issue #864).
203+
204+
When reading CSVs with pandas, column names should be accessible as
205+
'Vz (m/s)' not ' Vz (m/s)' (with leading space).
206+
207+
Parameters
208+
----------
209+
flight_calisto : rocketpy.Flight
210+
Flight object to be tested.
211+
tmp_path : pathlib.Path
212+
Pytest fixture for temporary directories.
213+
"""
214+
file_name = tmp_path / "flight_data_columns.csv"
215+
FlightDataExporter(flight_calisto).export_data(
216+
str(file_name), "z", "vz", "altitude"
217+
)
218+
219+
# Read the header line directly
220+
with open(file_name, "r") as f:
221+
header_line = f.readline().strip()
222+
223+
# Verify header format - should have no spaces after commas
224+
# Format should be: # Time (s),Z (m),Vz (m/s),Altitude AGL (m)
225+
assert header_line.startswith("# Time (s),")
226+
assert ", " not in header_line, "Header should not contain ', ' (comma-space)"
227+
228+
# Verify with pandas that columns are accessible without leading spaces
229+
pd = pytest.importorskip("pandas")
230+
df = pd.read_csv(file_name)
231+
columns = df.columns.tolist()
232+
233+
# First column should be '# Time (s)'
234+
assert columns[0] == "# Time (s)"
235+
236+
# Other columns should NOT have leading spaces
237+
for col in columns[1:]:
238+
assert not col.startswith(" "), f"Column '{col}' has leading space"
239+
240+
# Verify columns are accessible with expected names (no leading spaces)
241+
assert "Z (m)" in columns
242+
assert "Vz (m/s)" in columns
243+
assert "Altitude AGL (m)" in columns
244+
245+
# Verify we can access data using column names without spaces
246+
_ = df["# Time (s)"]
247+
_ = df["Z (m)"]
248+
_ = df["Vz (m/s)"]
249+
_ = df["Altitude AGL (m)"]

0 commit comments

Comments
 (0)