Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions src/numpy/numpy_scan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1325,12 +1325,11 @@ void NumpyScan::Scan(ClientContext &context, PandasColumnBindData &bind_data, id

switch (bind_data.numpy_type.type) {
case NumpyNullableType::DATETIME_NS:
if (bind_data.numpy_type.has_timezone) {
// Our timezone type is US, so we need to convert from NS to US
convert_func = Timestamp::FromEpochNanoSeconds;
} else {
convert_func = Timestamp::FromEpochMicroSeconds;
}
case NumpyNullableType::DATETIME_US:
// The bound timestamp type stores the same unit as the input.
convert_func = [](int64_t value) {
return timestamp_t(value);
};
break;
case NumpyNullableType::DATETIME_MS:
if (bind_data.numpy_type.has_timezone) {
Expand All @@ -1340,9 +1339,6 @@ void NumpyScan::Scan(ClientContext &context, PandasColumnBindData &bind_data, id
convert_func = Timestamp::FromEpochMicroSeconds;
}
break;
case NumpyNullableType::DATETIME_US:
convert_func = Timestamp::FromEpochMicroSeconds;
break;
case NumpyNullableType::DATETIME_S:
if (bind_data.numpy_type.has_timezone) {
// Our timezone type is US, so we need to convert from S to US
Expand Down
2 changes: 1 addition & 1 deletion src/numpy/type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ LogicalType NumpyToLogicalType(const NumpyType &col_type) {
}
case NumpyNullableType::DATETIME_NS: {
if (col_type.has_timezone) {
return LogicalType::TIMESTAMP_TZ;
return LogicalType::TIMESTAMP_TZ_NS;
}
return LogicalType::TIMESTAMP_NS;
}
Expand Down
2 changes: 1 addition & 1 deletion src/pyresult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ void DuckDBPyResult::ConvertDateTimeTypes(PandasDataFrame &df, bool date_as_obje

auto &types = GetTypes();
for (idx_t i = 0; i < types.size(); i++) {
if (types[i] == LogicalType::TIMESTAMP_TZ) {
if (types[i] == LogicalType::TIMESTAMP_TZ || types[i] == LogicalType::TIMESTAMP_TZ_NS) {
// first localize to UTC then convert to timezone_config
auto utc_local = df[names[i].c_str()].attr("dt").attr("tz_localize")("UTC");
auto new_value = utc_local.attr("dt").attr("tz_convert")(GetClientProperties().time_zone);
Expand Down
2 changes: 1 addition & 1 deletion tests/fast/pandas/test_pandas_scan_bind_reads.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class ColumnCase(NamedTuple):
),
"datetime_tz": ColumnCase(
lambda: pd.Series(_TZ_TIMES),
"TIMESTAMP WITH TIME ZONE",
"TIMESTAMPTZ_NS" if _TZ_TIMES.dtype.unit == "ns" else "TIMESTAMP WITH TIME ZONE",
[timestamp.to_pydatetime() for timestamp in _TZ_TIMES],
),
"datetime_naive": ColumnCase(
Expand Down
14 changes: 14 additions & 0 deletions tests/fast/pandas/test_pandas_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ def test_run_pandas_with_tz(timezone):
assert duck_df["timestamp"][0] == df["timestamp"][0]


@pytest.mark.parametrize("timezone", ["UTC", "Asia/Kathmandu"])
def test_timezone_aware_nanosecond_timestamps(duckdb_cursor, timezone):
timestamps = pandas.to_datetime([-1, 100, 900, None], unit="ns", utc=True).tz_convert(timezone)
frame = pandas.DataFrame({"ts": timestamps})
duckdb_cursor.execute(f"SET TimeZone = '{timezone}'")
duckdb_cursor.register("timestamps", frame)

rows = duckdb_cursor.execute("SELECT typeof(ts), epoch_ns(ts) FROM timestamps ORDER BY ts NULLS LAST").fetchall()

assert rows == [("TIMESTAMPTZ_NS", -1), ("TIMESTAMPTZ_NS", 100), ("TIMESTAMPTZ_NS", 900), ("TIMESTAMPTZ_NS", None)]
result = duckdb_cursor.sql("SELECT ts FROM timestamps ORDER BY ts NULLS LAST").df()
pandas.testing.assert_frame_equal(result, frame)


def test_timestamp_conversion(duckdb_cursor):
tzinfo = pandas.Timestamp("2024-01-01 00:00:00+0100", tz="Europe/Copenhagen").tzinfo
ts_df = pandas.DataFrame( # noqa: F841
Expand Down
5 changes: 2 additions & 3 deletions tests/fast/pandas/test_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def test_timestamp_types_roundtrip(self, unit):
def test_timestamp_timezone_roundtrip(self, unit):
if pandas_2_or_higher():
dtype = pd.core.dtypes.dtypes.DatetimeTZDtype(unit=unit, tz="UTC")
expected_dtype = pd.core.dtypes.dtypes.DatetimeTZDtype(unit="us", tz="UTC")
expected_unit = "ns" if unit == "ns" else "us"
expected_dtype = pd.core.dtypes.dtypes.DatetimeTZDtype(unit=expected_unit, tz="UTC")
else:
# Older versions of pandas only support 'ns' as timezone unit
expected_dtype = pd.core.dtypes.dtypes.DatetimeTZDtype(unit="ns", tz="UTC")
Expand All @@ -40,8 +41,6 @@ def test_timestamp_timezone_roundtrip(self, unit):
}
df = pd.DataFrame(data=d)

# Our timezone aware type is in US (microseconds), when we scan a timestamp column that isn't US and has
# timezone info, we convert the time unit to US
expected = pd.DataFrame(data=d, dtype=expected_dtype)
df_from_duck = conn.from_df(df).df()
assert df_from_duck.equals(expected)
Expand Down