Skip to content

fix: drop dtype coercion in CSV update path - #1370

Open
davidberenstein1957 wants to merge 1 commit into
masterfrom
fix/csv-update-dtype-coercion
Open

fix: drop dtype coercion in CSV update path#1370
davidberenstein1957 wants to merge 1 commit into
masterfrom
fix/csv-update-dtype-coercion

Conversation

@davidberenstein1957

Copy link
Copy Markdown
Collaborator

What changed

FileOutput.out() in on_csv_write="update" mode no longer assigns the new values column by column with df[col].dtype.type(val). The single-existing-row branch now drops the previous row for the run and re-appends the new one via pd.concat, which achieves the same dedupe-by-run_id without any dtype coercion. The len(df_run) > 1 warning branch is unchanged, and the "no existing row" case falls out of the same concat (dropping nothing).

Why

df[col].dtype.type(val) coerced each incoming value to whatever dtype pandas inferred when reading the existing CSV back. A column empty in every existing row is read back as float64, so this evaluated numpy.float64("") or numpy.float64(None) and raised.

This is reachable today: an OfflineEmissionsTracker leaves longitude/latitude empty, and gpu_count/gpu_model are empty on CPU-only machines. Any run writing twice (a flush() then stop()) crashed on the second write with ValueError: could not convert string to float: ''.

Row ordering

The updated row now moves to the end of the file instead of staying in place. Rows are timestamped, so this only affects files where runs were interleaved.

Verification

  • New test test_file_output_out_update_with_always_empty_columns fails on master with the ValueError above and passes with this change.
  • uv run pytest tests/output_methods/ -q — 48 passed.
  • uv run pytest tests/test_emissions_tracker.py -q — 31 passed.
  • uv run pre-commit run --files codecarbon/output_methods/file.py tests/output_methods/test_file.py — all hooks pass.

Note: #1366 (SLURM) touches this same block incidentally; whichever lands second will need a rebase.

Closes #1367

🤖 Generated with Claude Code

Updating a run row coerced each incoming value to the dtype pandas
inferred for the existing column. Columns empty in every row are read
back as float64, so numpy.float64("") / numpy.float64(None) raised.
Rebuild the row via concat instead, which dedupes by run_id without
touching dtypes.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@codecov

codecov Bot commented Aug 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.42%. Comparing base (065d0e6) to head (900f7dd).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1370      +/-   ##
==========================================
+ Coverage   91.39%   91.42%   +0.03%     
==========================================
  Files          49       49              
  Lines        5056     5051       -5     
==========================================
- Hits         4621     4618       -3     
+ Misses        435      433       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@davidberenstein1957
davidberenstein1957 marked this pull request as ready for review August 12, 2026 17:36
@davidberenstein1957
davidberenstein1957 requested a review from a team as a code owner August 12, 2026 17:36
davidberenstein1957 added a commit that referenced this pull request Aug 12, 2026
Rebased on fix/csv-update-dtype-coercion (#1370), which fixes the CSV dtype
coercion properly, so the local workaround in file.py is dropped.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

on_csv_write="update" crashes on second write when a column is empty in all existing rows

1 participant