Skip to content

Commit 576e90d

Browse files
committed
allow coercing casting
1 parent ab1d01a commit 576e90d

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

pandas/core/arrays/sparse.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1906,8 +1906,20 @@ def make_sparse(arr, kind='block', fill_value=None, dtype=None, copy=False):
19061906

19071907
index = _make_index(length, indices, kind)
19081908
sparsified_values = arr[mask]
1909+
1910+
# careful about casting here
1911+
# as we could easily specify a type that cannot hold the resulting values
1912+
# e.g. integer when we have floats
19091913
if dtype is not None:
1910-
sparsified_values = astype_nansafe(sparsified_values, dtype=dtype)
1914+
try:
1915+
sparsified_values = astype_nansafe(
1916+
sparsified_values, dtype=dtype, casting='same_kind')
1917+
except TypeError:
1918+
dtype = 'float64'
1919+
sparsified_values = astype_nansafe(
1920+
sparsified_values, dtype=dtype, casting='unsafe')
1921+
1922+
19111923
# TODO: copy
19121924
return sparsified_values, index, fill_value
19131925

pandas/core/series.py

Lines changed: 1 addition & 4 deletions
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

Lines changed: 8 additions & 4 deletions
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

Lines changed: 8 additions & 4 deletions
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)