File tree 1 file changed +41
-0
lines changed
1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 31
31
32
32
33
33
def _ensure_float (arr ):
34
+ """
35
+ Ensure that an array object has a float dtype if possible.
36
+
37
+ Parameters
38
+ ----------
39
+ arr : The array whose data type we want to enforce as float.
40
+
41
+ Returns
42
+ -------
43
+ float_arr : The original array cast to the float dtype if
44
+ possible. Otherwise, the original array is returned.
45
+
46
+ Examples
47
+ --------
48
+ >>> arr = np.array([1, 2, 3], dtype=np.int64)
49
+ >>> arr
50
+ array([1, 2, 3])
51
+ >>>
52
+ >>> arr.dtype
53
+ dtype('int64')
54
+ >>>
55
+ >>> float_arr = _ensure_float(arr)
56
+ >>> float_arr
57
+ array([ 1., 2., 3.])
58
+ >>>
59
+ >>> float_arr.dtype
60
+ dtype('float64')
61
+
62
+ If the array is not of the integer dtype, we return it as is:
63
+
64
+ >>> arr = np.array(['a', 'b', 'c'])
65
+ >>> arr
66
+ array(['a', 'b', 'c'],
67
+ dtype='<U1')
68
+ >>>
69
+ >>> float_arr = _ensure_float(arr)
70
+ >>> float_arr
71
+ array(['a', 'b', 'c'],
72
+ dtype='<U1')
73
+ """
74
+
34
75
if issubclass (arr .dtype .type , (np .integer , np .bool_ )):
35
76
arr = arr .astype (float )
36
77
return arr
You can’t perform that action at this time.
0 commit comments