Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix encoding type inference for boolean columns when pyarrow is installed #3210

Merged
merged 2 commits into from
Sep 30, 2023
Merged
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
6 changes: 4 additions & 2 deletions altair/utils/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,10 @@ def parse_shorthand(
column = dfi.get_column_by_name(unescaped_field)
try:
attrs["type"] = infer_vegalite_type_for_dfi_column(column)
except NotImplementedError:
# Fall back to pandas-based inference
except (NotImplementedError, AttributeError):
# Fall back to pandas-based inference.
# Note: The AttributeError catch is a workaround for
# https://github.com/pandas-dev/pandas/issues/55332
if isinstance(data, pd.DataFrame):
attrs["type"] = infer_vegalite_type(data[unescaped_field])
else:
Expand Down
8 changes: 8 additions & 0 deletions tests/utils/test_core.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import types
from packaging.version import Version
from importlib.metadata import version as importlib_version

import numpy as np
import pandas as pd
Expand All @@ -16,6 +18,8 @@
except ImportError:
pa = None

PANDAS_VERSION = Version(importlib_version("pandas"))


FAKE_CHANNELS_MODULE = f'''
"""Fake channels module for utility tests."""
Expand Down Expand Up @@ -160,6 +164,10 @@ def check(s, data, **kwargs):
check("month(z)", data, timeUnit="month", field="z", type="temporal")
check("month(t)", data, timeUnit="month", field="t", type="temporal")

if PANDAS_VERSION >= Version("1.0.0"):
data["b"] = pd.Series([True, False, True, False, None], dtype="boolean")
check("b", data, field="b", type="nominal")


@pytest.mark.skipif(pa is None, reason="pyarrow not installed")
def test_parse_shorthand_for_arrow_timestamp():
Expand Down