wellcast.clean¶
clean ¶
Cleaning and quality control for field production data.
Outlier detectors flag suspicious values and return a boolean Series aligned with the input. They never delete or overwrite data: the decision to drop, replace or keep a flagged value belongs to the engineer.
detect_shut_ins ¶
detect_shut_ins(df: DataFrame, rate_columns: Sequence[str] = ('oil_rate', 'gas_rate', 'water_rate'), *, threshold: float = 0.0, hours_column: str = 'on_stream_hours') -> pd.Series
Flag periods when a well is shut in.
A row is a shut-in when its producing hours are zero, or when every rate
column present in df is known and at or below threshold. Missing
rates are not treated as zero: a gap in reporting is not a shut-in.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Production table in the canonical schema. |
required |
rate_columns
|
sequence of str
|
Candidate rate columns; those absent from |
('oil_rate', 'gas_rate', 'water_rate')
|
threshold
|
float
|
Rate at or below which the well counts as not flowing. Default 0. |
0.0
|
hours_column
|
str
|
Producing-hours column, used when present. |
'on_stream_hours'
|
Returns:
| Type | Description |
|---|---|
Series
|
Boolean flags aligned with |
Raises:
| Type | Description |
|---|---|
DataValidationError
|
If none of |
Examples:
hampel_filter ¶
Flag outliers with the Hampel identifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
series
|
Series
|
Values in time order. |
required |
window
|
int
|
Odd length of the centered window. Default 7. |
7
|
n_sigmas
|
float
|
Threshold in robust standard deviations. Default 3. |
3.0
|
Returns:
| Type | Description |
|---|---|
Series
|
Boolean flags aligned with |
Raises:
| Type | Description |
|---|---|
DataValidationError
|
If |
Notes
A value \(x_i\) is flagged when
where \(m_i\) and \(\mathrm{MAD}_i\) are the median and median absolute deviation of the centered window. Missing values inside the window are ignored. See Pearson, R. K. (2002), "Outliers in process modeling and identification", IEEE Trans. Control Syst. Technol. 10(1).
Examples:
rolling_zscore_outliers ¶
rolling_zscore_outliers(series: Series, window: int = 30, threshold: float = 3.0, min_periods: int | None = None) -> pd.Series
Flag values far from a centered rolling mean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
series
|
Series
|
Values in time order. |
required |
window
|
int
|
Rolling window length in samples. Default 30. |
30
|
threshold
|
float
|
Absolute z-score above which a value is flagged. Default 3. |
3.0
|
min_periods
|
int
|
Minimum observations in the window. Default |
None
|
Returns:
| Type | Description |
|---|---|
Series
|
Boolean flags aligned with |
Notes
\(z_i = (x_i - \bar{x}_i) / s_i\) with the rolling mean \(\bar{x}_i\) and standard deviation \(s_i\). Windows with zero spread produce no flags.
Examples:
iqr_outliers ¶
Flag values outside Tukey's fences.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
series
|
Series
|
Values to test. |
required |
k
|
float
|
Fence multiplier. Default 1.5 (3.0 for "far out" values). |
1.5
|
Returns:
| Type | Description |
|---|---|
Series
|
Boolean flags aligned with |
Notes
Values below \(Q_1 - k\,\mathrm{IQR}\) or above \(Q_3 + k\,\mathrm{IQR}\) are flagged (Tukey, 1977, Exploratory Data Analysis).
Examples:
flag_outliers ¶
flag_outliers(df: DataFrame, column: str, method: Literal['hampel', 'zscore', 'iqr'] = 'hampel', *, by: str | None = 'well', **kwargs: Any) -> pd.Series
Apply an outlier detector to one column, separately for each well.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Production table, sorted by date within each well. |
required |
column
|
str
|
Column to test. |
required |
method
|
('hampel', 'zscore', 'iqr')
|
Detector to use. Default |
"hampel"
|
by
|
str or None
|
Grouping column. Default |
'well'
|
**kwargs
|
Any
|
Passed to the detector. |
{}
|
Returns:
| Type | Description |
|---|---|
Series
|
Boolean flags aligned with |
Raises:
| Type | Description |
|---|---|
DataValidationError
|
If |
Examples:
resample_regular ¶
resample_regular(df: DataFrame, freq: str = 'D', agg: Literal['mean', 'median', 'sum', 'first', 'last', 'min', 'max'] = 'mean', *, by: str | None = 'well') -> pd.DataFrame
Resample each well onto a regular time grid.
Missing periods become NaN rows so gaps stay visible. Non-numeric columns
other than by are dropped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Table with a |
required |
freq
|
str
|
Pandas offset alias. Default |
'D'
|
agg
|
str
|
Aggregation within each period. Default |
'mean'
|
by
|
str or None
|
Grouping column. Default |
'well'
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Regularly sampled data, wells stacked in order. |
Raises:
| Type | Description |
|---|---|
DataValidationError
|
If the index is not a |
Examples:
fill_gaps ¶
fill_gaps(df: DataFrame, columns: Sequence[str] | None = None, method: Literal['linear', 'ffill', 'bfill'] = 'linear', limit: int = 3, *, by: str | None = 'well') -> pd.DataFrame
Fill short gaps within each well, leaving longer gaps untouched.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Regularly sampled table with a |
required |
columns
|
sequence of str
|
Columns to fill. Default: all numeric columns. |
None
|
method
|
('linear', 'ffill', 'bfill')
|
|
"linear"
|
limit
|
int
|
Maximum number of consecutive missing values to fill. Default 3. |
3
|
by
|
str or None
|
Grouping column. Default |
'well'
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Copy of |
Raises:
| Type | Description |
|---|---|
DataValidationError
|
If |
Examples:
qc_report ¶
qc_report(df: DataFrame, *, by: str | None = 'well', flatline_min_run: int = 5, max_hours: float = 24.0) -> pd.DataFrame
Summarize data quality per well and column.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Production table in the canonical schema. |
required |
by
|
str or None
|
Grouping column. Default |
'well'
|
flatline_min_run
|
int
|
Minimum run of identical non-zero readings counted as a flatline. Default 5. |
5
|
max_hours
|
float
|
Upper bound for |
24.0
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Tidy table with one row per (well, column) and the columns |
Examples: