Skip to content

Commit

Permalink
fix: Prevent floating-point error
Browse files Browse the repository at this point in the history
  • Loading branch information
anth-volk committed Oct 23, 2024
1 parent 1d3f4e6 commit a7c1279
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions microdf/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a7c1279

Please sign in to comment.