Skip to content

Commit 355f879

Browse files
committed
fix
1 parent 875569c commit 355f879

File tree

2 files changed

+12
-20
lines changed

2 files changed

+12
-20
lines changed

marimo/_plugins/ui/_impl/altair_chart.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
)
1616

1717
import narwhals.stable.v2 as nw
18-
from narwhals.dependencies import is_narwhals_lazyframe
1918
from narwhals.typing import IntoDataFrame, IntoLazyFrame
2019

2120
from marimo import _loggers
@@ -55,7 +54,9 @@
5554
RowOrientedData = list[dict[str, Any]]
5655
ColumnOrientedData = dict[str, list[Any]]
5756

58-
ChartDataType = Union[IntoDataFrame, RowOrientedData, ColumnOrientedData]
57+
ChartDataType = Union[
58+
IntoDataFrame, IntoLazyFrame, RowOrientedData, ColumnOrientedData
59+
]
5960

6061
# Union of all possible chart types
6162
AltairChartType: TypeAlias = "altair.vegalite.v5.api.ChartType"
@@ -98,12 +99,8 @@ def _using_vegafusion() -> bool:
9899

99100
def _filter_dataframe(
100101
native_df: Union[IntoDataFrame, IntoLazyFrame], selection: ChartSelection
101-
) -> IntoDataFrame:
102-
df = nw.from_native(native_df).lazy()
103-
if is_narwhals_lazyframe(df):
104-
raise ValueError(
105-
"Lazyframes are not supported for filtering. Run `df.collect()` before filtering."
106-
)
102+
) -> Union[IntoDataFrame, IntoLazyFrame]:
103+
df = nw.from_native(native_df)
107104
if not isinstance(selection, dict):
108105
raise TypeError("Input 'selection' must be a dictionary")
109106

@@ -187,14 +184,9 @@ def _coerce_value(value: Any, dtype: Any) -> Any:
187184
if nw.Datetime == dtype and isinstance(dtype, nw.Datetime):
188185
if isinstance(value, str):
189186
res = datetime.datetime.fromisoformat(value)
190-
# If dtype has no timezone, shift by local timezone offset
191-
if dtype.time_zone is None:
192-
local_tz = datetime.datetime.now().astimezone().tzinfo
193-
LOGGER.warning(
194-
f"Datetime was given with a timezone when not expected. "
195-
f"Shifting by local timezone offset {local_tz}."
196-
)
197-
return res.astimezone(local_tz).replace(tzinfo=None)
187+
# If dtype has no timezone, but value has timezone, remove timezone without shifting
188+
if dtype.time_zone is None and res.tzinfo is not None:
189+
return res.replace(tzinfo=None)
198190
return res
199191

200192
# Value is milliseconds since epoch

tests/_plugins/ui/_impl/test_altair_chart.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -665,19 +665,19 @@ def test_parse_spec_pandas() -> None:
665665
chart = alt.Chart(data).mark_point().encode(x="values:Q")
666666
spec = _parse_spec(chart)
667667
# Replace data.url with a placeholder
668-
spec["data"]["url"] = "_placeholder_"
668+
spec["data"] = {"url": "_placeholder_", "format": spec["data"]["format"]}
669669
snapshot("parse_spec_pandas.txt", json.dumps(spec, indent=2))
670670

671671

672672
@pytest.mark.skipif(not HAS_DEPS, reason="optional dependencies not installed")
673673
def test_parse_spec_narwhal() -> None:
674674
import altair as alt
675675

676-
data = nw.from_native(pd.DataFrame({"values": [1, 2, 3]}))
676+
data = pd.DataFrame({"values": [1, 2, 3]})
677677
chart = alt.Chart(data).mark_point().encode(x="values:Q")
678678
spec = _parse_spec(chart)
679679
# Replace data.url with a placeholder
680-
spec["data"]["url"] = "_placeholder_"
680+
spec["data"] = {"url": "_placeholder_", "format": spec["data"]["format"]}
681681
snapshot("parse_spec_narwhal.txt", json.dumps(spec, indent=2))
682682

683683

@@ -690,7 +690,7 @@ def test_parse_spec_polars() -> None:
690690
chart = alt.Chart(data).mark_point().encode(x="values:Q")
691691
spec = _parse_spec(chart)
692692
# Replace data.url with a placeholder
693-
spec["data"]["url"] = "_placeholder_"
693+
spec["data"] = {"url": "_placeholder_", "format": spec["data"]["format"]}
694694
snapshot("parse_spec_polars.txt", json.dumps(spec, indent=2))
695695

696696

0 commit comments

Comments
 (0)