Ejemplo n.º 1
0
def test_series_fillna_invalid_dtype(data_dtype):
    gdf = Series([1, 2, None, 3], dtype=data_dtype)
    fill_value = 2.5
    with pytest.raises(TypeError) as raises:
        gdf.fillna(fill_value)
    raises.match("Cannot safely cast non-equivalent {} to {}".format(
        np.dtype(type(fill_value)).type.__name__, gdf.dtype.type.__name__))
Ejemplo n.º 2
0
def test_series_replace():
    a1 = np.array([0, 1, 2, 3, 4])

    # Numerical
    a2 = np.array([5, 1, 2, 3, 4])
    sr1 = Series(a1)
    sr2 = sr1.replace(0, 5)
    np.testing.assert_equal(sr2.to_array(), a2)

    # Categorical
    psr3 = pd.Series(["one", "two", "three"], dtype="category")
    psr4 = psr3.replace("one", "two")
    sr3 = Series.from_pandas(psr3)
    sr4 = sr3.replace("one", "two")
    pd.testing.assert_series_equal(sr4.to_pandas(), psr4)

    # List input
    a6 = np.array([5, 6, 2, 3, 4])
    sr6 = sr1.replace([0, 1], [5, 6])
    np.testing.assert_equal(sr6.to_array(), a6)

    a7 = np.array([5.5, 6.5, 2, 3, 4])
    sr7 = sr1.replace([0, 1], [5.5, 6.5])
    np.testing.assert_equal(sr7.to_array(), a7)

    # Series input
    a8 = np.array([5, 5, 5, 3, 4])
    sr8 = sr1.replace(sr1[:3], 5)
    np.testing.assert_equal(sr8.to_array(), a8)

    # large input containing null
    sr9 = Series(list(range(400)) + [None])
    sr10 = sr9.replace([22, 323, 27, 0], None)
    assert sr10.null_count == 5
    assert len(sr10.to_array()) == (401 - 5)

    sr11 = sr9.replace([22, 323, 27, 0], -1)
    assert sr11.null_count == 1
    assert len(sr11.to_array()) == (401 - 1)

    # large input not containing nulls
    sr9 = sr9.fillna(-11)
    sr12 = sr9.replace([22, 323, 27, 0], None)
    assert sr12.null_count == 4
    assert len(sr12.to_array()) == (401 - 4)

    sr13 = sr9.replace([22, 323, 27, 0], -1)
    assert sr13.null_count == 0
    assert len(sr13.to_array()) == 401
Ejemplo n.º 3
0
def test_series_fillna_numerical(dtype, fill_type, null_value, inplace):
    data = np.array([0, 1, null_value, 2, null_value], dtype='float64')
    sr = Series(data).astype(dtype)

    if fill_type == 'scalar':
        fill_value = np.random.randint(0, 5)
        expect = np.array([0, 1, fill_value, 2, fill_value], dtype=dtype)
    elif fill_type == 'series':
        fill_value = Series(np.random.randint(0, 5, (5, )))
        expect = np.array([0, 1, fill_value[2], 2, fill_value[4]], dtype=dtype)

    got = sr.fillna(fill_value, inplace=inplace)

    if inplace:
        got = sr
    else:
        got = got.to_array()

    np.testing.assert_equal(expect, got)
Ejemplo n.º 4
0
def test_series_fillna_numerical(data_dtype, fill_dtype, fill_type, null_value,
                                 inplace):
    # TODO: These tests should use Pandas' nullable int type
    # when we support a recent enough version of Pandas
    # https://pandas.pydata.org/pandas-docs/stable/user_guide/integer_na.html

    if fill_type == 'scalar':
        fill_value = np.random.randint(0, 5)
        expect = np.array([0, 1, fill_value, 2, fill_value], dtype=data_dtype)
    elif fill_type == 'series':
        data = np.random.randint(0, 5, (5, ))
        fill_value = pd.Series(data, dtype=data_dtype)
        expect = np.array([0, 1, fill_value[2], 2, fill_value[4]],
                          dtype=data_dtype)

    sr = Series([0, 1, null_value, 2, null_value], dtype=data_dtype)
    result = sr.fillna(fill_value, inplace=inplace)

    if inplace:
        result = sr

    got = result.to_array()

    np.testing.assert_equal(expect, got)