Skip to content

Commit cda2084

Browse files
committed
BUG: 1 ** NA issue in computing new fill value in SparseSeries. close pandas-dev#2220
1 parent b4f4e25 commit cda2084

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

RELEASE.rst

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ pandas 0.9.1
102102
e.g. DataFrame.index (#2200)
103103
- Fix conversion of mixed-type DataFrame to ndarray with dup columns (#2236)
104104
- Fix duplicate columns issue (#2218, #2219)
105+
- Fix SparseSeries.__pow__ issue with NA input (#2220)
105106
106107
pandas 0.9.0
107108
============

pandas/sparse/series.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@ def wrapper(self, other):
4242
elif isinstance(other, DataFrame):
4343
return NotImplemented
4444
elif np.isscalar(other):
45-
new_fill_value = op(np.float64(self.fill_value),
46-
np.float64(other))
45+
if isnull(other) or isnull(self.fill_value):
46+
new_fill_value = np.nan
47+
else:
48+
new_fill_value = op(np.float64(self.fill_value),
49+
np.float64(other))
4750

4851
return SparseSeries(op(self.sp_values, other),
4952
index=self.index,

pandas/sparse/tests/test_sparse.py

+16
Original file line numberDiff line numberDiff line change
@@ -1297,6 +1297,22 @@ def test_isin(self):
12971297
rs = sparse_df[sparse_df.flag.isin([1.])]
12981298
assert_frame_equal(xp, rs)
12991299

1300+
def test_sparse_pow_issue(self):
1301+
# #2220
1302+
df = SparseDataFrame({'A' : [1.1,3.3],'B' : [2.5,-3.9]})
1303+
1304+
# note : no error without nan
1305+
df = SparseDataFrame({'A' : [nan, 0, 1] })
1306+
1307+
# note that 2 ** df works fine, also df ** 1
1308+
result = 1 ** df
1309+
1310+
r1 = result.take([0],1)['A']
1311+
r2 = result['A']
1312+
1313+
self.assertEqual(len(r2.sp_values), len(r1.sp_values))
1314+
1315+
13001316
def _dense_series_compare(s, f):
13011317
result = f(s)
13021318
assert(isinstance(result, SparseSeries))

0 commit comments

Comments
 (0)