def test_import_optional():
    match = "Missing .*notapackage.* pip .* conda .* notapackage"
    with pytest.raises(ImportError, match=match):
        import_optional_dependency("notapackage")

    result = import_optional_dependency("notapackage", raise_on_missing=False)
    assert result is None
def test_no_version_raises():
    name = "fakemodule"
    module = types.ModuleType(name)
    sys.modules[name] = module
    VERSIONS[name] = "1.0.0"

    with pytest.raises(ImportError, match="Can't determine .* fakemodule"):
        import_optional_dependency(name)
def test_bad_version():
    name = "fakemodule"
    module = types.ModuleType(name)
    module.__version__ = "0.9.0"
    sys.modules[name] = module
    VERSIONS[name] = "1.0.0"

    match = "microdf requires .*1.0.0.* of .fakemodule.*'0.9.0'"
    with pytest.raises(ImportError, match=match):
        import_optional_dependency("fakemodule")

    with tm.assert_produces_warning(UserWarning):
        result = import_optional_dependency("fakemodule", on_version="warn")
    assert result is None

    module.__version__ = "1.0.0"  # exact match is OK
    result = import_optional_dependency("fakemodule")
    assert result is module
示例#4
0
def static_baseline_calc(recs, year):
    """Creates a static Calculator object.

    :param recs: Records object.
    :param year: Year to advance to.
    :returns: Calculator object.

    """
    tc = import_optional_dependency("taxcalc")
    calc = tc.Calculator(records=recs, policy=tc.Policy())
    calc.advance_to_year(year)
    calc.calc_all()
    return calc
示例#5
0
def calc_df(
    records=None,
    policy=None,
    year=2020,
    reform=None,
    group_vars=None,
    metric_vars=None,
    group_n65=False,
):
    """Creates a pandas DataFrame for given Tax-Calculator data.

    s006 is always included, and RECID is used as an index.

    :param records: An optional Records object. If not provided, uses CPS
        records. (Default value = None)
    :param policy: An optional Policy object. If not provided, uses default
            Policy.
    :param year: An optional year to advance to. If not provided, defaults to
            2020.
    :param reform: An optional reform to implement for the Policy object.
        (Default value = None)
    :param group_vars: An optional list of column names to include in the
            DataFrame. (Default value = None)
    :param metric_vars: An optional list of column names to include and
        calculate weighted sums of (in millions named as *_m) in the DataFrame.
        (Default value = None)
    :param group_n65: Whether to calculate and group by n65. Defaults to False.
    :returns: A pandas DataFrame. market_income is also always calculated.

    """
    tc = import_optional_dependency("taxcalc")
    # Assign defaults.
    if records is None:
        records = tc.Records.cps_constructor()
    if policy is None:
        policy = tc.Policy()
    if reform is not None:
        policy.implement_reform(reform)
    # Calculate.
    calc = tc.Calculator(records=records, policy=policy, verbose=False)
    calc.advance_to_year(year)
    calc.calc_all()
    # Get a deduplicated list of all columns.
    if group_n65:
        group_vars = group_vars + [
            "age_head",
            "age_spouse",
            "elderly_dependents",
        ]
    # Include expanded_income and benefits to produce market_income.
    all_cols = mdf.listify(
        [
            "RECID",
            "s006",
            "expanded_income",
            "aftertax_income",
            mdf.BENS,
            group_vars,
            metric_vars,
        ]
    )
    df = calc.dataframe(all_cols)
    # Create core elements.
    df["market_income"] = mdf.market_income(df)
    df["bens"] = df[mdf.BENS].sum(axis=1)
    df["tax"] = df.expanded_income - df.aftertax_income
    if group_n65:
        df["n65"] = n65(df.age_head, df.age_spouse, df.elderly_dependents)
        df.drop(
            ["age_head", "age_spouse", "elderly_dependents"],
            axis=1,
            inplace=True,
        )
    # Add calculated columns for metrics.
    mdf.add_weighted_metrics(df, metric_vars)
    # Set RECID to int and set it as index before returning.
    df["RECID"] = df.RECID.map(int)
    return df.set_index("RECID")
def test_xlrd_version_fallback():
    """ """
    pytest.importorskip("xlrd")
    import_optional_dependency("xlrd")
示例#7
0
def calc_df(records=None,
            policy=None,
            year=2019,
            reform=None,
            group_vars=None,
            metric_vars=None,
            group_n65=False):
    """Creates a pandas DataFrame for given Tax-Calculator data.

    s006 is always included, and RECID is used as an index.

    Args:
        records: An optional Records object. If not provided, uses CPS records.
        policy: An optional Policy object. If not provided, uses default
            Policy.
        year: An optional year to advance to. If not provided, defaults to
            2019.
        reform: An optional reform to implement for the Policy object.
        group_vars: An optional list of column names to include in the
            DataFrame.
        metric_vars: An optional list of column names to include and calculate
             weighted sums of (in millions named as *_m) in the DataFrame.
        group_n65: Whether to calculate and group by n65. Defaults to False.

    Returns:
        A pandas DataFrame. market_income is also always calculated.
    """
    tc = import_optional_dependency("taxcalc")
    # Assign defaults.
    if records is None:
        records = tc.Records.cps_constructor()
    if policy is None:
        policy = tc.Policy()
    if reform is not None:
        policy.implement_reform(reform)
    # Calculate.
    calc = tc.Calculator(records=records, policy=policy, verbose=False)
    calc.advance_to_year(year)
    calc.calc_all()
    # TODO: Make n65, ECI, etc. part of the list of columns you can request.
    # Get a deduplicated list of all columns.
    if group_n65:
        group_vars = group_vars + ['age_head', 'age_spouse',
                                   'elderly_dependents']
    # Include expanded_income and benefits to produce market_income.
    all_cols = mdf.listify(
        ['RECID', 's006', 'expanded_income', 'aftertax_income',
         mdf.BENS, group_vars, metric_vars])
    df = calc.dataframe(all_cols)
    # Create core elements.
    df['market_income'] = mdf.market_income(df)
    df['bens'] = df[mdf.BENS].sum(axis=1)
    df['tax'] = df.expanded_income - df.aftertax_income
    if group_n65:
        df['n65'] = n65(df.age_head, df.age_spouse, df.elderly_dependents)
        df.drop(['age_head', 'age_spouse', 'elderly_dependents'], axis=1,
                inplace=True)
    # Add calculated columns for metrics.
    mdf.add_weighted_metrics(df, metric_vars)
    # Set RECID to int and set it as index before returning.
    df['RECID'] = df.RECID.map(int)
    return df.set_index('RECID')