From a7c1279648b7687fa0505930a6f265bc19cf5fdc Mon Sep 17 00:00:00 2001 From: Anthony Volk Date: Wed, 23 Oct 2024 19:38:24 +0200 Subject: [PATCH] fix: Prevent floating-point error --- microdf/generic.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/microdf/generic.py b/microdf/generic.py index 86149b7..7947769 100644 --- a/microdf/generic.py +++ b/microdf/generic.py @@ -251,23 +251,24 @@ def rank(self, pct=False) -> pd.Series: ranks = np.array(self.weights.values)[order].cumsum()[inverse_order] if pct: ranks /= self.weights.values.sum() + np.where(ranks > 1.0, 1.0, ranks) return pd.Series(ranks, index=self.index) @vector_function def decile_rank(self): - return MicroSeries(np.ceil(self.rank(pct=True) * 10)) + return MicroSeries(np.minimum(np.ceil(self.rank(pct=True) * 10), 10)) @vector_function def quintile_rank(self): - return MicroSeries(np.ceil(self.rank(pct=True) * 5)) + return MicroSeries(np.minimum(np.ceil(self.rank(pct=True) * 5), 5)) @vector_function def quartile_rank(self): - return MicroSeries(np.ceil(self.rank(pct=True) * 4)) + return MicroSeries(np.minimum(np.ceil(self.rank(pct=True) * 4), 4)) @vector_function def percentile_rank(self): - return MicroSeries(np.ceil(self.rank(pct=True) * 100)) + return MicroSeries(np.minimum(np.ceil(self.rank(pct=True) * 100), 100)) def groupby(self, *args, **kwargs): gb = super().groupby(*args, **kwargs)