def test_limit_df(self, duckdb_cursor): conn = duckdb.connect() conn.execute("create table t (a integer)") conn.execute("insert into t values (1),(4)") test_df = pd.DataFrame.from_dict({"i":[1, 2, 3, 4]}) rel = duckdb.limit(test_df,1, connection=conn) assert rel.query('t_2','select * from t inner join t_2 on (a = i)').fetchall()[0] == (1,1)
# multi-relation operators are also supported, e.g union print(rel.union(rel)) # join rel with itself on i rel2 = conn.from_df(test_df) print(rel.join(rel2, 'i')) # for explicit join conditions the relations can be named using alias() print(rel.set_alias('a').join(rel.set_alias('b'), 'a.i=b.i')) # there are also shorthand methods to directly create a relation and apply an operator from pandas data frame objects print(duckdb.filter(test_df, 'i > 1')) print(duckdb.project(test_df, 'i +1')) print(duckdb.order(test_df, 'j')) print(duckdb.limit(test_df, 2)) print(duckdb.aggregate(test_df, "sum(i)")) print(duckdb.distinct(test_df)) # when chaining only the first call needs to include the data frame parameter print(duckdb.filter(test_df, 'i > 1').project('i + 1').order('j').limit(2)) # turn the relation into something else again # compute the query result from the relation res = rel.execute() print(res) # res is a query result, you can call fetchdf() or fetchnumpy() or fetchone() on it print(res.fetchone()) print(res.fetchall())
def test_limit_df(self, duckdb_cursor): df_in = pd.DataFrame({ 'numbers': [1, 2, 3, 4, 5], }) limit_df = duckdb.limit(df_in, 2) assert len(limit_df.execute().fetchall()) == 2