Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: quantile for ExtensionArray #39606

Merged
merged 9 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions pandas/core/array_algos/quantile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from typing import Sequence, Union

import numpy as np

from pandas._libs import lib

from pandas.core.dtypes.common import is_list_like

from pandas.core.nanops import nanpercentile


def quantile_with_mask(
values: np.ndarray,
mask: np.ndarray,
fill_value,
qs: Union[float, Sequence[float]],
interpolation: str,
axis: int,
) -> np.ndarray:
"""
Compute the quantiles of the given values for each quantile in `qs`.


Parameters
----------
values : np.ndarray
For ExtensionArray, this is _values_for_factorize()[0]
mask : np.ndarray[bool]
mask = isna(values)
For ExtensionArray, this is computed before calling _value_for_factorize
fill_value : Scalar
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also call this na_value ? (like nanpercentile does)

(I know we use both fill_value and na_value in many places, and somewhat interchangeably, but here I personally find na_value clearer)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about this and decided on fill_value on the theory that "na_value" means "the value that, when we see it, indicates we have an NA" and "fill_value" means "the value that we use when we need to fill in an NA value".

e.g. we get here with fill_value=iNaT, which would be weird to have as an na_value

The value to interpret fill NA entries with
For ExtensionArray, this is _values_for_factorize()[1]
qs : a scalar or list of the quantiles to be computed
interpolation : str
Type of interpolation
axis : int
Axis along which to compute quantiles.

Notes
-----
Assumes values is already 2D. For ExtensionArray this means np.atleast_2d
has been called on _values_for_factorize()[0]
"""
is_empty = values.shape[axis] == 0
orig_scalar = not is_list_like(qs)
if orig_scalar:
# make list-like, unpack later
qs = [qs]

if is_empty:
# create the array of na_values
# 2d len(values) * len(qs)
flat = np.array([fill_value] * len(qs))
result = np.repeat(flat, len(values)).reshape(len(values), len(qs))
else:
# asarray needed for Sparse, see GH#24600
result = nanpercentile(
values,
np.array(qs) * 100,
axis=axis,
na_value=fill_value,
mask=mask,
ndim=values.ndim,
interpolation=interpolation,
)

result = np.array(result, copy=False)
result = result.T

if orig_scalar:
assert result.shape[-1] == 1, result.shape
result = result[..., 0]
result = lib.item_from_zerodim(result)

return result
3 changes: 2 additions & 1 deletion pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,8 @@ def copy(self: DatetimeLikeArrayT) -> DatetimeLikeArrayT:
return new_obj

def _values_for_factorize(self):
return self._ndarray, iNaT
# int64 instead of int ensures we have a "view" method
return self._ndarray, np.int64(iNaT)

@classmethod
def _from_factorized(
Expand Down
93 changes: 42 additions & 51 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
putmask_smart,
putmask_without_repeat,
)
from pandas.core.array_algos.quantile import quantile_with_mask
from pandas.core.array_algos.replace import (
compare_or_regex_search,
replace_regex,
Expand All @@ -79,7 +80,6 @@
is_scalar_indexer,
)
import pandas.core.missing as missing
from pandas.core.nanops import nanpercentile

if TYPE_CHECKING:
from pandas import Index
Expand Down Expand Up @@ -1390,8 +1390,10 @@ def quantile(self, qs, interpolation="linear", axis: int = 0) -> Block:
Parameters
----------
qs: a scalar or list of the quantiles to be computed
interpolation: type of interpolation, default 'linear'
axis: axis to compute, default 0
interpolation : str, default "linear"
Type of interpolation
axis : int, default 0
Axis along which to compute quantiles.

Returns
-------
Expand All @@ -1400,44 +1402,16 @@ def quantile(self, qs, interpolation="linear", axis: int = 0) -> Block:
# We should always have ndim == 2 because Series dispatches to DataFrame
assert self.ndim == 2

values = self.get_values()

is_empty = values.shape[axis] == 0
orig_scalar = not is_list_like(qs)
if orig_scalar:
# make list-like, unpack later
qs = [qs]

if is_empty:
# create the array of na_values
# 2d len(values) * len(qs)
result = np.repeat(
np.array([self.fill_value] * len(qs)), len(values)
).reshape(len(values), len(qs))
else:
# asarray needed for Sparse, see GH#24600
mask = np.asarray(isna(values))
result = nanpercentile(
values,
np.array(qs) * 100,
axis=axis,
na_value=self.fill_value,
mask=mask,
ndim=values.ndim,
interpolation=interpolation,
)
fill_value = self.fill_value
values = self.values
mask = np.asarray(isna(values))

result = np.array(result, copy=False)
result = result.T
result = quantile_with_mask(values, mask, fill_value, qs, interpolation, axis)
ndim = np.ndim(result)

if orig_scalar and not lib.is_scalar(result):
# result could be scalar in case with is_empty and self.ndim == 1
assert result.shape[-1] == 1, result.shape
result = result[..., 0]
result = lib.item_from_zerodim(result)
placement = np.arange(len(result))

ndim = np.ndim(result)
return make_block(result, placement=np.arange(len(result)), ndim=ndim)
return make_block(result, placement=placement, ndim=ndim)

def _replace_coerce(
self,
Expand Down Expand Up @@ -1866,6 +1840,36 @@ def _unstack(self, unstacker, fill_value, new_placement):
]
return blocks, mask

def quantile(self, qs, interpolation="linear", axis: int = 0) -> Block:
# asarray needed for Sparse, see GH#24600
mask = np.asarray(isna(self.values))
mask = np.atleast_2d(mask)

values, fill_value = self.values._values_for_factorize()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, _values_for_factorize is not necessarily correct, but since quantile only makes sense for numeric-like dtypes (and for those dtypes, _values_for_factorize will most likely be the underlying numbers), that's probably OK.

(eg in geopands, _values_for_factorize returns serialized bytes, but quantile/percentile doesn't work on bytes anyway, so no direct problem)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think quantile makes sense for any ordered types, and IIUC values_for_factorize is supposed to preserve ordering

Copy link
Member

@jorisvandenbossche jorisvandenbossche Feb 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC values_for_factorize is supposed to preserve ordering

In theory I think that's not required for factorize (since we don't sort in factorization). In practice not sure if that matters though (can't think of a situation where it wouldn't be orderable).
But looking at my overview in #33276 again, I mentioned there that in principle we shouldn't use _values_for_factorize directly, but rather use the .factorize() method. But of course that's not usable for this use case.


values = np.atleast_2d(values)

result = quantile_with_mask(values, mask, fill_value, qs, interpolation, axis)
ndim = np.ndim(result)

if not is_sparse(self.dtype):
# shape[0] should be 1 as long as EAs are 1D

if result.ndim == 1:
# i.e. qs was originally a scalar
assert result.shape == (1,), result.shape
result = type(self.values)._from_factorized(result, self.values)
placement = np.arange(len(result))

else:
assert result.shape == (1, len(qs)), result.shape
result = type(self.values)._from_factorized(result[0], self.values)
placement = [0]
else:
placement = np.arange(len(result))

return make_block(result, placement=placement, ndim=ndim)


class HybridMixin:
"""
Expand Down Expand Up @@ -2184,19 +2188,6 @@ def fillna(
value, limit=limit, inplace=inplace, downcast=downcast
)

def quantile(self, qs, interpolation="linear", axis: int = 0) -> Block:
naive = self.values.view("M8[ns]")

# TODO(EA2D): kludge for 2D block with 1D values
naive = naive.reshape(self.shape)

blk = self.make_block(naive)
res_blk = blk.quantile(qs, interpolation=interpolation, axis=axis)

# TODO(EA2D): ravel is kludge for 2D block with 1D values, assumes column-like
aware = self._holder(res_blk.values.ravel(), dtype=self.dtype)
return self.make_block_same_class(aware, ndim=res_blk.ndim)

def _check_ndim(self, values, ndim):
"""
ndim inference and validation.
Expand Down
79 changes: 79 additions & 0 deletions pandas/tests/frame/methods/test_quantile.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,85 @@ def test_quantile(self, datetime_frame):
expected = Series([3.0, 4.0], index=[0, 1], name=0.5)
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize("as_dt64tz", [True, False])
def test_quantile_period(self, frame_or_series, as_dt64tz):
pi = pd.period_range("2016-01-01", periods=9, freq="D", name="A")
if as_dt64tz:
pi = pi.to_timestamp("S").tz_localize("US/Central")

obj = frame_or_series(pi)

qs = [0.5, 0, 1]
if frame_or_series is Series:
result = obj.quantile(qs)
else:
result = obj.quantile(qs, numeric_only=False)

expected = Series([pi[4], pi[0], pi[-1]], index=qs, name="A")
expected = frame_or_series(expected)

tm.assert_equal(result, expected)

# TODO: tests for axis=1?
# TODO: empty case? might as well do dt64 and td64 here too
@pytest.mark.parametrize("as_dt64tz", [True, False])
def test_quantile_period_with_nat(self, frame_or_series, as_dt64tz):
pi = pd.period_range("2016-01-01", periods=9, freq="D", name="A")
if as_dt64tz:
pi = pi.to_timestamp("S").tz_localize("US/Central")

obj = frame_or_series(pi)

obj.iloc[0] = pd.NaT
obj.iloc[-1] = pd.NaT

qs = [0.5, 0, 1]
if frame_or_series is Series:
result = obj.quantile(qs)
else:
result = obj.quantile(qs, numeric_only=False)

expected = Series([pi[4], pi[1], pi[-2]], index=qs, name="A")
expected = frame_or_series(expected)
tm.assert_equal(result, expected)

@pytest.mark.parametrize("as_dt64tz", [True, False])
def test_quantile_period_all_nat(self, frame_or_series, as_dt64tz):
pi = pd.period_range("2016-01-01", periods=9, freq="D", name="A")
if as_dt64tz:
pi = pi.to_timestamp("S").tz_localize("US/Central")

obj = frame_or_series(pi)
obj.iloc[:] = pd.NaT

qs = [0.5, 0, 1]
if frame_or_series is Series:
result = obj.quantile(qs)
else:
result = obj.quantile(qs, numeric_only=False)

expected = Series([pd.NaT, pd.NaT, pd.NaT], dtype=pi.dtype, index=qs, name="A")
expected = frame_or_series(expected)
tm.assert_equal(result, expected)

def test_quantile_period_scalar(self, frame_or_series):
# scalar qs
pi = pd.period_range("2016-01-01", periods=9, freq="D", name="A")
obj = frame_or_series(pi)

qs = 0.5
if frame_or_series is Series:
result = obj.quantile(qs)
else:
result = obj.quantile(qs, numeric_only=False)

expected = Series({"A": pi[4]}, name=0.5)
if frame_or_series is Series:
expected = expected["A"]
assert result == expected
else:
tm.assert_series_equal(result, expected)

def test_quantile_date_range(self):
# GH 2460

Expand Down