Skip to content

Commit 99afe81

Browse files
committed
Add percentile scaling transformation and unit tests
1 parent 65bf9cd commit 99afe81

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

pandas/io/percentile_scaling.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import numpy as np
2+
3+
def percentile_scaling(data):
4+
data = np.array(data)
5+
min_val = np.min(data)
6+
max_val = np.max(data)
7+
if max_val == min_val:
8+
raise ValueError("Cannot scale data with identical values.")
9+
10+
scaled = 100 * (data - min_val) / (max_val - min_val)
11+
return scaled.tolist()
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import unittest
2+
from pandas.io.percentile_scaling import percentile_scaling
3+
4+
class TestPercentileScaling(unittest.TestCase):
5+
def test_scaling(self):
6+
data = [10, 20, 30, 40, 50]
7+
expected = [0.0, 25.0, 50.0, 75.0, 100.0]
8+
result = percentile_scaling(data)
9+
for r, e in zip(result, expected):
10+
self.assertAlmostEqual(r, e)
11+
12+
def test_identical_values(self):
13+
with self.assertRaises(ValueError):
14+
percentile_scaling([5, 5, 5])
15+
16+
def test_empty(self):
17+
with self.assertRaises(ValueError):
18+
percentile_scaling([])
19+
20+
if __name__ == "__main__":
21+
unittest.main()

0 commit comments

Comments
 (0)