|
10 | 10 | from .paths import DatasetPath, DatasetPathCollection |
11 | 11 | from .quiet import silent, suppress_stdout_stderr |
12 | 12 | from .timeseries import RegularTimeseries |
| 13 | +from .timeseries.interval import Interval |
13 | 14 |
|
14 | 15 | module_engine = ModuleEngine() |
15 | 16 |
|
@@ -349,6 +350,73 @@ def write_rts(self, path: DatasetPath | str, rts: RegularTimeseries): |
349 | 350 | with suppress_stdout_stderr(): |
350 | 351 | return self.engine.write_rts(path, rts) |
351 | 352 |
|
| 353 | + def write_dataframe( |
| 354 | + self, |
| 355 | + paths: list[DatasetPath], |
| 356 | + df: pd.DataFrame, |
| 357 | + units: str | list[str], |
| 358 | + period_types: str | list[str], |
| 359 | + intervals: str | Interval | list[str | Interval], |
| 360 | + ): |
| 361 | + """Write DSS data from a DataFrame, where each column is a RegularTimeseries |
| 362 | +
|
| 363 | + Parameters |
| 364 | + ---------- |
| 365 | + paths : list[DatasetPath] |
| 366 | + The paths to write to in the DSS file. |
| 367 | + df : pd.DataFrame |
| 368 | + The data to convert into a DSS file. |
| 369 | + units : str | list[str] |
| 370 | + The units to use for the data in the DSS file. |
| 371 | + period_types : str | list[str] |
| 372 | + The DSS period types to use for the data in the DSS file. |
| 373 | + intervals : str | Interval | list[str | Interval] |
| 374 | + The DSS intervals to use for the data in the DSS file. |
| 375 | +
|
| 376 | + Raises |
| 377 | + ------ |
| 378 | + ValueError |
| 379 | + Raised when the data given cannot be assigned uniquely to the columns of the |
| 380 | + DataFrame, or when a RegularTimeseries object cannot be created. |
| 381 | +
|
| 382 | + """ |
| 383 | + if isinstance(units, str): |
| 384 | + units = [units for _ in paths] |
| 385 | + if isinstance(period_types, str): |
| 386 | + period_types = [period_types for _ in paths] |
| 387 | + if isinstance(intervals, (str, Interval)): |
| 388 | + intervals = [intervals for _ in paths] |
| 389 | + |
| 390 | + try: |
| 391 | + data = zip( |
| 392 | + paths, |
| 393 | + df.columns, |
| 394 | + units, |
| 395 | + period_types, |
| 396 | + intervals, |
| 397 | + ) |
| 398 | + except Exception as e: |
| 399 | + raise ValueError( |
| 400 | + "The lengths of the data given do not match," |
| 401 | + + " could not determine a 1-to-1 retionship between inputs" |
| 402 | + + " and dataframe columns" |
| 403 | + ) from e |
| 404 | + for p, col, unit, pt, inter in data: |
| 405 | + series = df[col] |
| 406 | + try: |
| 407 | + rts = RegularTimeseries.from_series( |
| 408 | + s=series, |
| 409 | + path=p, |
| 410 | + units=unit, |
| 411 | + period_type=pt, |
| 412 | + interval=inter, |
| 413 | + ) |
| 414 | + except Exception as e: |
| 415 | + raise ValueError( |
| 416 | + "Could not create a RegularTimeseries object from the data given." |
| 417 | + ) from e |
| 418 | + self.write_rts(path=p, rts=rts) |
| 419 | + |
352 | 420 | def resolve_wildcard( |
353 | 421 | self, |
354 | 422 | path: DatasetPath | str, |
|
0 commit comments