|
24 | 24 | infer_dtype_from_scalar)
|
25 | 25 | from pandas.core.dtypes.common import (
|
26 | 26 | is_array_like, is_bool_dtype, is_datetime64_any_dtype, is_dtype_equal,
|
27 |
| - is_integer, is_object_dtype, is_scalar, is_string_dtype, pandas_dtype) |
| 27 | + is_float_dtype, is_integer, is_integer_dtype, is_object_dtype, is_scalar, |
| 28 | + is_string_dtype, pandas_dtype) |
28 | 29 | from pandas.core.dtypes.dtypes import register_extension_dtype
|
29 | 30 | from pandas.core.dtypes.generic import (
|
30 | 31 | ABCIndexClass, ABCSeries, ABCSparseArray, ABCSparseSeries)
|
@@ -1907,15 +1908,24 @@ def make_sparse(arr, kind='block', fill_value=None, dtype=None, copy=False):
|
1907 | 1908 | index = _make_index(length, indices, kind)
|
1908 | 1909 | sparsified_values = arr[mask]
|
1909 | 1910 |
|
1910 |
| - # careful about casting here as we could easily specify a type that |
1911 |
| - # cannot hold the resulting values, e.g. integer when we have floats |
1912 |
| - # if we don't have an object specified then use this as the cast |
1913 | 1911 | if dtype is not None:
|
1914 | 1912 |
|
1915 |
| - ok_to_cast = all(not (is_object_dtype(t) or is_bool_dtype(t)) |
1916 |
| - for t in (dtype, sparsified_values.dtype)) |
1917 |
| - if ok_to_cast: |
| 1913 | + # careful about casting here as we could easily specify a type that |
| 1914 | + # cannot hold the resulting values, e.g. integer when we have floats |
| 1915 | + # if this is not safe then convert the dtype; note that if there are |
| 1916 | + # nan's in the source array this will raise |
| 1917 | + |
| 1918 | + # TODO: ideally this would be done by 'safe' casting in astype_nansafe |
| 1919 | + # but alas too many cases rely upon this working in the current way |
| 1920 | + # and casting='safe' doesn't really work in numpy properly |
| 1921 | + if is_integer_dtype(dtype) and is_float_dtype(sparsified_values.dtype): |
| 1922 | + result = astype_nansafe( |
| 1923 | + sparsified_values, dtype=dtype) |
| 1924 | + if np.allclose(result, sparsified_values, rtol=0): |
| 1925 | + return result, index, fill_value |
| 1926 | + |
1918 | 1927 | dtype = find_common_type([dtype, sparsified_values.dtype])
|
| 1928 | + |
1919 | 1929 | sparsified_values = astype_nansafe(
|
1920 | 1930 | sparsified_values, dtype=dtype)
|
1921 | 1931 |
|
|
0 commit comments