Skip to content

Commit

Permalink
chore: bump narwhals to v1.14.2 (#3697)
Browse files Browse the repository at this point in the history
Co-authored-by: dangotbanned <125183946+dangotbanned@users.noreply.github.com>
  • Loading branch information
mattijn and dangotbanned authored Nov 23, 2024
1 parent 6c79fa9 commit b6481d2
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions altair/_magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from importlib.util import find_spec
from typing import Any

import narwhals.stable.v1 as nw
from IPython.core import magic_arguments
from narwhals.stable.v1.dependencies import is_pandas_dataframe

from altair.vegalite import v5 as vegalite_v5

Expand All @@ -32,7 +32,7 @@ def _prepare_data(data, data_transformers):
"""Convert input data to data for use within schema."""
if data is None or isinstance(data, dict):
return data
elif nw.dependencies.is_pandas_dataframe(data):
elif is_pandas_dataframe(data):
if func := data_transformers.get():
data = func(data)
return data
Expand Down
6 changes: 2 additions & 4 deletions altair/utils/_vegafusion_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import TYPE_CHECKING, Any, Callable, Final, TypedDict, Union, overload
from weakref import WeakValueDictionary

import narwhals.stable.v1 as nw
from narwhals.stable.v1.dependencies import is_into_dataframe
from packaging.version import Version

from altair.utils._importers import import_vegafusion
Expand Down Expand Up @@ -54,9 +54,7 @@
def is_supported_by_vf(data: Any) -> TypeIs[DataFrameLike]:
# Test whether VegaFusion supports the data type
# VegaFusion v2 support narwhals-compatible DataFrames
return isinstance(data, DataFrameLike) or nw.dependencies.is_into_dataframe(
data
)
return isinstance(data, DataFrameLike) or is_into_dataframe(data)

else:

Expand Down
7 changes: 4 additions & 3 deletions altair/utils/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import jsonschema
import narwhals.stable.v1 as nw
from narwhals.stable.v1.dependencies import is_pandas_dataframe, is_polars_dataframe
from narwhals.stable.v1.typing import IntoDataFrame

from altair.utils.schemapi import SchemaBase, SchemaLike, Undefined
Expand Down Expand Up @@ -469,9 +470,9 @@ def sanitize_narwhals_dataframe(
columns: list[IntoExpr] = []
# See https://github.com/vega/altair/issues/1027 for why this is necessary.
local_iso_fmt_string = "%Y-%m-%dT%H:%M:%S"
is_polars_dataframe = nw.dependencies.is_polars_dataframe(data.to_native())
is_polars = is_polars_dataframe(data.to_native())
for name, dtype in schema.items():
if dtype == nw.Date and is_polars_dataframe:
if dtype == nw.Date and is_polars:
# Polars doesn't allow formatting `Date` with time directives.
# The date -> datetime cast is extremely fast compared with `to_string`
columns.append(
Expand Down Expand Up @@ -673,7 +674,7 @@ def parse_shorthand( # noqa: C901
if schema[unescaped_field] in {
nw.Object,
nw.Unknown,
} and nw.dependencies.is_pandas_dataframe(data_nw.to_native()):
} and is_pandas_dataframe(data_nw.to_native()):
attrs["type"] = infer_vegalite_type_for_pandas(column.to_native())
else:
attrs["type"] = infer_vegalite_type_for_narwhals(column)
Expand Down
11 changes: 6 additions & 5 deletions altair/utils/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
)

import narwhals.stable.v1 as nw
from narwhals.stable.v1.dependencies import is_pandas_dataframe
from narwhals.stable.v1.typing import IntoDataFrame

from ._importers import import_pyarrow_interchange
Expand Down Expand Up @@ -187,7 +188,7 @@ def sample(
if data is None:
return partial(sample, n=n, frac=frac)
check_data_type(data)
if nw.dependencies.is_pandas_dataframe(data):
if is_pandas_dataframe(data):
return data.sample(n=n, frac=frac)
elif isinstance(data, dict):
if "values" in data:
Expand Down Expand Up @@ -322,7 +323,7 @@ def to_values(data: DataType) -> ToValuesReturnType:
data_native = nw.to_native(data, pass_through=True)
if isinstance(data_native, SupportsGeoInterface):
return {"values": _from_geo_interface(data_native)}
elif nw.dependencies.is_pandas_dataframe(data_native):
elif is_pandas_dataframe(data_native):
data_native = sanitize_pandas_dataframe(data_native)
return {"values": data_native.to_dict(orient="records")}
elif isinstance(data_native, dict):
Expand Down Expand Up @@ -363,7 +364,7 @@ def _from_geo_interface(data: SupportsGeoInterface | Any) -> dict[str, Any]:
- ``typing.TypeGuard``
- ``pd.DataFrame.__getattr__``
"""
if nw.dependencies.is_pandas_dataframe(data):
if is_pandas_dataframe(data):
data = sanitize_pandas_dataframe(data)
return sanitize_geo_interface(data.__geo_interface__)

Expand All @@ -373,7 +374,7 @@ def _data_to_json_string(data: DataType) -> str:
check_data_type(data)
if isinstance(data, SupportsGeoInterface):
return json.dumps(_from_geo_interface(data))
elif nw.dependencies.is_pandas_dataframe(data):
elif is_pandas_dataframe(data):
data = sanitize_pandas_dataframe(data)
return data.to_json(orient="records", double_precision=15)
elif isinstance(data, dict):
Expand All @@ -400,7 +401,7 @@ def _data_to_csv_string(data: DataType) -> str:
f"See https://github.com/vega/altair/issues/3441"
)
raise NotImplementedError(msg)
elif nw.dependencies.is_pandas_dataframe(data):
elif is_pandas_dataframe(data):
data = sanitize_pandas_dataframe(data)
return data.to_csv(index=False)
elif isinstance(data, dict):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dependencies = [
# If you update the minimum required jsonschema version, also update it in build.yml
"jsonschema>=3.0",
"packaging",
"narwhals>=1.13.1"
"narwhals>=1.14.2"
]
description = "Vega-Altair: A declarative statistical visualization library for Python."
readme = "README.md"
Expand Down

0 comments on commit b6481d2

Please sign in to comment.