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

infer dtype pandas fallback #3179

Merged
merged 2 commits into from
Aug 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
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