Skip to content

Commit ea51c41

Browse files
theOehrlymwaskom
andauthored
FIX: handle potential TypeError when determining variable type (#3516)
* FIX: handle potential TypeError when determining variable type * Handle edge case in duplicated _core code * Add tests --------- Co-authored-by: Michael Waskom <[email protected]>
1 parent 8ed641f commit ea51c41

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

seaborn/_base.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,8 +1520,13 @@ def variable_type(vector, boolean_type="numeric"):
15201520
warnings.simplefilter(
15211521
action='ignore', category=(FutureWarning, DeprecationWarning)
15221522
)
1523-
if np.isin(vector, [0, 1]).all():
1524-
return VariableType(boolean_type)
1523+
try:
1524+
if np.isin(vector, [0, 1]).all():
1525+
return VariableType(boolean_type)
1526+
except TypeError:
1527+
# .isin comparison is not guaranteed to be possible under NumPy
1528+
# casting rules, depending on the (unknown) dtype of 'vector'
1529+
pass
15251530

15261531
# Defer to positive pandas tests
15271532
if pd.api.types.is_numeric_dtype(vector):

seaborn/_core/rules.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,12 @@ def variable_type(
9797
boolean_dtypes = ["bool"]
9898
boolean_vector = vector.dtype in boolean_dtypes
9999
else:
100-
boolean_vector = bool(np.isin(vector, [0, 1]).all())
100+
try:
101+
boolean_vector = bool(np.isin(vector, [0, 1]).all())
102+
except TypeError:
103+
# .isin comparison is not guaranteed to be possible under NumPy
104+
# casting rules, depending on the (unknown) dtype of 'vector'
105+
boolean_vector = False
101106
if boolean_vector:
102107
return VarType(boolean_type)
103108

tests/_core/test_rules.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ def test_variable_type():
5252
assert variable_type(s, boolean_type="categorical") == "categorical"
5353
assert variable_type(s, boolean_type="boolean") == "boolean"
5454

55+
# This should arguably be datmetime, but we don't currently handle it correctly
56+
# Test is mainly asserting that this doesn't fail on the boolean check.
57+
s = pd.timedelta_range(1, periods=3, freq="D").to_series()
58+
assert variable_type(s) == "categorical"
59+
5560
s_cat = s.astype("category")
5661
assert variable_type(s_cat, boolean_type="categorical") == "categorical"
5762
assert variable_type(s_cat, boolean_type="numeric") == "categorical"
@@ -61,6 +66,9 @@ def test_variable_type():
6166
assert variable_type(s, boolean_type="boolean") == "boolean"
6267
assert variable_type(s, boolean_type="boolean", strict_boolean=True) == "numeric"
6368

69+
s = pd.Series([1, 0, 0])
70+
assert variable_type(s, boolean_type="boolean") == "boolean"
71+
6472
s = pd.Series([pd.Timestamp(1), pd.Timestamp(2)])
6573
assert variable_type(s) == "datetime"
6674
assert variable_type(s.astype(object)) == "datetime"

tests/test_base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,6 +1508,11 @@ def test_variable_type(self):
15081508
assert variable_type(s.to_numpy()) == "categorical"
15091509
assert variable_type(s.to_list()) == "categorical"
15101510

1511+
# This should arguably be datmetime, but we don't currently handle it correctly
1512+
# Test is mainly asserting that this doesn't fail on the boolean check.
1513+
s = pd.timedelta_range(1, periods=3, freq="D").to_series()
1514+
assert variable_type(s) == "categorical"
1515+
15111516
s = pd.Series([True, False, False])
15121517
assert variable_type(s) == "numeric"
15131518
assert variable_type(s, boolean_type="categorical") == "categorical"

0 commit comments

Comments
 (0)