Skip to content

Commit

Permalink
FIX: handle potential TypeError when determining variable type (#3516)
Browse files Browse the repository at this point in the history
* FIX: handle potential TypeError when determining variable type

* Handle edge case in duplicated _core code

* Add tests

---------

Co-authored-by: Michael Waskom <[email protected]>
  • Loading branch information
theOehrly and mwaskom authored Nov 4, 2023
1 parent 8ed641f commit ea51c41
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
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

0 comments on commit ea51c41

Please sign in to comment.