Skip to content

Commit

Permalink
Fix encoding type inference for boolean columns when pyarrow is insta…
Browse files Browse the repository at this point in the history
…lled (#3210)

* Work around pandas-dev/pandas#55332

* Only test boolean column with pandas >= 1.0.0
  • Loading branch information
jonmmease authored Sep 30, 2023
1 parent 19bd5a5 commit da0de55
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
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

0 comments on commit da0de55

Please sign in to comment.