|
25 | 25 | infer_dtype_from_scalar) |
26 | 26 | from pandas.core.dtypes.common import ( |
27 | 27 | is_array_like, is_bool_dtype, is_datetime64_any_dtype, is_dtype_equal, |
28 | | - is_integer, is_object_dtype, is_scalar, is_string_dtype, pandas_dtype) |
| 28 | + is_float_dtype, is_integer, is_integer_dtype, is_object_dtype, is_scalar, |
| 29 | + is_string_dtype, pandas_dtype) |
29 | 30 | from pandas.core.dtypes.dtypes import register_extension_dtype |
30 | 31 | from pandas.core.dtypes.generic import ( |
31 | 32 | ABCIndexClass, ABCSeries, ABCSparseArray, ABCSparseSeries) |
@@ -1927,15 +1928,24 @@ def make_sparse(arr, kind='block', fill_value=None, dtype=None, copy=False): |
1927 | 1928 | index = _make_index(length, indices, kind) |
1928 | 1929 | sparsified_values = arr[mask] |
1929 | 1930 |
|
1930 | | - # careful about casting here as we could easily specify a type that |
1931 | | - # cannot hold the resulting values, e.g. integer when we have floats |
1932 | | - # if we don't have an object specified then use this as the cast |
1933 | 1931 | if dtype is not None: |
1934 | 1932 |
|
1935 | | - ok_to_cast = all(not (is_object_dtype(t) or is_bool_dtype(t)) |
1936 | | - for t in (dtype, sparsified_values.dtype)) |
1937 | | - if ok_to_cast: |
| 1933 | + # careful about casting here as we could easily specify a type that |
| 1934 | + # cannot hold the resulting values, e.g. integer when we have floats |
| 1935 | + # if this is not safe then convert the dtype; note that if there are |
| 1936 | + # nan's in the source array this will raise |
| 1937 | + |
| 1938 | + # TODO: ideally this would be done by 'safe' casting in astype_nansafe |
| 1939 | + # but alas too many cases rely upon this working in the current way |
| 1940 | + # and casting='safe' doesn't really work in numpy properly |
| 1941 | + if is_integer_dtype(dtype) and is_float_dtype(sparsified_values.dtype): |
| 1942 | + result = astype_nansafe( |
| 1943 | + sparsified_values, dtype=dtype) |
| 1944 | + if np.allclose(result, sparsified_values, rtol=0): |
| 1945 | + return result, index, fill_value |
| 1946 | + |
1938 | 1947 | dtype = find_common_type([dtype, sparsified_values.dtype]) |
| 1948 | + |
1939 | 1949 | sparsified_values = astype_nansafe( |
1940 | 1950 | sparsified_values, dtype=dtype) |
1941 | 1951 |
|
|
0 commit comments