Пример #1
0
 def test_from_df(self, duckdb_cursor):
     conn = duckdb.connect()
     conn.execute("create table t (a integer)")
     conn.execute("insert into t values (1)")
     test_df = pd.DataFrame.from_dict({"i":[1, 2, 3, 4]})
     rel = duckdb.df(test_df, connection=conn)
     assert rel.query('t_2','select count(*) from t inner join t_2 on (a = i)').fetchall()[0] ==  (1,)
     rel = duckdb.from_df(test_df, connection=conn)
     assert rel.query('t_2','select count(*) from t inner join t_2 on (a = i)').fetchall()[0] ==  (1,)
Пример #2
0
    "i": [1, 2, 3, 4],
    "j": ["one", "two", "three", "four"]
})

# make this data frame available as a view in duckdb
conn.register("test_df", test_df)
print(conn.execute("SELECT j FROM test_df WHERE i > 1").fetchdf())

# relation API, programmatic querying. relations are lazily evaluated chains of relational operators

# create a "relation" from a pandas data frame with an existing connection
rel = conn.from_df(test_df)
print(rel)

# alternative shorthand, use a built-in default connection to create a relation from a pandas data frame
rel = duckdb.df(test_df)
print(rel)

# create a relation from a CSV file

# first create a CSV file from our pandas example
import tempfile, os
temp_file_name = os.path.join(tempfile.mkdtemp(),
                              next(tempfile._get_candidate_names()))
test_df.to_csv(temp_file_name, index=False)

# now create a relation from it
rel = duckdb.from_csv_auto(temp_file_name)
print(rel)

# create a relation from an existing table