Skip to content

Commit e1c38c9

Browse files
authored
API: stop special-casing SparseArray._quantile (#49583)
* API: stop special-casing SparseArray._quantile * GH ref
1 parent 2f751ad commit e1c38c9

File tree

4 files changed

+3
-26
lines changed

4 files changed

+3
-26
lines changed

doc/source/whatsnew/v2.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ Other API changes
336336
- Passing ``dtype`` of "timedelta64[s]", "timedelta64[ms]", or "timedelta64[us]" to :class:`TimedeltaIndex`, :class:`Series`, or :class:`DataFrame` constructors will now retain that dtype instead of casting to "timedelta64[ns]"; passing a dtype with lower resolution for :class:`Series` or :class:`DataFrame` will be cast to the lowest supported resolution "timedelta64[s]" (:issue:`49014`)
337337
- Passing a ``np.datetime64`` object with non-nanosecond resolution to :class:`Timestamp` will retain the input resolution if it is "s", "ms", or "ns"; otherwise it will be cast to the closest supported resolution (:issue:`49008`)
338338
- The ``other`` argument in :meth:`DataFrame.mask` and :meth:`Series.mask` now defaults to ``no_default`` instead of ``np.nan`` consistent with :meth:`DataFrame.where` and :meth:`Series.where`. Entries will be filled with the corresponding NULL value (``np.nan`` for numpy dtypes, ``pd.NA`` for extension dtypes). (:issue:`49111`)
339+
- Changed behavior of :meth:`Series.quantile` and :meth:`DataFrame.quantile` with :class:`SparseDtype` to retain sparse dtype (:issue:`49583`)
339340
- When creating a :class:`Series` with a object-dtype :class:`Index` of datetime objects, pandas no longer silently converts the index to a :class:`DatetimeIndex` (:issue:`39307`, :issue:`23598`)
340341
- :meth:`Series.unique` with dtype "timedelta64[ns]" or "datetime64[ns]" now returns :class:`TimedeltaArray` or :class:`DatetimeArray` instead of ``numpy.ndarray`` (:issue:`49176`)
341342
- :func:`to_datetime` and :class:`DatetimeIndex` now allow sequences containing both ``datetime`` objects and numeric entries, matching :class:`Series` behavior (:issue:`49037`)

pandas/core/arrays/sparse/array.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@
8686
ops,
8787
)
8888
import pandas.core.algorithms as algos
89-
from pandas.core.array_algos.quantile import quantile_with_mask
9089
from pandas.core.arraylike import OpsMixin
9190
from pandas.core.arrays import ExtensionArray
9291
from pandas.core.arrays.sparse.dtype import SparseDtype
@@ -909,29 +908,6 @@ def value_counts(self, dropna: bool = True) -> Series:
909908
index = keys
910909
return Series(counts, index=index)
911910

912-
def _quantile(self, qs: npt.NDArray[np.float64], interpolation: str):
913-
914-
if self._null_fill_value or self.sp_index.ngaps == 0:
915-
# We can avoid densifying
916-
npvalues = self.sp_values
917-
mask = np.zeros(npvalues.shape, dtype=bool)
918-
else:
919-
npvalues = self.to_numpy()
920-
mask = self.isna()
921-
922-
fill_value = na_value_for_dtype(npvalues.dtype, compat=False)
923-
res_values = quantile_with_mask(
924-
npvalues,
925-
mask,
926-
fill_value,
927-
qs,
928-
interpolation,
929-
)
930-
931-
# Special case: the returned array isn't _really_ sparse, so we don't
932-
# wrap it in a SparseArray
933-
return res_values
934-
935911
# --------
936912
# Indexing
937913
# --------

pandas/tests/frame/methods/test_quantile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_quantile_sparse(self, df, expected):
4747
# GH#17198
4848
# GH#24600
4949
result = df.quantile()
50-
50+
expected = expected.astype("Sparse[float]")
5151
tm.assert_series_equal(result, expected)
5252

5353
def test_quantile(

pandas/tests/series/methods/test_quantile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def test_quantile_nat(self):
183183
def test_quantile_sparse(self, values, dtype):
184184
ser = Series(values, dtype=dtype)
185185
result = ser.quantile([0.5])
186-
expected = Series(np.asarray(ser)).quantile([0.5])
186+
expected = Series(np.asarray(ser)).quantile([0.5]).astype("Sparse[float]")
187187
tm.assert_series_equal(result, expected)
188188

189189
def test_quantile_empty(self):

0 commit comments

Comments
 (0)