1

我将 ibis 与 bigquery 后端一起使用。

我想使用 .add() 方法为日期添加时间间隔。

但是我不知道如何指定这样的时间间隔:下面代码中的“which_type_here”变量。

谢谢你的帮助!

import ibis
import ibis_bigquery
import pandas as pd
import datetime as dt

my_project = "your_poject"
my_dataset = 'your_dataset'

conn = ibis_bigquery.connect(
    project_id=my_project,
    dataset_id=my_dataset)

pdf = pd.DataFrame({'date': [dt.date(2020, 1, 1),
                             dt.date(2020, 5, 19),
                             dt.date(2021, 7, 9)],
                     })
client = bigquery.Client(project=my_project)
table = my_dataset + ".mytable"
client.delete_table(table, not_found_ok=True)
job = client.load_table_from_dataframe(pdf, table)

t = conn.table("mytable")

which_type_here = ????
e = t.mutate(new_date=t.date.add(which_type_here))
4

1 回答 1

0

from的interval类型在ibis这里似乎是一个很好的候选者。

import ibis
import ibis_bigquery
import pandas as pd
import datetime as dt

my_project = "your_poject"
my_dataset = 'your_dataset'

conn = ibis_bigquery.connect(
    project_id=my_project,
    dataset_id=my_dataset)

pdf = pd.DataFrame({'date': [dt.date(2020, 1, 1),
                             dt.date(2020, 5, 19),
                             dt.date(2021, 7, 9)],
                     })
client = bigquery.Client(project=my_project)
table = my_dataset + ".mytable"
client.delete_table(table, not_found_ok=True)
job = client.load_table_from_dataframe(pdf, table)

t = conn.table("mytable")

two_days = ibis.interval(days=2)
e = t.mutate(new_date=t.date.add(two_days))
result = e.execute()

结果:

        date   new_date
0 2020-01-01 2020-01-03
1 2020-05-19 2020-05-21
2 2021-07-09 2021-07-11
于 2021-08-23T09:02:20.563 回答