From c119d1e7e6b55b23d3166cadd2b54b0c1697c341 Mon Sep 17 00:00:00 2001 From: mattijn Date: Sun, 24 Mar 2024 16:12:06 +0100 Subject: [PATCH] check for instance DataFrameLike instead of __dataframe__ attribute --- altair/utils/_vegafusion_data.py | 2 +- altair/utils/core.py | 2 +- altair/utils/data.py | 4 ++-- altair/vegalite/v5/api.py | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/altair/utils/_vegafusion_data.py b/altair/utils/_vegafusion_data.py index 8b46bab78..ce30e8d6d 100644 --- a/altair/utils/_vegafusion_data.py +++ b/altair/utils/_vegafusion_data.py @@ -45,7 +45,7 @@ def vegafusion_data_transformer( # Use default transformer for geo interface objects # # (e.g. a geopandas GeoDataFrame) return default_data_transformer(data) - elif hasattr(data, "__dataframe__"): + elif isinstance(data, DataFrameLike): table_name = f"table_{uuid.uuid4()}".replace("-", "_") extracted_inline_tables[table_name] = data return {"url": VEGAFUSION_PREFIX + table_name} diff --git a/altair/utils/core.py b/altair/utils/core.py index ea275d589..baf1013f7 100644 --- a/altair/utils/core.py +++ b/altair/utils/core.py @@ -589,7 +589,7 @@ def parse_shorthand( # if data is specified and type is not, infer type from data if "type" not in attrs: - if pyarrow_available() and data is not None and hasattr(data, "__dataframe__"): + if pyarrow_available() and data is not None and isinstance(data, DataFrameLike): dfi = data.__dataframe__() if "field" in attrs: unescaped_field = attrs["field"].replace("\\", "") diff --git a/altair/utils/data.py b/altair/utils/data.py index e4b135d38..871b43092 100644 --- a/altair/utils/data.py +++ b/altair/utils/data.py @@ -238,8 +238,8 @@ def to_values(data: DataType) -> ToValuesReturnType: def check_data_type(data: DataType) -> None: - if not isinstance(data, (dict, pd.DataFrame)) and not any( - hasattr(data, attr) for attr in ["__geo_interface__", "__dataframe__"] + if not isinstance(data, (dict, pd.DataFrame, DataFrameLike)) and not any( + hasattr(data, attr) for attr in ["__geo_interface__"] ): raise TypeError( "Expected dict, DataFrame or a __geo_interface__ attribute, got: {}".format( diff --git a/altair/vegalite/v5/api.py b/altair/vegalite/v5/api.py index 363a86e84..dfde5ee7e 100644 --- a/altair/vegalite/v5/api.py +++ b/altair/vegalite/v5/api.py @@ -114,7 +114,7 @@ def _prepare_data(data, context=None): elif isinstance(data, str): data = core.UrlData(data) - elif hasattr(data, "__dataframe__"): + elif isinstance(data, DataFrameLike): data = _pipe(data, data_transformers.get()) # consolidate inline data to top-level datasets