Example #1
0
def test_ops(tup):
    a, b = tup
    v1 = mpd.MoneyArray(a, 'GBP')
    v2 = mpd.MoneyArray(b, 'GBP')

    r1 = v1 <= v2
    r2 = v2 >= v1
    tm.assert_numpy_array_equal(r1, r2)
def test_factorize():
    arr = mpd.MoneyArray([1, 1, 10, 10], 'JPY')
    labels, uniques = pd.factorize(arr)

    expected_labels = np.array([0, 0, 1, 1])
    tm.assert_numpy_array_equal(labels, expected_labels)

    expected_uniques = mpd.MoneyArray([1, 10], 'JPY')
    assert uniques.equals(expected_uniques)
Example #3
0
def test_dropna():
    missing = pd.Series(mpd.MoneyArray([1, 0], 'USD'))
    result = missing.dropna()
    expected = pd.Series(mpd.MoneyArray([1], 'USD'))
    tm.assert_series_equal(result, expected)

    result = missing.to_frame().dropna()
    expected = expected.to_frame()
    tm.assert_frame_equal(result, expected)
def test_dataframe_from_series_no_dict():
    s = pd.Series(mpd.MoneyArray([1, 2, 3], 'INR'))
    result = pd.DataFrame(s)
    expected = pd.DataFrame({0: s})
    tm.assert_frame_equal(result, expected)

    s = pd.Series(mpd.MoneyArray([1, 2, 3], 'INR'), name='A')
    result = pd.DataFrame(s)
    expected = pd.DataFrame({'A': s})
    tm.assert_frame_equal(result, expected)
Example #5
0
def test_reindex(frame):
    result = frame.reindex([0, 10])
    expected = pd.DataFrame(
        {
            "A": mpd.MoneyArray([None, np.nan], 'USD'),
            "B": [0, np.nan],
            "C": mpd.MoneyArray([None, np.nan], 'USD')
        },
        index=[0, 10])
    tm.assert_frame_equal(result, expected)
Example #6
0
def test_bytes_roundtrip():
    arr = mpd.MoneyArray([1, 2, 3], 'USD')
    bytestring = arr.to_bytes()
    assert isinstance(bytestring, bytes)

    result = mpd.MoneyArray.from_bytes(bytestring)
    assert result.equals(arr)
Example #7
0
def test_comparison_raises(op):
    arr = mpd.MoneyArray([0, 1, 2], 'JPY')
    with pytest.raises(TypeError):
        op(arr, 'a')

    with pytest.raises(TypeError):
        op('a', arr)
Example #8
0
def test_repr_works():
    values = mpd.MoneyArray([0, 1, 2, 3], 'GBP')
    result = repr(values)
    expected = (
        "<MoneyArray[GBP]>\n[GBP 0.00, GBP 1.00, GBP 2.00, GBP 3.00]\nLength: 4, dtype: money"
    )
    assert result == expected
def test_groupby_make_grouper():
    df = pd.DataFrame({
        "A": [1, 1, 2, 2],
        "B": mpd.MoneyArray([1, 1, 2, 2], 'EUR')
    })
    gr = df.groupby("B")
    result = gr.grouper.groupings[0].grouper
    assert result.equals(df.B.values)
Example #10
0
def test_isna():
    v = mpd.MoneyArray([None, 2], 'GBP')
    r1 = v.isna()
    r2 = pd.isna(v)
    expected = np.array([True, False])

    np.testing.assert_array_equal(r1, expected)
    np.testing.assert_array_equal(r2, expected)
Example #11
0
def test_unique():
    arr = mpd.MoneyArray([3, 3, 1, 2, 3], 'USD')
    result = arr.unique()
    assert isinstance(result, mpd.MoneyArray)

    result = result.astype(object)
    expected = pd.unique(arr.astype(object))
    tm.assert_numpy_array_equal(result, expected)
Example #12
0
def test_iter_works():
    x = mpd.MoneyArray([0, 1, 2], 'GBP')
    result = list(x)
    expected = [
        money.XMoney(0, 'GBP'),
        money.XMoney(1, 'GBP'),
        money.XMoney(2, 'GBP'),
    ]
    assert result == expected
Example #13
0
def test_to_pymoney():
    v = mpd.MoneyArray([1, 2, 3], 'USD')
    result = v.to_pymoney()
    expected = [
        money.XMoney(1, 'USD'),
        money.XMoney(2, 'USD'),
        money.XMoney(3, 'USD'),
    ]
    assert result == expected
Example #14
0
def test_array():
    v = mpd.MoneyArray([1, 2, 3], 'GBP')
    result = np.array(v)
    expected = np.array([
        money.XMoney(1, 'GBP'),
        money.XMoney(2, 'GBP'),
        money.XMoney(3, 'GBP'),
    ])
    tm.assert_numpy_array_equal(result, expected)
Example #15
0
def test_factorize():
    arr = mpd.MoneyArray([3, 3, 1, 2, 3], 'USD')
    labels, uniques = arr.factorize()
    expected_labels, expected_uniques = pd.factorize(arr.astype(object))

    assert isinstance(uniques, mpd.MoneyArray)

    uniques = uniques.astype(object)
    tm.assert_numpy_array_equal(labels, expected_labels)
    tm.assert_numpy_array_equal(uniques, expected_uniques)
def test_groupby_make_grouper_groupings():
    df = pd.DataFrame({
        "A": [1, 1, 2, 2],
        "B": mpd.MoneyArray([1, 1, 2, 2], 'EUR')
    })
    p1 = df.groupby("A").grouper.groupings[0]
    p2 = df.groupby("B").grouper.groupings[0]

    result = {int(k): v for k, v in p2.groups.items()}
    assert result.keys() == p1.groups.keys()
    for k in result.keys():
        assert result[k].equals(p1.groups[k])
Example #17
0
def test_getitem_scalar():
    ser = mpd.MoneyArray([0, 1, 2], 'USD')
    result = ser[1]
    assert result == money.XMoney(1, 'USD')
Example #18
0
def test_from_ndarray(values):
    result = mpd.MoneyArray(np.asarray(values), 'USD')
    expected = mpd.MoneyArray(values, 'USD')
    assert result.equals(expected)
Example #19
0
def test_value_counts():
    x = mpd.MoneyArray([0, 0, 1], 'USD')
    result = x.value_counts()
    assert len(result)
Example #20
0
def data_missing_for_sorting():
    return mpd.MoneyArray([2, None, 1], default_money_code='GBP')
Example #21
0
def data_missing():
    return mpd.MoneyArray([np.nan, 1], 'USD')
Example #22
0
def test_todecimal():
    values = [0, 1, 2]
    arr = mpd.MoneyArray(values, 'EUR')
    result = arr.to_decimals()
    assert all([r == decimal.Decimal(v) for r, v in zip(result, values)])
Example #23
0
def test_tolist():
    v = mpd.MoneyArray([1, 2, 3], 'USD')
    result = v.tolist()
    expected = [(1, 'USD'), (2, 'USD'), (3, 'USD')]
    assert result == expected
Example #24
0
def test_make_container():
    values = mpd.MoneyArray([1, 2, 3], 'GBP')
    npt.assert_array_equal(
        values.data,
        np.array([(1, 'GBP'), (2, 'GBP'), (3, 'GBP')],
                 dtype=values.dtype._record_type))
Example #25
0
def data():
    ma = mpd.MoneyArray(list(range(1, 101)), 'USD')
    return ma
Example #26
0
def test_getitem_slice():
    ser = mpd.MoneyArray([0, 1, 2], 'USD')
    result = ser[1:]
    expected = mpd.MoneyArray([1, 2], 'USD')
    assert result.equals(expected)
Example #27
0
def data_for_sorting():
    return mpd.MoneyArray([10, 123, 1], default_money_code='GBP')
Example #28
0
def test_setitem_scalar(value):
    ser = mpd.MoneyArray([0, 1, 2], 'USD')
    ser[1] = value
    expected = mpd.MoneyArray([0, 123, 2], 'USD')
    assert ser.equals(expected)
Example #29
0
def data_for_grouping():
    b = 1
    a = 233
    c = 242
    return mpd.MoneyArray([b, b, np.nan, None, a, a, b, c], 'USD')
Example #30
0
def test_setitem_array():
    ser = mpd.MoneyArray([0, 1, 2], 'USD')
    ser[[1, 2]] = ['10 USD', '20 USD']
    expected = mpd.MoneyArray([0, 10, 20], 'USD')
    assert ser.equals(expected)