We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent d9e00d2 commit 29b4b58Copy full SHA for 29b4b58
pandas/types/common.py
@@ -31,6 +31,35 @@
31
32
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
63
if issubclass(arr.dtype.type, (np.integer, np.bool_)):
64
arr = arr.astype(float)
65
return arr
0 commit comments