Skip to content

Commit e3a995e

Browse files
committed
use 'insert' to ensure the replaced column ends up in the right position
1 parent 135f09c commit e3a995e

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

src/duckdb_py/pyresult.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ void DuckDBPyResult::ChangeToTZType(PandasDataFrame &df) {
298298
auto new_value = utc_local.attr("dt").attr("tz_convert")(result->client_properties.time_zone);
299299
// We need to create the column anew because the exact dt changed to a new timezone
300300
df.attr("drop")("columns"_a = names[i].c_str(), "inplace"_a = true);
301-
df.attr("__setitem__")(names[i].c_str(), new_value);
301+
df.attr("insert")(i, names[i].c_str(), new_value);
302302
}
303303
}
304304
}
@@ -384,7 +384,7 @@ PandasDataFrame DuckDBPyResult::FrameFromNumpy(bool date_as_object, const py::ha
384384
if (result->types[i] == LogicalType::DATE) {
385385
auto new_value = df[names[i].c_str()].attr("dt").attr("date");
386386
df.attr("drop")("columns"_a = names[i].c_str(), "inplace"_a = true);
387-
df.attr("__setitem__")(names[i].c_str(), new_value);
387+
df.attr("insert")(i, names[i].c_str(), new_value);
388388
}
389389
}
390390
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytest
2+
3+
import pandas as pd
4+
import duckdb
5+
6+
class TestColumnOrder:
7+
def test_column_order(self, duckdb_cursor):
8+
to_execute = """
9+
CREATE OR REPLACE TABLE t1 AS (
10+
SELECT NULL AS col1,
11+
NULL::TIMESTAMPTZ AS timepoint,
12+
);
13+
SELECT timepoint, col1 FROM t1;
14+
"""
15+
df = duckdb.execute(to_execute).fetchdf()
16+
cols = list(df.columns)
17+
assert cols == ['timepoint', 'col1']

0 commit comments

Comments
 (0)