Skip to content

Commit 7c29393

Browse files
committed
allow coercing casting
1 parent d6db2ea commit 7c29393

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
@@ -1926,8 +1926,20 @@ def make_sparse(arr, kind='block', fill_value=None, dtype=None, copy=False):
19261926

19271927
index = _make_index(length, indices, kind)
19281928
sparsified_values = arr[mask]
1929+
1930+
# careful about casting here
1931+
# as we could easily specify a type that cannot hold the resulting values
1932+
# e.g. integer when we have floats
19291933
if dtype is not None:
1930-
sparsified_values = astype_nansafe(sparsified_values, dtype=dtype)
1934+
try:
1935+
sparsified_values = astype_nansafe(
1936+
sparsified_values, dtype=dtype, casting='same_kind')
1937+
except TypeError:
1938+
dtype = 'float64'
1939+
sparsified_values = astype_nansafe(
1940+
sparsified_values, dtype=dtype, casting='unsafe')
1941+
1942+
19311943
# TODO: copy
19321944
return sparsified_values, index, fill_value
19331945

pandas/core/series.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -782,10 +782,7 @@ def __array_wrap__(self, result: np.ndarray,
782782

783783
# we try to cast extension array types back to the original
784784
if is_extension_array_dtype(self):
785-
result = result.astype(self.dtype,
786-
copy=False,
787-
errors='ignore',
788-
casting='same_kind')
785+
result = result.astype(self.dtype, copy=False)
789786

790787
return result.__finalize__(self)
791788

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)