Skip to content

Commit

Permalink
Fixing NumPy/Pandas String fetching and add test case based on #448
Browse files Browse the repository at this point in the history
  • Loading branch information
hannes committed Mar 7, 2020
1 parent f126192 commit bc8a11d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tools/pythonpkg/cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ PyObject *duckdb_cursor_fetchnumpy(duckdb_Cursor *self) {
assert(!chunk->data[col_idx].sel_vector);
PyObject *str_obj;
if (!mask_data[chunk_idx + offset]) {
str_obj = PyUnicode_FromString(((const char **)chunk->data[col_idx].GetData())[chunk_idx]);
str_obj = PyUnicode_FromString(((duckdb::string_t*)chunk->data[col_idx].GetData())[chunk_idx].GetData());
} else {
assert(cols[col_idx].found_nil);
str_obj = Py_None;
Expand Down
17 changes: 17 additions & 0 deletions tools/pythonpkg/tests/test_dbapi08.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# test fetchdf with various types
import pandas
import numpy

class TestType(object):
def test_fetchdf(self, duckdb_cursor):
duckdb_cursor.execute("CREATE TABLE items(item VARCHAR)")
duckdb_cursor.execute("INSERT INTO items VALUES ('jeans'), (''), (NULL)")
res = duckdb_cursor.execute("SELECT item FROM items").fetchdf()
assert isinstance(res, pandas.DataFrame)

arr = numpy.ma.masked_array(['jeans', '', None])
arr.mask = [False, False, True]
arr = {'item': arr}
df = pandas.DataFrame.from_dict(arr)

pandas.testing.assert_frame_equal(res, df)

0 comments on commit bc8a11d

Please sign in to comment.