Skip to content

Commit d02b811

Browse files
committed
allow coercing casting
1 parent 1afad6f commit d02b811

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

pandas/core/arrays/sparse.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -1915,8 +1915,20 @@ def make_sparse(arr, kind='block', fill_value=None, dtype=None, copy=False):
19151915

19161916
index = _make_index(length, indices, kind)
19171917
sparsified_values = arr[mask]
1918+
1919+
# careful about casting here
1920+
# as we could easily specify a type that cannot hold the resulting values
1921+
# e.g. integer when we have floats
19181922
if dtype is not None:
1919-
sparsified_values = astype_nansafe(sparsified_values, dtype=dtype)
1923+
try:
1924+
sparsified_values = astype_nansafe(
1925+
sparsified_values, dtype=dtype, casting='same_kind')
1926+
except TypeError:
1927+
dtype = 'float64'
1928+
sparsified_values = astype_nansafe(
1929+
sparsified_values, dtype=dtype, casting='unsafe')
1930+
1931+
19201932
# TODO: copy
19211933
return sparsified_values, index, fill_value
19221934

pandas/core/series.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -765,10 +765,7 @@ def __array_wrap__(self, result: np.ndarray,
765765

766766
# we try to cast extension array types back to the original
767767
if is_extension_array_dtype(self):
768-
result = result.astype(self.dtype,
769-
copy=False,
770-
errors='ignore',
771-
casting='same_kind')
768+
result = result.astype(self.dtype, copy=False)
772769

773770
return result.__finalize__(self)
774771

pandas/tests/sparse/frame/test_analytics.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,18 @@ def test_quantile_multi():
4242
tm.assert_sp_frame_equal(result, sparse_expected)
4343

4444

45+
@pytest.mark.parametrize(
46+
'data, dtype',
47+
[([1, np.nan, 3], SparseDtype('float64', np.nan)),
48+
([1, 2, 3], SparseDtype('int'))])
4549
@pytest.mark.parametrize('func', [np.exp, np.sqrt], ids=str)
46-
def test_ufunc(func):
50+
def test_ufunc(data, dtype, func):
4751
# GH 23743
4852
# assert we preserve the incoming dtype on ufunc operation
4953
df = DataFrame(
50-
{'A': Series([1, np.nan, 3], dtype=SparseDtype('float64', np.nan))})
54+
{'A': Series(data, dtype=dtype)})
5155
result = func(df)
5256
expected = DataFrame(
53-
{'A': Series(func([1, np.nan, 3]),
54-
dtype=SparseDtype('float64', np.nan))})
57+
{'A': Series(func(data),
58+
dtype=dtype)})
5559
tm.assert_frame_equal(result, expected)

pandas/tests/sparse/series/test_analytics.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
from pandas.util import testing as tm
66

77

8+
@pytest.mark.parametrize(
9+
'data, dtype',
10+
[([1, np.nan, 3], SparseDtype('float64', np.nan)),
11+
([1, 2, 3], SparseDtype('int'))])
812
@pytest.mark.parametrize('func', [np.exp, np.sqrt], ids=str)
9-
def test_ufunc(func):
13+
def test_ufunc(data, dtype, func):
1014
# GH 23743
1115
# assert we preserve the incoming dtype on ufunc operation
12-
s = Series([1, np.nan, 3], dtype=SparseDtype('float64', np.nan))
16+
s = Series(data, dtype=dtype)
1317
result = func(s)
14-
expected = Series(func([1, np.nan, 3]),
15-
dtype=SparseDtype('float64', np.nan))
18+
expected = Series(func(data),
19+
dtype=dtype)
1620
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)