Exemplo n.º 1
0
def _fetch_series(zone_key, series_id, session=None, target_datetime=None,
                  logger=None):
    """Fetches and converts a data series."""

    s = session or requests.Session()

    # local import to avoid the exception that happens if EIAPY token is not set
    # even if this module is unused
    from eiapy import Series
    series = Series(series_id=series_id, session=s)

    if target_datetime:
        utc = tz.gettz('UTC')
        #eia currently only accepts utc timestamps in the form YYYYMMDDTHHZ
        end = target_datetime.astimezone(utc).strftime('%Y%m%dT%HZ')
        start = (target_datetime.astimezone(utc) - datetime.timedelta(days=1)).strftime('%Y%m%dT%HZ')
        raw_data = series.get_data(start=start, end=end)
    else:
        # Get the last 24 hours available.
        raw_data = series.last(24)

    # UTC timestamp with no offset returned.
    if not raw_data.get('series'):
        # Series doesn't exist. Probably requesting a fuel from a region that
        # doesn't have any capacity for that fuel type.
        return []

    return [{
        'zoneKey': zone_key,
        'datetime': parser.parse(datapoint[0]),
        'value': datapoint[1],
        'source': 'eia.gov',
    } for datapoint in raw_data['series'][0]['data']]
Exemplo n.º 2
0
def fetchEIA(series_ID, update=True):
    try:
        sr = Series(series_ID)
        if update:
            data = sr.last(20)['series'][0]['data']
        else:
            data = sr.get_data(all_data=True)['series'][0]['data']
        df = pd.DataFrame.from_records(data, columns=['date', 'value'])
        df['date'] = pd.to_datetime(df['date'])
        df.dropna(inplace=True)
        return df
    except:
        raise fetchEIA_Error('Fetching EIA failed')
Exemplo n.º 3
0
def _fetch_series(zone_key,
                  series_id,
                  session=None,
                  target_datetime=None,
                  logger=None):
    """Fetches and converts a data series."""

    s = session or requests.Session()

    # local import to avoid the exception that happens if EIAPY token is not set
    # even if this module is unused
    from eiapy import Series

    series = Series(series_id=series_id, session=s)

    if target_datetime:
        utc = tz.gettz("UTC")
        # eia currently only accepts utc timestamps in the form YYYYMMDDTHHZ
        end = target_datetime.astimezone(utc).strftime("%Y%m%dT%HZ")
        start = (target_datetime.astimezone(utc) -
                 datetime.timedelta(days=1)).strftime("%Y%m%dT%HZ")
        raw_data = series.get_data(start=start, end=end)
    else:
        # Get the last 24 hours available.
        raw_data = series.last(24)

    eia_error_message = raw_data.get("data", {}).get("error")
    if eia_error_message:
        logger.error(
            f"EIA error, for series_id [{series_id}]: {eia_error_message}")
        return []

    # UTC timestamp with no offset returned.
    if not raw_data.get("series"):
        # Series doesn't exist. Probably requesting a fuel from a region that
        # doesn't have any capacity for that fuel type.
        return []

    return [{
        "zoneKey": zone_key,
        "datetime": parser.parse(datapoint[0]),
        "value": datapoint[1],
        "source": "eia.gov",
    } for datapoint in raw_data["series"][0]["data"]]