Skip to content

Conversation

@isztldav
Copy link

@isztldav isztldav commented Dec 3, 2025

Description

This PR fixes a bug where pandas.errors.EmptyDataError is raised when the FileOutput class attempts to read an existing but empty CSV file. The fix adds error handling in the out() method to catch this exception and treat empty files as if they don't exist, creating a new file with the current emissions data.

Related Issue

I have not opened an issue.

Motivation and Context

This bug occurs when CSV emission files exist but are empty, which can happen if:

  • A previous write operation was interrupted
  • The file was manually created but never populated
  • File system errors occurred during a write operation

Without this fix, the application crashes with pandas.errors.EmptyDataError: No columns to parse from file when trying to append or update emissions data, preventing users from tracking their carbon emissions.

How Has This Been Tested?

  1. Unit Tests: Added two new test cases in tests/output_methods/test_file.py:

    • test_file_output_out_append_empty_file_exists(): Verifies append mode handles empty files correctly
    • test_file_output_out_update_empty_file_exists(): Verifies update mode handles empty files correctly
  2. Manual Testing:

    • Created empty CSV files and verified the fix handles them gracefully
    • Confirmed existing functionality remains unchanged with non-empty files
  3. Pre-commit Hooks: Ran all linting and formatting checks

  4. Test Suite: Ran the full test suite to ensure no regressions:

    uv run python -m unittest tests.output_methods.test_file.TestFileOutput

Screenshots (if appropriate):

N/A - This is a bug fix with no UI changes

Types of changes

What types of changes does your code introduce? Put an x in all the boxes that apply:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

Go over all the following points, and put an x in all the boxes that apply.

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING.md document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Handle pandas.errors.EmptyDataError that occurs when pd.read_csv() encounters an existing but empty CSV file. This fix catches the error in both 'append' and 'update' modes and treats the empty file as if it doesn't exist, creating a new file with the current data.

The fix prevents crashes when emission CSV files exist but are empty, which can happen if a previous write operation was interrupted.

Added test cases for both append and update modes with empty files.
@isztldav isztldav requested a review from a team as a code owner December 3, 2025 09:45
@cianc
Copy link
Contributor

cianc commented Dec 5, 2025

If we changed line 90 from

file_exists: bool = os.path.isfile(self.save_file_path)

to something like

file_exists_and_non_empty = (os.path.isfile(self.save_file_path)) and (os.path.getsize(self.save_file_path) != 0)

then I think we could rely on the existing logic without adding more conditionals? Personally I already find the existing logic hard to follow.

@isztldav
Copy link
Author

isztldav commented Dec 5, 2025

good idea, done

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.

2 participants