コード例 #1
0
def test_annotation_stripes_continuous_transformed():
    pdf = mtcars.assign(am=pd.Categorical(mtcars.am))
    p = (ggplot(pdf) +
         annotation_stripes(fills=["red", "green", "blue"], alpha=0.1) +
         geom_jitter(aes("hp", "wt", color="am"), random_state=5) +
         scale_x_continuous(trans='log2'))
    assert p == "annotation_stripes_continuous_transformed"
コード例 #2
0
def test_annotation_stripes_continuous():
    pdf = mtcars.assign(am=pd.Categorical(mtcars.am))
    p = (ggplot(pdf) + annotation_stripes(
        fills=["red", "green", "blue"], alpha=0.4, size=1, linetype="dashed") +
         geom_jitter(aes("gear", "wt", color="am"), random_state=5))

    assert p == "annotation_stripes_continuous"
コード例 #3
0
def test_as_type_kwargs():
    with pytest.raises(ValueError):
        dp(mtcars).astype(X.columns, int).pd
    actual = dp(mtcars).astype(X.columns, int, errors="ignore").pd
    should = mtcars.assign(
        **{x: mtcars[x].astype(int)
           for x in mtcars.columns if x != "name"})
    assert_frame_equal(should, actual)
コード例 #4
0
def test_select_inverse_with_minus_column_and_normal_column():
    # the rule is: if it's ambigous, raise
    m2 = mtcars.assign(**{"-name": mtcars.name})
    with pytest.raises(ValueError):  # this one is ambigous
        dp(m2).select("-name").pd
    actual = dp(m2).select("--name").pd  # this one isn't
    assert (actual.columns == [x for x in m2.columns if x not in ["-name"]]).all()
    with pytest.raises(KeyError):
        dp(m2).select("---name").pd  # and this one is not defined
コード例 #5
0
def test_groupby_two_mutate_grouped():
    actual = (dp(mtcars).groupby(["cyl", "vs"]).mutate(
        grp_rank={grp: sub_df.hp.rank()
                  for (grp, sub_df) in X.itergroups()}).select(
                      "grp_rank").ungroup().pd.sort_index())
    ac = []
    for grp, sub_df in mtcars.groupby(["cyl", "vs"]):
        x = sub_df["hp"].rank()
        ac.append(x)
    ac = pd.concat(ac)
    should = mtcars.assign(grp_rank=ac)[["cyl", "vs", "grp_rank"]]
    assert_frame_equal(should, actual)
コード例 #6
0
def test_grouped_mutate_callable():
    actual = (dp(mtcars).groupby("cyl").mutate(
        max_hp=lambda x: x["hp"].max()).select(["cyl", "max_hp",
                                                "name"]).ungroup().pd)
    ac = []
    for grp, sub_df in mtcars.groupby("cyl"):
        x = pd.Series(sub_df["hp"].max(), index=sub_df.index)
        ac.append(x)
    ac = pd.concat(ac)
    should = mtcars.assign(max_hp=ac)[["cyl", "max_hp",
                                       "name"]].sort_values("name")
    assert_frame_equal(should, actual.sort_values("name"))
コード例 #7
0
def test_annotation_stripes_double():
    pdf = mtcars.assign(gear=pd.Categorical(mtcars.gear),
                        am=pd.Categorical(mtcars.am))
    p = (
        ggplot(pdf) + annotation_stripes(
            fills=["#0000FF", "#FF0000"], alpha=0.3, direction='vertical') +
        annotation_stripes(
            fills=["#AAAAAA", "#FFFFFF"], alpha=0.3, direction='horizontal') +
        geom_jitter(aes("gear", "wt", shape="gear", color="am"),
                    random_state=5) +
        scale_shape_discrete(guide=guide_legend(order=1))  # work around #229
    )
    assert p == "annotation_stripes_double"
コード例 #8
0
def test_annotation_stripes_coord_flip():
    pdf = mtcars.assign(gear=pd.Categorical(mtcars.gear),
                        am=pd.Categorical(mtcars.am))
    p = (
        ggplot(pdf) + annotation_stripes(
            fills=["#AAAAAA", "#FFFFFF", "#7F7FFF"], alpha=0.3) + geom_jitter(
                aes("gear", "wt", shape="gear", color="am"), random_state=5) +
        geom_vline(xintercept=0.5, color="black") +
        geom_vline(xintercept=1.5, color="black") +
        geom_vline(xintercept=2.5, color="black") +
        geom_vline(xintercept=3.5, color="black") +
        scale_shape_discrete(guide=guide_legend(order=1))  # work around #229
        + coord_flip())
    assert p == "annotation_stripes_coord_flip"
コード例 #9
0
def test_as_type():
    actual = dp(mtcars).astype("-hp", str).pd
    should = mtcars.assign(
        **{x: mtcars[x].astype(str)
           for x in mtcars.columns if x != "hp"})
    assert_frame_equal(should, actual)
コード例 #10
0
ファイル: lesson2_pandas.py プロジェクト: jq88712/pykurs
list(zip((mtcars.mpg > 20), (mtcars.cyl == 6)))

mtcars2.loc['Mazda RX4', 'hp'] = 120  # "magic function" __set_item__

mtcars2.loc[:, 'am'] = 99

mtcars2.iloc[:, 8] = pd.Series(range(32))
mtcars2.drop('am', inplace=True)

mtcars2.loc[:, 'am'] = pd.Series(range(32))
mtcars.loc[:, 'am'] = 2 * pd.Series(range(32))
mtcars2.loc[:, 'am'] = list(range(32))
mtcars2.loc[:, 'am'] = pd.Series(range(32)).values

mtcars.assign(am=pd.Series(range(32)))

mtcars2['new_col'] = 0
mtcars2.head()

mtcars2.loc[:, 'new_col2'] = 3

mtcars2.assign(new_col3=mtcars2.hp * 2).head()

mtcars2 = mtcars2.drop(['new_col', 'new_col2'], axis=1)

condition = mtcars2.index.str.contains('Merc')
mtcars2.loc[~condition, :]

# bool ... element-wise
# and  ... &