Skip to content

Commit 85a99b4

Browse files
committed
Added test case and function to support NaN
1 parent 5dabba3 commit 85a99b4

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

doc/source/whatsnew/v1.3.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ Sparse
874874

875875
- Bug in :meth:`DataFrame.sparse.to_coo` raising ``KeyError`` with columns that are a numeric :class:`Index` without a 0 (:issue:`18414`)
876876
- Bug in :meth:`SparseArray.astype` with ``copy=False`` producing incorrect results when going from integer dtype to floating dtype (:issue:`34456`)
877-
- Bug in :meth:`max` and `min` for SparseArrat type do not exist (:issue:`40921`)
877+
- Bug in :meth:`max` and `min` for :class:`SparseArray` type do not exist (:issue:`40921`)
878878

879879
ExtensionArray
880880
^^^^^^^^^^^^^^

pandas/core/arrays/sparse/array.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,20 +1392,26 @@ def mean(self, axis=0, *args, **kwargs):
13921392
nsparse = self.sp_index.ngaps
13931393
return (sp_sum + self.fill_value * nsparse) / (ct + nsparse)
13941394

1395-
13961395
def max(self, axis=0, *args, **kwargs):
13971396
nv.validate_max(args, kwargs)
13981397

13991398
if self.sp_index.ngaps > 0 and np.all(self._valid_sp_values < 0):
1399+
1400+
if self.size>0 and self._valid_sp_values.size==0:
1401+
return np.nan
1402+
14001403
return 0
14011404
else:
14021405
return np.amax(self._valid_sp_values, axis)
14031406

1404-
14051407
def min(self, axis=0, *args, **kwargs):
14061408
nv.validate_min(args, kwargs)
14071409

14081410
if self.sp_index.ngaps > 0 and np.all(self._valid_sp_values > 0):
1411+
1412+
if self.size>0 and self._valid_sp_values.size==0:
1413+
return np.nan
1414+
14091415
return 0
14101416
else:
14111417
return np.amin(self._valid_sp_values, axis)

0 commit comments

Comments
 (0)