Skip to content

Commit cd0b842

Browse files
committed
Global s/np.issubdtype/safe_issubdtype/. Probably most cases were OK, but this is easier than carefully checking each!
1 parent c25fdb5 commit cd0b842

File tree

6 files changed

+19
-17
lines changed

6 files changed

+19
-17
lines changed

patsy/build.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
CategoricalSniffer,
1818
categorical_to_int)
1919
from patsy.util import (atleast_2d_column_default,
20-
have_pandas, asarray_or_pandas)
20+
have_pandas, asarray_or_pandas,
21+
safe_issubdtype)
2122
from patsy.design_info import DesignMatrix, DesignInfo
2223
from patsy.redundancy import pick_contrasts_for_term
2324
from patsy.desc import ModelDesc
@@ -76,7 +77,7 @@ def eval(self, data, NA_action):
7677
% (self.factor.name(), self._expected_columns,
7778
result.shape[1]),
7879
self.factor)
79-
if not np.issubdtype(np.asarray(result).dtype, np.number):
80+
if not safe_issubdtype(np.asarray(result).dtype, np.number):
8081
raise PatsyError("when evaluating numeric factor %s, "
8182
"I got non-numeric data of type '%s'"
8283
% (self.factor.name(), result.dtype),

patsy/categorical.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
safe_is_pandas_categorical,
4646
pandas_Categorical_from_codes,
4747
pandas_Categorical_categories,
48-
pandas_Categorical_codes)
48+
pandas_Categorical_codes,
49+
safe_issubdtype)
4950

5051
if have_pandas:
5152
import pandas
@@ -123,7 +124,7 @@ def guess_categorical(data):
123124
if isinstance(data, _CategoricalBox):
124125
return True
125126
data = np.asarray(data)
126-
if np.issubdtype(data.dtype, np.number):
127+
if safe_issubdtype(data.dtype, np.number):
127128
return False
128129
return True
129130

@@ -190,7 +191,7 @@ def sniff(self, data):
190191
data = data.data
191192
# fastpath to avoid doing an item-by-item iteration over boolean
192193
# arrays, as requested by #44
193-
if hasattr(data, "dtype") and np.issubdtype(data.dtype, np.bool_):
194+
if hasattr(data, "dtype") and safe_issubdtype(data.dtype, np.bool_):
194195
self._level_set = set([True, False])
195196
return True
196197

@@ -333,7 +334,7 @@ def categorical_to_int(data, levels, NA_action, origin=None):
333334

334335
# fastpath to avoid doing an item-by-item iteration over boolean arrays,
335336
# as requested by #44
336-
if hasattr(data, "dtype") and np.issubdtype(data.dtype, np.bool_):
337+
if hasattr(data, "dtype") and safe_issubdtype(data.dtype, np.bool_):
337338
if level_to_int[False] == 0 and level_to_int[True] == 1:
338339
return data.astype(np.int_)
339340
out = np.empty(len(data), dtype=int)

patsy/contrasts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import numpy as np
1616
from patsy import PatsyError
1717
from patsy.compat import triu_indices, tril_indices, diag_indices
18-
from patsy.util import repr_pretty_delegate, repr_pretty_impl
18+
from patsy.util import repr_pretty_delegate, repr_pretty_impl, safe_issubdtype
1919

2020
class ContrastMatrix(object):
2121
"""A simple container for a matrix used for coding categorical factors.
@@ -567,7 +567,7 @@ def code_contrast_matrix(intercept, levels, contrast, default=None):
567567
if isinstance(contrast, ContrastMatrix):
568568
return contrast
569569
as_array = np.asarray(contrast)
570-
if np.issubdtype(as_array.dtype, np.number):
570+
if safe_issubdtype(as_array.dtype, np.number):
571571
return ContrastMatrix(as_array,
572572
_name_levels("custom", range(as_array.shape[1])))
573573
if intercept:

patsy/design_info.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from patsy import PatsyError
2020
from patsy.util import atleast_2d_column_default
2121
from patsy.compat import OrderedDict
22-
from patsy.util import repr_pretty_delegate, repr_pretty_impl
22+
from patsy.util import repr_pretty_delegate, repr_pretty_impl, safe_issubdtype
2323
from patsy.constraint import linear_constraint
2424

2525
class DesignInfo(object):
@@ -278,7 +278,7 @@ def from_array(cls, array_like, default_column_prefix="column"):
278278
raise ValueError("design matrix can't have >2 dimensions")
279279
columns = getattr(arr, "columns", range(arr.shape[1]))
280280
if (hasattr(columns, "dtype")
281-
and not np.issubdtype(columns.dtype, np.integer)):
281+
and not safe_issubdtype(columns.dtype, np.integer)):
282282
column_names = [str(obj) for obj in columns]
283283
else:
284284
column_names = ["%s%s" % (default_column_prefix, i)
@@ -527,7 +527,7 @@ def __new__(cls, input_array, design_info=None,
527527
return input_array
528528
self = atleast_2d_column_default(input_array).view(cls)
529529
# Upcast integer to floating point
530-
if np.issubdtype(self.dtype, np.integer):
530+
if safe_issubdtype(self.dtype, np.integer):
531531
self = np.asarray(self, dtype=float).view(cls)
532532
if self.ndim > 2:
533533
raise ValueError("DesignMatrix must be 2d")
@@ -539,7 +539,7 @@ def __new__(cls, input_array, design_info=None,
539539
"(got %s, wanted %s)"
540540
% (len(design_info.column_names), self.shape[1]))
541541
self.design_info = design_info
542-
if not np.issubdtype(self.dtype, np.floating):
542+
if not safe_issubdtype(self.dtype, np.floating):
543543
raise ValueError("design matrix must be real-valued floating point")
544544
return self
545545

patsy/state.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import numpy as np
2828
from patsy.util import (atleast_2d_column_default,
2929
asarray_or_pandas, pandas_friendly_reshape,
30-
wide_dtype_for)
30+
wide_dtype_for, safe_issubdtype)
3131
from patsy.compat import wraps
3232

3333
# These are made available in the patsy.* namespace
@@ -107,7 +107,7 @@ def transform(self, x):
107107
# heterogenous types. And in that case we're going to be munging the
108108
# types anyway, so copying isn't a big deal.
109109
x_arr = np.asarray(x)
110-
if np.issubdtype(x_arr.dtype, np.integer):
110+
if safe_issubdtype(x_arr.dtype, np.integer):
111111
dt = float
112112
else:
113113
dt = x_arr.dtype

patsy/util.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,10 @@ def test_to_uniqueify_list():
270270

271271
def wide_dtype_for(arr):
272272
arr = np.asarray(arr)
273-
if (np.issubdtype(arr.dtype, np.integer)
274-
or np.issubdtype(arr.dtype, np.floating)):
273+
if (safe_issubdtype(arr.dtype, np.integer)
274+
or safe_issubdtype(arr.dtype, np.floating)):
275275
return widest_float
276-
elif np.issubdtype(arr.dtype, np.complexfloating):
276+
elif safe_issubdtype(arr.dtype, np.complexfloating):
277277
return widest_complex
278278
raise ValueError("cannot widen a non-numeric type %r" % (arr.dtype,))
279279

0 commit comments

Comments
 (0)