pandas_openscm.testing#
Testing helpers
Placed here to avoid putting __init__.py files in our tests directory,
see details here: https://docs.pytest.org/en/stable/explanation/goodpractices.html#which-import-mode.
Also see here: https://docs.pytest.org/en/stable/explanation/pythonpath.html#pytest-import-mechanisms-and-sys-path-pythonpath.
Functions:
| Name | Description |
|---|---|
assert_frame_alike |
Assert that two pd.DataFrame are alike |
assert_move_plan_equal |
Assert that two MovePlan are equal |
changer |
Change a value |
check_result |
Check result in the case where it could be multiple types |
convert_to_desired_type |
Convert a |
create_test_df |
Create a pd.DataFrame to use in testing |
assert_frame_alike #
assert_frame_alike(
res: DataFrame,
exp: DataFrame,
check_like: bool = True,
**kwargs: Any,
) -> None
Assert that two pd.DataFrame are alike
Here, alike means that they have the same data, just potentially not in the same order. This includes the order of index levels, which may also differ.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
res
|
DataFrame
|
Result to check |
required |
exp
|
DataFrame
|
Expected result |
required |
check_like
|
bool
|
Passed to assert_frame_equal |
True
|
**kwargs
|
Any
|
Passed to assert_frame_equal |
{}
|
Source code in src/pandas_openscm/testing.py
assert_move_plan_equal #
Assert that two MovePlan are equal
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
res
|
MovePlan
|
The result |
required |
exp
|
MovePlan
|
The expectation |
required |
Raises:
| Type | Description |
|---|---|
AssertionError
|
|
Source code in src/pandas_openscm/testing.py
changer #
Change a value
This is just meant as a helper for our tests
check_result #
Check result in the case where it could be multiple types
Specifically, pd.DataFrame or pd.Series.
This is a thin wrapper, if you want specific functionality, use the underlying function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
res
|
P
|
Result |
required |
exp
|
P
|
Expected |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
Type of |
Source code in src/pandas_openscm/testing.py
convert_to_desired_type #
convert_to_desired_type(
df: DataFrame, pobj_type: Literal["DataFrame", "Series"]
) -> DataFrame | Series[Any]
Convert a df to the desired type for testing
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
pd.DataFrame to convert |
required |
pobj_type
|
Literal['DataFrame', 'Series']
|
Type to convert to If "DataFrame", then |
required |
Returns:
| Type | Description |
|---|---|
DataFrame | Series[Any]
|
|
Source code in src/pandas_openscm/testing.py
create_test_df #
create_test_df(
*,
variables: Collection[tuple[str, str]],
n_scenarios: int,
n_runs: int,
timepoints: NDArray[floating[Any]],
rng: Generator | None = None,
) -> DataFrame
Create a pd.DataFrame to use in testing
This uses the idea of simple climate model runs, where you have a number of scenarios, each of which has a number of variables from a number of different model runs with output for a number of different time points.
The result will contain all combinations of scenarios, variables and runs, with the units being defined by each variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
variables
|
Collection[tuple[str, str]]
|
Variables and their units to create |
required |
n_scenarios
|
int
|
Number of scenarios to create. These are simply incremented with their number. |
required |
n_runs
|
int
|
Number of runs to create. These are simply numbered. |
required |
timepoints
|
NDArray[floating[Any]]
|
Time points to use with the data. |
required |
rng
|
Generator | None
|
Random number generator to use. If not supplied, we create an instance using numpy.random.default_rng. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Generated test pd.DataFrame. |
Source code in src/pandas_openscm/testing.py
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | |