Skip to content

Percentile Scaling Data Transformation #61374

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions pandas/io/percentile_scaling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import numpy as np

def percentile_scaling(data):
data = np.array(data)
min_val = np.min(data)
max_val = np.max(data)
if max_val == min_val:
raise ValueError("Cannot scale data with identical values.")

scaled = 100 * (data - min_val) / (max_val - min_val)
return scaled.tolist()
21 changes: 21 additions & 0 deletions pandas/tests/io/test_percentile_scaling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import unittest
from pandas.io.percentile_scaling import percentile_scaling

class TestPercentileScaling(unittest.TestCase):
def test_scaling(self):
data = [10, 20, 30, 40, 50]
expected = [0.0, 25.0, 50.0, 75.0, 100.0]
result = percentile_scaling(data)
for r, e in zip(result, expected):
self.assertAlmostEqual(r, e)

def test_identical_values(self):
with self.assertRaises(ValueError):
percentile_scaling([5, 5, 5])

def test_empty(self):
with self.assertRaises(ValueError):
percentile_scaling([])

if __name__ == "__main__":
unittest.main()
Loading