ibis 测试
import ibis
import pandas as pd
from ibis import _
penguins = ibis.examples.penguins.fetch()
df = pd.DataFrame(
[["a", 1, 2], ["b", 3, 4]],
columns=["one", "two", "three"],
index=[5, 6],
)
t = ibis.memtable(df, name="t")
(penguins.filter([_.species == "Adelie", _.island.isin(["Biscoe", "Dream"])]))
(penguins.limit(11, offset=9))
(penguins.order_by([_.island, _.bill_length_mm.desc()]))
(penguins.select(_.bill_length_mm, _.sex))
(penguins.rename(bill_length="bill_length_mm", bill_depth="bill_depth_mm"))
(
penguins.mutate(ones=1, bill_length=_.bill_length_mm / 10)
.mutate(bill_length_squared=_.bill_length**2)
.select(_.ones, _.bill_length_mm, _.bill_length, _.bill_length_squared)
)
(
penguins.select(
_.island,
_.bill_depth_mm,
_.flipper_length_mm,
_.body_mass_g,
_.species,
_.bill_length_mm,
_.sex,
)
)
(
penguins.group_by("island")
.aggregate(bill_depth_mean=_.bill_depth_mm.mean())
.order_by("island")
)
(
penguins.group_by("island")
.aggregate(
count=_.count(),
bill_depth_mean=_.bill_depth_mm.mean(),
flipper_length_median=_.flipper_length_mm.median(),
body_mass_sd=_.body_mass_g.std(),
share_female=(_.sex == "female").mean(),
)
.order_by("island")
)
person = ibis.memtable(person2)
#delta 计算间隔 timestamps
person=(
person.mutate(
vacc_age=(_.vaccination_date.delta(_.birth_date,'days')/365.25).floor().cast('int64')
)
.mutate(
age_group=ibis.case()
.when(_.vacc_age < 1, '幼年')
.when((_.vacc_age >= 1) & (_.vacc_age < 18), '青少年')
.else_('成年')
.end()
)
)
(
person.group_by("age_group")
.aggregate(
count=_.count(),
max_age=_.vacc_age.max(),
min_age=_.vacc_age.min()
)
)
评论
发表评论