wellcast.features¶
features ¶
Feature engineering for production data.
Two kinds of features are built:
- Physics features combine measurements of the same row (GOR, water cut,
drawdown and so on).
FEATURE_SOURCESrecords which raw columns each one uses, so a model can drop features computed from its own target. - Lag and rolling features summarize the past of a column within each
well. They are leakage-safe by construction: the value at time
tonly uses rows strictly beforetof the same well.
All functions expect the canonical schema of wellcast.io, sorted by
date within each well, and return a new DataFrame.
features_using ¶
Return physics features computed from column on the same row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
column
|
str
|
Raw column, typically a model target such as |
required |
Returns:
| Type | Description |
|---|---|
list of str
|
Feature names that must be excluded when predicting |
Examples:
add_physics_features ¶
add_physics_features(df: DataFrame, *, reservoir_pressure: float | Mapping[str, float] | None = None, by: str | None = 'well') -> pd.DataFrame
Add physics-informed features computed from each row.
Features are added only when their source columns are present.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Production table with a |
required |
reservoir_pressure
|
float or mapping of str to float
|
Average reservoir pressure (psi), one value for all wells or one per well. When omitted, the static pressure is estimated as the last bottomhole pressure recorded during a shut-in (end of build-up), carried forward in time; rows before the first shut-in get NaN. |
None
|
by
|
str or None
|
Well column. Default |
'well'
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Copy of
|
Raises:
| Type | Description |
|---|---|
DataValidationError
|
If the index is not a |
Notes
Gas rates are in Mscf/d, hence the factor 1000. Cumulative volumes integrate daily-average rates over the time step to the previous row.
Examples:
>>> import pandas as pd
>>> idx = pd.date_range("2024-01-01", periods=2, freq="D")
>>> df = pd.DataFrame(
... {
... "oil_rate": [800.0, 0.0],
... "gas_rate": [400.0, 0.0],
... "water_rate": [200.0, 0.0],
... },
... index=idx,
... )
>>> out = add_physics_features(df)
>>> out[["gor", "water_cut", "cum_oil"]]
gor water_cut cum_oil
2024-01-01 500.0 0.2 800.0
2024-01-02 NaN NaN 800.0
add_lag_features ¶
add_lag_features(df: DataFrame, columns: Sequence[str], lags: Sequence[int] = (1, 7), *, by: str | None = 'well') -> pd.DataFrame
Add lagged copies of columns, shifted within each well.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Table sorted by date within each well. |
required |
columns
|
sequence of str
|
Columns to lag. |
required |
lags
|
sequence of int
|
Positive lags in rows. Default (1, 7). |
(1, 7)
|
by
|
str or None
|
Well column. Default |
'well'
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Copy with columns named |
Raises:
| Type | Description |
|---|---|
DataValidationError
|
If a column is missing or a lag is not a positive integer. |
Examples:
add_rolling_features ¶
add_rolling_features(df: DataFrame, columns: Sequence[str], windows: Sequence[int] = (7, 30), stats: Sequence[str] = ('mean', 'std'), *, by: str | None = 'well', min_periods: int = 1) -> pd.DataFrame
Add rolling statistics over strictly past rows of each well.
The window ending at row t covers rows t - w to t - 1, so the
current value never enters its own feature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Table sorted by date within each well. |
required |
columns
|
sequence of str
|
Columns to summarize. |
required |
windows
|
sequence of int
|
Window lengths in rows. Default (7, 30). |
(7, 30)
|
stats
|
sequence of str
|
Any of |
('mean', 'std')
|
by
|
str or None
|
Well column. Default |
'well'
|
min_periods
|
int
|
Minimum past observations required. Default 1. |
1
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Copy with columns named |
Raises:
| Type | Description |
|---|---|
DataValidationError
|
If a column is missing, a window is < 1 or a statistic is unknown. |
Examples:
build_features ¶
build_features(df: DataFrame, *, lag_columns: Sequence[str] | None = None, lags: Sequence[int] = (1, 7), windows: Sequence[int] = (7, 30), reservoir_pressure: float | Mapping[str, float] | None = None, by: str | None = 'well') -> pd.DataFrame
Build physics, lag and rolling features in one call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Production table in the canonical schema. |
required |
lag_columns
|
sequence of str
|
Columns for lag and rolling-mean features. Default: the canonical rate
and pressure columns present in |
None
|
lags
|
sequence of int
|
Lags in rows. Default (1, 7). |
(1, 7)
|
windows
|
sequence of int
|
Rolling-mean windows in rows. Default (7, 30). |
(7, 30)
|
reservoir_pressure
|
float or mapping
|
Passed to |
None
|
by
|
str or None
|
Well column. Default |
'well'
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Copy of |
Examples: