diff --git a/src/duckdb_py/pyresult.cpp b/src/duckdb_py/pyresult.cpp index 43edf0e1..d25e0102 100644 --- a/src/duckdb_py/pyresult.cpp +++ b/src/duckdb_py/pyresult.cpp @@ -298,7 +298,7 @@ void DuckDBPyResult::ChangeToTZType(PandasDataFrame &df) { auto new_value = utc_local.attr("dt").attr("tz_convert")(result->client_properties.time_zone); // We need to create the column anew because the exact dt changed to a new timezone df.attr("drop")("columns"_a = names[i].c_str(), "inplace"_a = true); - df.attr("__setitem__")(names[i].c_str(), new_value); + df.attr("insert")(i, names[i].c_str(), new_value); } } } @@ -384,7 +384,7 @@ PandasDataFrame DuckDBPyResult::FrameFromNumpy(bool date_as_object, const py::ha if (result->types[i] == LogicalType::DATE) { auto new_value = df[names[i].c_str()].attr("dt").attr("date"); df.attr("drop")("columns"_a = names[i].c_str(), "inplace"_a = true); - df.attr("__setitem__")(names[i].c_str(), new_value); + df.attr("insert")(i, names[i].c_str(), new_value); } } } diff --git a/tests/fast/pandas/test_column_order.py b/tests/fast/pandas/test_column_order.py new file mode 100644 index 00000000..7e8b868a --- /dev/null +++ b/tests/fast/pandas/test_column_order.py @@ -0,0 +1,17 @@ +import pytest + +import pandas as pd +import duckdb + +class TestColumnOrder: + def test_column_order(self, duckdb_cursor): + to_execute = """ + CREATE OR REPLACE TABLE t1 AS ( + SELECT NULL AS col1, + NULL::TIMESTAMPTZ AS timepoint, + ); + SELECT timepoint, col1 FROM t1; + """ + df = duckdb.execute(to_execute).fetchdf() + cols = list(df.columns) + assert cols == ['timepoint', 'col1']