Source code for scmdata.pyam_compat

Imports and classes required to ensure compatibility with Pyam is intelligently handled.
import datetime

from dateutil import parser

    from pyam import IamDataFrame  # type: ignore

    # mypy can't work out try-except block forces IamDataFrame to be here
[docs] class LongDatetimeIamDataFrame(IamDataFrame): # type: ignore """ Custom implementation of :class:`pyam.IamDataFrame` which handles long datetime data. This custom implementation allows the data frame to handle times outside panda's limited time range of 1677-09-22 00:12:43.145225 to 2262-04-11 23:47:16.854775807, see `this discussion <>`_. TODO: use np.datetime64 instead """ def _format_datetime_col(self): if["time"].apply(lambda x: isinstance(x, str)).any(): def convert_str_to_datetime(inp): try: return parser.parse(inp) except ValueError: return inp["time"] =["time"].apply(convert_str_to_datetime) not_datetime = [ not isinstance(x, (datetime.datetime, for x in["time"] ] if any(not_datetime): bad_values =[not_datetime]["time"] error_msg = ( "All time values must be convertible to datetime. The following " f"values are not:\n{bad_values}" ) raise ValueError(error_msg)
except ImportError: # pragma: no cover # mypy can't work out try-except block sets typing IamDataFrame = None LongDatetimeIamDataFrame = None # type: ignore __all__ = ["IamDataFrame", "LongDatetimeIamDataFrame"]