Skip to content

Commit da0de55

Browse files
authored
Fix encoding type inference for boolean columns when pyarrow is installed (#3210)
* Work around pandas-dev/pandas#55332 * Only test boolean column with pandas >= 1.0.0
1 parent 19bd5a5 commit da0de55

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

altair/utils/core.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,8 +588,10 @@ def parse_shorthand(
588588
column = dfi.get_column_by_name(unescaped_field)
589589
try:
590590
attrs["type"] = infer_vegalite_type_for_dfi_column(column)
591-
except NotImplementedError:
592-
# Fall back to pandas-based inference
591+
except (NotImplementedError, AttributeError):
592+
# Fall back to pandas-based inference.
593+
# Note: The AttributeError catch is a workaround for
594+
# https://github.com/pandas-dev/pandas/issues/55332
593595
if isinstance(data, pd.DataFrame):
594596
attrs["type"] = infer_vegalite_type(data[unescaped_field])
595597
else:

tests/utils/test_core.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import types
2+
from packaging.version import Version
3+
from importlib.metadata import version as importlib_version
24

35
import numpy as np
46
import pandas as pd
@@ -16,6 +18,8 @@
1618
except ImportError:
1719
pa = None
1820

21+
PANDAS_VERSION = Version(importlib_version("pandas"))
22+
1923

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

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

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

0 commit comments

Comments
 (0)