i have a range:
start = 87.5
end = 107.5
in between start and end i have allocations that look like this
if visualized it looks like this:
now i have another input as X = 0.3
0.3 is actually a value that can be allocated inside the range.
my question is how do i solve this in dataframe-pandas and have the most optimal solution.
by optimal solution i mean the new allocation doesn't create more whitespaces hence decreasing the continuity. inshort i want to increase the congestion in the range so i can place more X's in the range.
if default allocations are:
lower upper
87.85 88.15
89.75 90.05
91.45 91.75
96.05 96.35
96.85 97.15
98.85 99.15
100.85 101.15
101.9 102.1
102.9 103.1
105.35 105.65
107.81 107.99
then one of the many solutions can be(considering x= 0.3:
lower upper
87.85 88.15
88.55 88.85
89.75 90.05
91.45 91.75
96.05 96.35
96.85 97.15
98.85 99.15
100.85 101.15
101.9 102.1
102.9 103.1
105.35 105.65
107.81 107.99
IIUC, you can compute the size of the empty ranges, find the smallest that is larger than your target:
X = 0.3
diff = df['lower'].sub(df['upper'].shift()).sort_values()
n = np.searchsorted(diff, X)
idx = diff.index[n]
a = df.loc[idx-1, "upper"]
b = df.loc[idx, "lower"]
delta = (b-a)-X//3
print(f'insert {X} between {a} and {b}')
print(f'potential range: {a+delta}-{b-delta}')
Output:
insert 0.3 between 96.35 and 96.85
potential range: 95.05-92.75