Пример #1
0
import numpy as np
import pandas as pd
import pytest
from hypothesis import given
from hypothesis import strategies as st
from pandas.testing import assert_series_equal

from janitor.testing_utils.strategies import df_strategy


@pytest.mark.functions
@given(df=df_strategy())
def test_add_column_add_integer(df):
    # col_name wasn't a string
    with pytest.raises(TypeError):
        df.add_column(column_name=42, value=42)


@pytest.mark.functions
@given(df=df_strategy())
def test_add_column_already_exists(df):
    # column already exists
    with pytest.raises(ValueError):
        df.add_column("a", 42)


@pytest.mark.functions
@given(df=df_strategy())
def test_add_column_too_many(df):
    # too many values for dataframe num rows:
    with pytest.raises(ValueError):
Пример #2
0
import pandas as pd
import pytest
from hypothesis import given

from janitor.errors import JanitorError
from janitor.testing_utils.strategies import df_strategy


@pytest.mark.functions
@given(df=df_strategy())
def test_clean_names_method_chain(df):
    df = df.clean_names()
    expected_columns = [
        "a",
        "bell_chart",
        "decorated_elephant",
        "animals@#$%^",
        "cities",
    ]
    assert set(df.columns) == set(expected_columns)


@pytest.mark.functions
@given(df=df_strategy())
def test_clean_names_special_characters(df):
    df = df.clean_names(remove_special=True)
    expected_columns = [
        "a",
        "bell_chart",
        "decorated_elephant",
        "animals",
Пример #3
0
import numpy as np
import pandas as pd
import pytest
from hypothesis import assume, given
from hypothesis import strategies as st
from hypothesis.extra.numpy import arrays

from janitor.testing_utils.strategies import df_strategy


@pytest.mark.test
@pytest.mark.functions
@given(
    df=df_strategy(),
    x_vals=st.floats(),
    n_yvals=st.integers(min_value=0, max_value=100),
)
def test_add_columns(df, x_vals, n_yvals):
    """
    Test for adding multiple columns at the same time.
    """
    y_vals = np.linspace(0, 42, n_yvals)

    if n_yvals != len(df) or n_yvals == 0:
        with pytest.raises(ValueError):
            df = df.add_columns(x=x_vals, y=y_vals)

    else:
        df = df.add_columns(x=x_vals, y=y_vals)
        series = pd.Series([x_vals] * len(df))
        series.name = "x"