Skip to content

Commit 34f07a7

Browse files
bodevoneAlibi Shalgymbaydvora-h
authored
Add TDIGEST.TRIMMED_MEAN (#2300)
* Add tdigest trimmed mean command with test * Add skip version for test * add to module callbacks Co-authored-by: Alibi Shalgymbay <[email protected]> Co-authored-by: dvora-h <[email protected]> Co-authored-by: dvora-h <[email protected]>
1 parent aca0050 commit 34f07a7

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

redis/commands/bf/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ def __init__(self, client, **kwargs):
170170
TDIGEST_QUANTILE: parse_tdigest_quantile,
171171
TDIGEST_MIN: float,
172172
TDIGEST_MAX: float,
173+
TDIGEST_TRIMMED_MEAN: float,
173174
TDIGEST_INFO: TDigestInfo,
174175
}
175176

redis/commands/bf/commands.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
TDIGEST_MIN = "TDIGEST.MIN"
5050
TDIGEST_MAX = "TDIGEST.MAX"
5151
TDIGEST_INFO = "TDIGEST.INFO"
52+
TDIGEST_TRIMMED_MEAN = "TDIGEST.TRIMMED_MEAN"
5253
TDIGEST_MERGESTORE = "TDIGEST.MERGESTORE"
5354

5455

@@ -418,6 +419,16 @@ def info(self, key):
418419
""" # noqa
419420
return self.execute_command(TDIGEST_INFO, key)
420421

422+
def trimmed_mean(self, key, low_cut_quantile, high_cut_quantile):
423+
"""
424+
Return mean value from the sketch, excluding observation values outside
425+
the low and high cutoff quantiles.
426+
For more information see `TDIGEST.TRIMMED_MEAN <https://redis.io/commands/tdigest.trimmed_mean>`_.
427+
""" # noqa
428+
return self.execute_command(
429+
TDIGEST_TRIMMED_MEAN, key, low_cut_quantile, high_cut_quantile
430+
)
431+
421432
def mergestore(self, dest_key, numkeys, *sourcekeys, compression=False):
422433
"""
423434
Merges all of the values from `sourcekeys` keys to `dest_key` sketch.

tests/test_bloom.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,17 @@ def test_tdigest_cdf(client):
388388
assert 0.9 == round(client.tdigest().cdf("tDigest", 9.0), 1)
389389

390390

391+
@pytest.mark.redismod
392+
@pytest.mark.experimental
393+
@skip_ifmodversion_lt("2.4.0", "bf")
394+
def test_tdigest_trimmed_mean(client):
395+
assert client.tdigest().create("tDigest", 100)
396+
# insert data-points into sketch
397+
assert client.tdigest().add("tDigest", list(range(1, 10)), [1.0] * 10)
398+
assert 5 == client.tdigest().trimmed_mean("tDigest", 0.1, 0.9)
399+
assert 4.5 == client.tdigest().trimmed_mean("tDigest", 0.4, 0.5)
400+
401+
391402
@pytest.mark.redismod
392403
@pytest.mark.experimental
393404
@skip_ifmodversion_lt("2.4.0", "bf")

0 commit comments

Comments
 (0)