示例#1
0
def test_formatters_with_operator_on_df_with_nans_replaces():
    df = pd.DataFrame(np.arange(8, dtype=float).reshape(2, 4), index=['a', 'b'], columns=['aa', 'bb', 'cc', 'dd'])
    df.iloc[0, 0] = np.nan
    fmt_nan_replace = tf.FmtReplaceNaN()
    fmt_sum_rows = tf.FmtAppendTotalsRow()
    fmt_sum_columns = tf.FmtAppendTotalsColumn()

    b_no_replace = Block(df, formatters=[fmt_sum_rows, fmt_sum_columns], use_default_formatters=False)

    b_replace = Block(df, formatters=[fmt_nan_replace, fmt_sum_rows, fmt_sum_columns],
                      use_default_formatters=False)

    assert_series_equal(b_replace.df.iloc[:, -1],  b_no_replace.df.iloc[:, -1], check_dtype=False)
    assert_series_equal(b_replace.df.iloc[-1, :],  b_no_replace.df.iloc[-1, :], check_dtype=False)
示例#2
0
def test_FmtReplaceNaN():
    n = 42.0
    fmt = pbtf.FmtReplaceNaN(value=n)
    # Check basic replacement
    df = pd.concat({'a': pd.Series([0, 1, 2, np.NaN])}, axis=1)
    res = fmt._modify_dataframe(df)
    assert res.equals(pd.concat({'a': pd.Series([0, 1, 2, n])}, axis=1))
    # Check that non-nan number is preseved
    df = pd.concat({'a': pd.Series(range(0, 5))}, axis=1)
    res = fmt._modify_dataframe(df)
    assert res.equals(pd.concat({'a': pd.Series(range(0, 5))}, axis=1))
    # Check that string is preseved
    df = pd.concat({'a': pd.Series([0, 1, 2, 'foo'])}, axis=1)
    res = fmt._modify_dataframe(df)
    assert res.equals(pd.concat({'a': pd.Series([0, 1, 2, 'foo'])}, axis=1))