Skip to content

Commit

Permalink
Jonmmease/infer dtype pandas fallback (#3179)
Browse files Browse the repository at this point in the history
* Add failing parse_shorthand tests with object dtypes

* Fall back to pandas-based type inference when NotImplementedError is raised
  • Loading branch information
jonmmease authored Aug 30, 2023
1 parent 17e8d07 commit d8a01d2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
10 changes: 9 additions & 1 deletion altair/utils/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,15 @@ def parse_shorthand(
unescaped_field = attrs["field"].replace("\\", "")
if unescaped_field in dfi.column_names():
column = dfi.get_column_by_name(unescaped_field)
attrs["type"] = infer_vegalite_type_for_dfi_column(column)
try:
attrs["type"] = infer_vegalite_type_for_dfi_column(column)
except NotImplementedError:
# Fall back to pandas-based inference
if isinstance(data, pd.DataFrame):
attrs["type"] = infer_vegalite_type(data[unescaped_field])
else:
raise

if isinstance(attrs["type"], tuple):
attrs["sort"] = attrs["type"][1]
attrs["type"] = attrs["type"][0]
Expand Down
6 changes: 5 additions & 1 deletion tests/utils/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ def check(s, **kwargs):
)


def test_parse_shorthand_with_data():
@pytest.mark.parametrize("object_dtype", [False, True])
def test_parse_shorthand_with_data(object_dtype):
def check(s, data, **kwargs):
assert parse_shorthand(s, data) == kwargs

Expand All @@ -147,6 +148,9 @@ def check(s, data, **kwargs):
}
)

if object_dtype:
data = data.astype("object")

check("x", data, field="x", type="quantitative")
check("y", data, field="y", type="nominal")
check("z", data, field="z", type="temporal")
Expand Down

0 comments on commit d8a01d2

Please sign in to comment.