def test_write_csv_operator(self,duckdb_cursor): conn = duckdb.connect() df_rel = get_relation(conn) temp_file_name = os.path.join(tempfile.mkdtemp(), next(tempfile._get_candidate_names())) df_rel.write_csv(temp_file_name) csv_rel = duckdb.from_csv_auto(temp_file_name) assert df_rel.execute().fetchall() == csv_rel.execute().fetchall()
def test_from_csv(self, duckdb_cursor): temp_file_name = os.path.join(tempfile.mkdtemp(), next(tempfile._get_candidate_names())) 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]}) test_df.to_csv(temp_file_name, index=False) rel = duckdb.from_csv_auto(temp_file_name, conn) assert rel.query('t_2','select count(*) from t inner join t_2 on (a = i)').fetchall()[0] == (1,)
def test_csv_auto(self, duckdb_cursor): conn = duckdb.connect() df_rel = get_relation(conn) temp_file_name = os.path.join(tempfile.mkdtemp(), next(tempfile._get_candidate_names())) test_df = pd.DataFrame.from_dict({"i":[1, 2, 3, 4], "j":["one", "two", "three", "four"]}) test_df.to_csv(temp_file_name, index=False) # now create a relation from it csv_rel = duckdb.from_csv_auto(temp_file_name) assert df_rel.execute().fetchall() == csv_rel.execute().fetchall()
def test_df_write_csv(self, duckdb_cursor): test_df = pd.DataFrame.from_dict({ "i": [1, 2, 3, 4], "j": ["one", "two", "three", "four"] }) temp_file_name = os.path.join(tempfile.mkdtemp(), next(tempfile._get_candidate_names())) duckdb.write_csv(test_df, temp_file_name) csv_rel = duckdb.from_csv_auto(temp_file_name) assert csv_rel.execute().fetchall() == [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
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 rel = conn.table("test_table") print(rel) # a relation has an alias (like a table name) print(rel.alias) # we can change the alias, useful for (self)joins for example rel2 = rel.set_alias('new_alias') print(rel2.alias) # we can inspect the type of a relation print(rel.type)