Skip to content

Fix pandas SettingWithCopyWarning in Elexon forecast preprocessing #401

@SaitejaKommi

Description

@SaitejaKommi

Description

In the Elexon forecast data processing pipeline, assignment to start_time is performed on a DataFrame slice, which raises a pandas SettingWithCopyWarning.

This warning occurs when a transformation is applied to a view rather than a standalone DataFrame, which may lead to unpredictable behavior.

Expected Behavior

  • No SettingWithCopyWarning should be emitted during Elexon preprocessing tests.
  • Column assignments should operate on a safe DataFrame copy or use .loc.

Actual Behavior

  • SettingWithCopyWarning is raised when assigning to start_time.

Scope

  • elexon_plots.py only.

Suggested Fix

Avoid chained assignment by explicitly creating a copy of the DataFrame or using .loc before mutating columns.

Example approaches:

df = df.copy()
df["start_time"] = ...

or

df.loc[:, "start_time"] = ...

Validation

Run the following test to confirm the warning is resolved:

python -m pytest tests/test_elexon_plot.py -q

Verify:

  • Tests pass successfully
  • SettingWithCopyWarning no longer appears

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions