Skip to content

Commit

Permalink
perf: use Narwhals expressions (and stable API) (#528)
Browse files Browse the repository at this point in the history
* use Narwhals stable api, use expressions

* just use .dt.convert_time_zone
  • Loading branch information
MarcoGorelli authored Nov 5, 2024
1 parent c249028 commit aced322
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions vegafusion-python/vegafusion/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from types import ModuleType
from typing import TYPE_CHECKING, Any, Literal, TypedDict, Union, cast

import narwhals as nw
import narwhals.stable.v1 as nw
from arro3.core import Table

from vegafusion._vegafusion import get_cpu_count, get_virtual_memory
Expand Down Expand Up @@ -566,18 +566,15 @@ def pre_transform_datasets(
"transformed data"
)

# Localize datetime columns to UTC, then extract the native DataFrame
# to return
# Convert to `local_tz` (or, set to UTC and then convert if starting
# from time-zone-naive data), then extract the native DataFrame to return.
processed_datasets = []
for df in nw_dataframes:
for name in df.columns:
dtype = df[name].dtype
if dtype == nw.Datetime:
df = df.with_columns(
df[name]
.dt.replace_time_zone("UTC")
.dt.convert_time_zone(local_tz)
)
df = df.with_columns(
nw.col(col).dt.convert_time_zone(local_tz)
for col, dtype in df.schema.items()
if dtype == nw.Datetime
)
processed_datasets.append(df.to_native())

return processed_datasets, warnings
Expand Down

0 comments on commit aced322

Please sign in to comment.