Skip to content

Commit d3c6ab4

Browse files
authored
Min max sparse (pandas-dev#41159)
1 parent 770e0e9 commit d3c6ab4

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

doc/source/whatsnew/v1.3.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ Sparse
878878

879879
- Bug in :meth:`DataFrame.sparse.to_coo` raising ``KeyError`` with columns that are a numeric :class:`Index` without a 0 (:issue:`18414`)
880880
- Bug in :meth:`SparseArray.astype` with ``copy=False`` producing incorrect results when going from integer dtype to floating dtype (:issue:`34456`)
881-
-
881+
- Implemented :meth:`SparseArray.max` and :meth:`SparseArray.min` (:issue:`40921`)
882882

883883
ExtensionArray
884884
^^^^^^^^^^^^^^

pandas/core/arrays/sparse/array.py

+18
Original file line numberDiff line numberDiff line change
@@ -1392,6 +1392,24 @@ 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+
def max(self, axis=0, *args, **kwargs):
1396+
nv.validate_max(args, kwargs)
1397+
1398+
# This condition returns a nan if there are no valid values in the array.
1399+
if self.size > 0 and self._valid_sp_values.size == 0:
1400+
return np.nan
1401+
else:
1402+
return np.nanmax(self, axis)
1403+
1404+
def min(self, axis=0, *args, **kwargs):
1405+
nv.validate_min(args, kwargs)
1406+
1407+
# This condition returns a nan if there are no valid values in the array.
1408+
if self.size > 0 and self._valid_sp_values.size == 0:
1409+
return np.nan
1410+
else:
1411+
return np.nanmin(self, axis)
1412+
13951413
# ------------------------------------------------------------------------
13961414
# Ufuncs
13971415
# ------------------------------------------------------------------------

pandas/tests/arrays/sparse/test_array.py

+22
Original file line numberDiff line numberDiff line change
@@ -1311,3 +1311,25 @@ def test_dropna(fill_value):
13111311
df = pd.DataFrame({"a": [0, 1], "b": arr})
13121312
expected_df = pd.DataFrame({"a": [1], "b": exp}, index=pd.Int64Index([1]))
13131313
tm.assert_equal(df.dropna(), expected_df)
1314+
1315+
1316+
class TestMinMax:
1317+
plain_data = np.arange(5).astype(float)
1318+
data_neg = plain_data * (-1)
1319+
data_NaN = SparseArray(np.array([0, 1, 2, np.nan, 4]))
1320+
data_all_NaN = SparseArray(np.array([np.nan, np.nan, np.nan, np.nan, np.nan]))
1321+
1322+
@pytest.mark.parametrize(
1323+
"raw_data,max_expected,min_expected",
1324+
[
1325+
(plain_data, [4], [0]),
1326+
(data_neg, [0], [-4]),
1327+
(data_NaN, [4], [0]),
1328+
(data_all_NaN, [np.nan], [np.nan]),
1329+
],
1330+
)
1331+
def test_maxmin(self, raw_data, max_expected, min_expected):
1332+
max_result = SparseArray(raw_data).max()
1333+
min_result = SparseArray(raw_data).min()
1334+
assert max_result in max_expected
1335+
assert min_result in min_expected

0 commit comments

Comments
 (0)