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: handle potential TypeError when determining variable type #3516

Merged
merged 3 commits into from
Nov 4, 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
9 changes: 7 additions & 2 deletions seaborn/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1520,8 +1520,13 @@ def variable_type(vector, boolean_type="numeric"):
warnings.simplefilter(
action='ignore', category=(FutureWarning, DeprecationWarning)
)
if np.isin(vector, [0, 1]).all():
return VariableType(boolean_type)
try:
if np.isin(vector, [0, 1]).all():
return VariableType(boolean_type)
except TypeError:
# .isin comparison is not guaranteed to be possible under NumPy
# casting rules, depending on the (unknown) dtype of 'vector'
pass

# Defer to positive pandas tests
if pd.api.types.is_numeric_dtype(vector):
Expand Down
7 changes: 6 additions & 1 deletion seaborn/_core/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@ def variable_type(
boolean_dtypes = ["bool"]
boolean_vector = vector.dtype in boolean_dtypes
else:
boolean_vector = bool(np.isin(vector, [0, 1]).all())
try:
boolean_vector = bool(np.isin(vector, [0, 1]).all())
except TypeError:
# .isin comparison is not guaranteed to be possible under NumPy
# casting rules, depending on the (unknown) dtype of 'vector'
boolean_vector = False
if boolean_vector:
return VarType(boolean_type)

Expand Down
8 changes: 8 additions & 0 deletions tests/_core/test_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ def test_variable_type():
assert variable_type(s, boolean_type="categorical") == "categorical"
assert variable_type(s, boolean_type="boolean") == "boolean"

# This should arguably be datmetime, but we don't currently handle it correctly
# Test is mainly asserting that this doesn't fail on the boolean check.
s = pd.timedelta_range(1, periods=3, freq="D").to_series()
assert variable_type(s) == "categorical"

s_cat = s.astype("category")
assert variable_type(s_cat, boolean_type="categorical") == "categorical"
assert variable_type(s_cat, boolean_type="numeric") == "categorical"
Expand All @@ -61,6 +66,9 @@ def test_variable_type():
assert variable_type(s, boolean_type="boolean") == "boolean"
assert variable_type(s, boolean_type="boolean", strict_boolean=True) == "numeric"

s = pd.Series([1, 0, 0])
assert variable_type(s, boolean_type="boolean") == "boolean"

s = pd.Series([pd.Timestamp(1), pd.Timestamp(2)])
assert variable_type(s) == "datetime"
assert variable_type(s.astype(object)) == "datetime"
Expand Down
5 changes: 5 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1508,6 +1508,11 @@ def test_variable_type(self):
assert variable_type(s.to_numpy()) == "categorical"
assert variable_type(s.to_list()) == "categorical"

# This should arguably be datmetime, but we don't currently handle it correctly
# Test is mainly asserting that this doesn't fail on the boolean check.
s = pd.timedelta_range(1, periods=3, freq="D").to_series()
assert variable_type(s) == "categorical"

s = pd.Series([True, False, False])
assert variable_type(s) == "numeric"
assert variable_type(s, boolean_type="categorical") == "categorical"
Expand Down
Loading