# Buggy code
def fetch_production(
    zone_key="CR",
    session=None,
    target_datetime=None,
    logger=logging.getLogger(__name__),
):
    # ensure we have an arrow object. if no target_datetime is specified, this defaults to now.
    target_datetime = arrow.get(target_datetime).to(TIMEZONE)

    if target_datetime < arrow.get("2012-07-01"):
        # data availability limit found by manual trial and error
        logger.error(
            "CR API does not provide data before 2012-07-01, {} was requested".format(
                target_datetime
            ),
            extra={"key": zone_key},
        )
        return None

    # Do not use existing session as some amount of cache is taking place
    r = requests.session()
    url = "https://apps.grupoice.com/CenceWeb/CencePosdespachoNacional.jsf"
    response = r.get(url)

    soup = BeautifulSoup(response.text, "html.parser")
    jsf_view_state = soup.find("input", {"name": "javax.faces.ViewState"})["value"]

    data = [
        ("formPosdespacho:txtFechaInicio_input", target_datetime.format(DATE_FORMAT)),
        ("formPosdespacho:pickFecha", ""),
        ("formPosdespacho_SUBMIT", 1),
        ("javax.faces.ViewState", jsf_view_state),
    ]
    response = r.post(url, data=data)

    # tell pandas which table to use by providing CHARACTERISTIC_NAME
    df = pd.read_html(
        response.text, match=CHARACTERISTIC_NAME, skiprows=1, index_col=0
    )[0]

    results = df_to_data(zone_key, target_datetime, df, logger)

    return results


# Traceback (ValueError)
Traceback (most recent call last):
File "/home/feeder/lib/fetch_data.py", line 130, in launch_parsers
**parser_kwargs)
File "/home/electricitymap/parsers/CR.py", line 191, in fetch_production
df = pd.read_html(response.text, match=CHARACTERISTIC_NAME, skiprows=1, index_col=0)[0]
File "/usr/local/lib/python3.6/site-packages/pandas/io/html.py", line 915, in read_html
keep_default_na=keep_default_na)
File "/usr/local/lib/python3.6/site-packages/pandas/io/html.py", line 749, in _parse
raise_with_traceback(retained)
File "/usr/local/lib/python3.6/site-packages/pandas/compat/__init__.py", line 385, in raise_with_traceback
raise exc.with_traceback(traceback)
ValueError: No tables found matching pattern 'Angostura'

# Fixed code
def fetch_production(
    zone_key="CR",
    session=None,
    target_datetime=None,
    logger=logging.getLogger(__name__),
):
    # ensure we have an arrow object.
    # if no target_datetime is specified, this defaults to now.
    target_datetime = arrow.get(target_datetime).to(TIMEZONE)

    # if before 01:30am on the current day then fetch previous day due to
    # data lag.
    today = arrow.get().to(TIMEZONE).date()
    if target_datetime.date() == today:
        target_datetime = (
            target_datetime
            if target_datetime.time() >= dt.time(1, 30)
            else target_datetime.shift(days=-1)
        )

    if target_datetime < arrow.get("2012-07-01"):
        # data availability limit found by manual trial and error
        logger.error(
            "CR API does not provide data before 2012-07-01, {} was requested".format(
                target_datetime
            ),
            extra={"key": zone_key},
        )
        return None

    # Do not use existing session as some amount of cache is taking place
    r = requests.session()
    url = "https://apps.grupoice.com/CenceWeb/CencePosdespachoNacional.jsf"
    response = r.get(url)

    soup = BeautifulSoup(response.text, "html.parser")
    jsf_view_state = soup.find("input", {"name": "javax.faces.ViewState"})["value"]

    data = [
        ("formPosdespacho:txtFechaInicio_input", target_datetime.format(DATE_FORMAT)),
        ("formPosdespacho:pickFecha", ""),
        ("formPosdespacho_SUBMIT", 1),
        ("javax.faces.ViewState", jsf_view_state),
    ]
    response = r.post(url, data=data)

    # tell pandas which table to use by providing CHARACTERISTIC_NAME
    df = pd.read_html(
        response.text, match=CHARACTERISTIC_NAME, skiprows=1, index_col=0
    )[0]

    results = df_to_data(zone_key, target_datetime, df, logger)

    return results