Skip to content

Commit 3790cd2

Browse files
committed
Closes issue pandas-dev#10174. Added 'interpolation' keyword in Dataframe.quantile and Series.quantile
1 parent 9b04bd0 commit 3790cd2

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

pandas/core/frame.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -4468,7 +4468,7 @@ def mode(self, axis=0, numeric_only=False):
44684468
f = lambda s: s.mode()
44694469
return data.apply(f, axis=axis)
44704470

4471-
def quantile(self, q=0.5, axis=0, numeric_only=True):
4471+
def quantile(self, q=0.5, axis=0, numeric_only=True, interpolation='linear'):
44724472
"""
44734473
Return values at the given quantile over requested axis, a la
44744474
numpy.percentile.
@@ -4479,6 +4479,14 @@ def quantile(self, q=0.5, axis=0, numeric_only=True):
44794479
0 <= q <= 1, the quantile(s) to compute
44804480
axis : {0, 1}
44814481
0 for row-wise, 1 for column-wise
4482+
interpolation : {'linear', 'lower', 'higher', 'midpoint', 'nearest'}
4483+
Specifies the interpolation method to use, when the desired quantile lies between two data points i and j:
4484+
linear: i + (j - i) * fraction, where fraction is the fractional part of the index surrounded by i and j.
4485+
lower: i.
4486+
higher: j.
4487+
nearest: i or j whichever is nearest.
4488+
midpoint: (i + j) / 2.
4489+
44824490
44834491
Returns
44844492
-------
@@ -4512,7 +4520,7 @@ def quantile(self, q=0.5, axis=0, numeric_only=True):
45124520
else:
45134521
squeeze = False
45144522

4515-
def f(arr, per):
4523+
def f(arr, per,interpolation):
45164524
if arr._is_datelike_mixed_type:
45174525
values = _values_from_object(arr).view('i8')
45184526
else:
@@ -4521,7 +4529,7 @@ def f(arr, per):
45214529
if len(values) == 0:
45224530
return NA
45234531
else:
4524-
return _quantile(values, per)
4532+
return _quantile(values, per, interpolation=interpolation)
45254533

45264534
data = self._get_numeric_data() if numeric_only else self
45274535
if axis == 1:
@@ -4532,7 +4540,7 @@ def f(arr, per):
45324540
is_dt_col = data.dtypes.map(com.is_datetime64_dtype)
45334541
is_dt_col = is_dt_col[is_dt_col].index
45344542

4535-
quantiles = [[f(vals, x) for x in per]
4543+
quantiles = [[f(vals, x, interpolation) for x in per]
45364544
for (_, vals) in data.iteritems()]
45374545
result = DataFrame(quantiles, index=data._info_axis, columns=q).T
45384546
if len(is_dt_col) > 0:

pandas/core/series.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -1216,15 +1216,21 @@ def round(self, decimals=0, out=None):
12161216

12171217
return result
12181218

1219-
def quantile(self, q=0.5):
1219+
def quantile(self, q=0.5, interpolation='linear'):
12201220
"""
12211221
Return value at the given quantile, a la numpy.percentile.
12221222
12231223
Parameters
12241224
----------
12251225
q : float or array-like, default 0.5 (50% quantile)
12261226
0 <= q <= 1, the quantile(s) to compute
1227-
1227+
interpolation : {'linear', 'lower', 'higher', 'midpoint', 'nearest'}
1228+
Specifies the interpolation method to use, when the desired quantile lies between two data points i and j:
1229+
linear: i + (j - i) * fraction, where fraction is the fractional part of the index surrounded by i and j.
1230+
lower: i.
1231+
higher: j.
1232+
nearest: i or j whichever is nearest.
1233+
midpoint: (i + j) / 2.
12281234
Returns
12291235
-------
12301236
quantile : float or Series
@@ -1245,14 +1251,14 @@ def quantile(self, q=0.5):
12451251
"""
12461252
valid = self.dropna()
12471253

1248-
def multi(values, qs):
1254+
def multi(values, qs, interpolation):
12491255
if com.is_list_like(qs):
1250-
return Series([_quantile(values, x*100)
1256+
return Series([_quantile(values, x*100, interpolation=interpolation)
12511257
for x in qs], index=qs)
12521258
else:
1253-
return _quantile(values, qs*100)
1259+
return _quantile(values, qs*100, interpolation=interpolation)
12541260

1255-
return self._maybe_box(lambda values: multi(values, q), dropna=True)
1261+
return self._maybe_box(lambda values: multi(values, q, interpolation), dropna=True)
12561262

12571263
def ptp(self, axis=None, out=None):
12581264
return _values_from_object(self).ptp(axis, out)

0 commit comments

Comments
 (0)