|
59 | 59 | from .offsets import generate_range, to_offset |
60 | 60 | from .ops import inject_ops_methods |
61 | 61 | from .plotting import inject_plotting_methods |
62 | | -from .pyam_compat import IamDataFrame, LongDatetimeIamDataFrame |
63 | 62 | from .time import _TARGET_DTYPE, TimePoints, TimeseriesConverter |
64 | 63 | from .units import UnitConverter |
65 | 64 |
|
|
75 | 74 |
|
76 | 75 | from scmdata.groupby import RunGroupBy |
77 | 76 |
|
| 77 | + from .pyam_compat import LongDatetimeIamDataFrame |
| 78 | + |
78 | 79 | P = ParamSpec("P") |
79 | 80 |
|
80 | 81 |
|
@@ -513,6 +514,9 @@ def _init_timeseries( |
513 | 514 | copy_data: bool = False, |
514 | 515 | **kwargs: Any, |
515 | 516 | ) -> None: |
| 517 | + # Lazy load |
| 518 | + from .pyam_compat import IamDataFrame |
| 519 | + |
516 | 520 | if isinstance(data, np.ndarray): |
517 | 521 | if columns is None: |
518 | 522 | raise ValueError("`columns` argument is required") |
@@ -871,7 +875,7 @@ def timeseries( |
871 | 875 | raise NonUniqueMetadataError(_meta) |
872 | 876 |
|
873 | 877 | if time_axis is None: |
874 | | - columns = self._time_points.to_index() |
| 878 | + columns = self._time_points.to_index().infer_objects() |
875 | 879 | elif time_axis == "year": |
876 | 880 | columns = self._time_points.years() |
877 | 881 | elif time_axis == "year-month": |
@@ -902,8 +906,11 @@ def calc_seconds(x): |
902 | 906 | if len(np.unique(columns)) != len(columns): |
903 | 907 | raise ValueError(f"Ambiguous time values with time_axis = '{time_axis}'") |
904 | 908 |
|
905 | | - df.columns = pd.Index(columns, name="time") |
906 | 909 | df.index = pd.MultiIndex.from_frame(_meta) |
| 910 | + if isinstance(columns, pd.Index): |
| 911 | + df.columns = columns |
| 912 | + else: |
| 913 | + df.columns = pd.Index(columns, name="time") |
907 | 914 |
|
908 | 915 | if drop_all_nan_times: |
909 | 916 | df = df.dropna(how="all", axis="columns") |
@@ -2366,6 +2373,9 @@ def to_iamdataframe(self) -> LongDatetimeIamDataFrame: # pragma: no cover |
2366 | 2373 | ImportError |
2367 | 2374 | If `pyam <https://github.com/IAMconsortium/pyam>`_ is not installed |
2368 | 2375 | """ |
| 2376 | + # Lazy load |
| 2377 | + from .pyam_compat import LongDatetimeIamDataFrame |
| 2378 | + |
2369 | 2379 | if LongDatetimeIamDataFrame is None: |
2370 | 2380 | raise ImportError( |
2371 | 2381 | "pyam is not installed. Features involving IamDataFrame are unavailable" |
@@ -2617,9 +2627,10 @@ def run_append( # noqa: PLR0912, PLR0915 |
2617 | 2627 | ret._df = pd.concat([ret._df, *to_join_dfs], axis="columns").sort_index() |
2618 | 2628 | ret._time_points = TimePoints(ret._df.index.values) |
2619 | 2629 | ret._df.index = ret._time_points.to_index() |
2620 | | - ret._meta = pd.MultiIndex.from_frame( |
2621 | | - pd.concat([ret._meta.to_frame(), *to_join_metas]).astype("category") |
2622 | | - ) |
| 2630 | + if not all(m.empty for m in to_join_metas): |
| 2631 | + ret._meta = pd.MultiIndex.from_frame( |
| 2632 | + pd.concat([ret._meta.to_frame(), *to_join_metas]).astype("category") |
| 2633 | + ) |
2623 | 2634 |
|
2624 | 2635 | if ret._duplicated_meta(): |
2625 | 2636 | if overlapping_times and duplicate_msg: |
|
0 commit comments