Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update/calculate hourly airqualitydata using bigqdata #4355

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/workflows/airqo_etl_utils/airqo_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,9 +606,8 @@ def calibrate_data(data: pd.DataFrame) -> pd.DataFrame:
]

# TODO: Need to opt for a different approach eg forward fill, can't do here as df only has data of last 1 hour. Perhaps use raw data only?
default_values = {col: 0 for col in columns_to_fill}
data = data.assign(**default_values)
# data[columns_to_fill] = data[columns_to_fill].fillna(0)
# Fill nas for the specified fields.
data[columns_to_fill] = data[columns_to_fill].fillna(0)
Comment on lines +609 to +610
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider using forward fill instead of zero for missing values.

Filling missing sensor readings with zero can significantly skew the calibration results. A more appropriate approach would be to use forward fill (ffill) or interpolation, which better preserves the data trends and real-world conditions.

Here's a suggested implementation:

-# Fill nas for the specified fields.
-data[columns_to_fill] = data[columns_to_fill].fillna(0)
+# Forward fill missing values, then backward fill any remaining NAs at the start
+data[columns_to_fill] = data[columns_to_fill].fillna(method='ffill').fillna(method='bfill')

This approach:

  1. First attempts to forward fill using previous valid measurements
  2. Then backward fills any remaining NAs at the start of the series
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Fill nas for the specified fields.
data[columns_to_fill] = data[columns_to_fill].fillna(0)
# Forward fill missing values, then backward fill any remaining NAs at the start
data[columns_to_fill] = data[columns_to_fill].fillna(method='ffill').fillna(method='bfill')


# additional input columns for calibration
data["avg_pm2_5"] = data[["s1_pm2_5", "s2_pm2_5"]].mean(axis=1).round(2)
Expand Down
Loading