1

I am using ibis with the bigquery backend.

I want to add a time interval to a date, using the .add() method.

However I can't figured out how to specify such a time interval: the "which_type_here" variable in the code below.

Thanks for your help!

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))
Progman
  • 16,827
  • 6
  • 33
  • 48
jcmincke
  • 41
  • 2

1 Answers1

0

The interval type from ibis seems to be a good candidate 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")

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

result:

        date   new_date
0 2020-01-01 2020-01-03
1 2020-05-19 2020-05-21
2 2021-07-09 2021-07-11