Skip to content

Commit 7a95a31

Browse files
committed
IDConverter semi-manual serialization, doctest just use pandas frames for stability
1 parent d7a6533 commit 7a95a31

File tree

2 files changed

+26
-29
lines changed

2 files changed

+26
-29
lines changed

docs/conf.py

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
"pandas": ("https://pandas.pydata.org/pandas-docs/stable", None),
9090
"pandera": ("https://pandera.readthedocs.io/en/stable", None),
9191
"plotly": ("https://plotly.com/python-api-reference/", None),
92+
"polars": ("https://pola-rs.github.io/polars/py-polars/html/reference/", None),
9293
"pytest": ("https://docs.pytest.org/en/latest/", None),
9394
"python": ("https://docs.python.org/3", None),
9495
"scipy": ("https://docs.scipy.org/doc/scipy/", None),

src/dispatch/model.py

+25-29
Original file line numberDiff line numberDiff line change
@@ -397,37 +397,30 @@ def __init__(
397397
398398
>>> dm = dm()
399399
400-
Explore the results, starting with how much load could not be met.
401-
402-
>>> dm.lost_load().collect() # doctest: +NORMALIZE_WHITESPACE
403-
shape: (1, 2)
404-
┌─────────────┬───────┐
405-
│ category ┆ count │
406-
│ --- ┆ --- │
407-
│ cat ┆ u32 │
408-
╞═════════════╪═══════╡
409-
│ (-inf, 0.0] ┆ 8784 │
410-
└─────────────┴───────┘
400+
Explore the results, starting with how much load could not be met. The results
401+
are now returned as :class:`polars.LazyFrame` so `collect()` must be called on
402+
them to see the results. We convert to :class:`pandas.DataFrame` to show how
403+
that would be done.
404+
405+
>>> dm.lost_load().collect().to_pandas() # doctest: +NORMALIZE_WHITESPACE
406+
category count
407+
0 (-inf, 0.0] 8784
411408
412409
Generate a full, combined output of all resources at specified frequency.
413410
414-
>>> dm.full_output(freq="YS").collect() # doctest: +NORMALIZE_WHITESPACE
415-
shape: (9, 28)
416-
┌──────────────┬──────────────┬─────────────┬─────────────────────┬───┬─────────────┬──────────────┬───────────────┬─────────┐
417-
│ plant_id_eia ┆ generator_id ┆ capacity_mw ┆ datetime ┆ … ┆ gridcharge ┆ duration_hrs ┆ roundtrip_eff ┆ reserve │
418-
│ --- ┆ --- ┆ --- ┆ --- ┆ ┆ --- ┆ --- ┆ --- ┆ --- │
419-
│ i32 ┆ str ┆ f64 ┆ datetime[μs] ┆ ┆ f32 ┆ i64 ┆ f64 ┆ f64 │
420-
╞══════════════╪══════════════╪═════════════╪═════════════════════╪═══╪═════════════╪══════════════╪═══════════════╪═════════╡
421-
│ 0 ┆ curtailment ┆ null ┆ 2020-01-01 00:00:00 ┆ … ┆ null ┆ null ┆ null ┆ null │
422-
│ 0 ┆ deficit ┆ null ┆ 2020-01-01 00:00:00 ┆ … ┆ null ┆ null ┆ null ┆ null │
423-
│ 1 ┆ 1 ┆ 350.0 ┆ 2020-01-01 00:00:00 ┆ … ┆ null ┆ null ┆ null ┆ null │
424-
│ 1 ┆ 2 ┆ 500.0 ┆ 2020-01-01 00:00:00 ┆ … ┆ null ┆ null ┆ null ┆ null │
425-
│ 2 ┆ 1 ┆ 600.0 ┆ 2020-01-01 00:00:00 ┆ … ┆ null ┆ null ┆ null ┆ null │
426-
│ 5 ┆ 1 ┆ 500.0 ┆ 2020-01-01 00:00:00 ┆ … ┆ null ┆ null ┆ null ┆ null │
427-
│ 5 ┆ es ┆ 250.0 ┆ 2020-01-01 00:00:00 ┆ … ┆ 3031.205566 ┆ 4 ┆ 0.9 ┆ 0.0 │
428-
│ 6 ┆ 1 ┆ 500.0 ┆ 2020-01-01 00:00:00 ┆ … ┆ null ┆ null ┆ null ┆ null │
429-
│ 7 ┆ 1 ┆ 200.0 ┆ 2020-01-01 00:00:00 ┆ … ┆ 184.283997 ┆ 12 ┆ 0.5 ┆ 0.0 │
430-
└──────────────┴──────────────┴─────────────┴─────────────────────┴───┴─────────────┴──────────────┴───────────────┴─────────┘
411+
>>> dm.full_output(freq="YS").collect().to_pandas() # doctest: +NORMALIZE_WHITESPACE
412+
plant_id_eia generator_id capacity_mw ... duration_hrs roundtrip_eff reserve
413+
0 0 curtailment NaN ... NaN NaN NaN
414+
1 0 deficit NaN ... NaN NaN NaN
415+
2 1 1 350.0 ... NaN NaN NaN
416+
3 1 2 500.0 ... NaN NaN NaN
417+
4 2 1 600.0 ... NaN NaN NaN
418+
5 5 1 500.0 ... NaN NaN NaN
419+
6 5 es 250.0 ... 4.0 0.9 0.0
420+
7 6 1 500.0 ... NaN NaN NaN
421+
8 7 1 200.0 ... 12.0 0.5 0.0
422+
<BLANKLINE>
423+
[9 rows x 28 columns]
431424
"""
432425
if not name and "balancing_authority_code_eia" in dispatchable_specs:
433426
name = dispatchable_specs.balancing_authority_code_eia.mode().iloc[0]
@@ -636,8 +629,9 @@ def _add_optional_cols(df: pd.DataFrame, df_name) -> pd.DataFrame:
636629
def __getstate__(self):
637630
state = {}
638631
for name in self.__slots__:
639-
if all((hasattr(self, name), name not in ("_cached", "dt_idx"))):
632+
if all((hasattr(self, name), name not in ("_cached", "dt_idx", "_polars"))):
640633
state[name] = getattr(self, name)
634+
state["polars_state"] = self._polars.__getstate__()
641635
if not self.redispatch.collect().is_empty():
642636
for df_name in ("full_output", "load_summary"):
643637
try:
@@ -653,6 +647,8 @@ def __setstate__(self, state: tuple[Any, dict]):
653647
setattr(self, k, v)
654648
self.dt_idx = self.load_profile.index
655649
self._cached = {}
650+
self._polars = IDConverter.__new__(IDConverter)
651+
self._polars.__setstate__(state["polars_state"])
656652

657653
@classmethod
658654
def from_patio(cls, *args, **kwargs) -> DispatchModel:

0 commit comments

Comments
 (0)