Using pd.interpolate since your data is a pd series:
pd.interpolate fills NaN values using interpolation between the adjacient numeric values
- the distance between the adjacient values and the number of NaNs in between sets the interpolation step
- To get the requested interpolation step individually in between each given values pair a new series is population with the reqired_number_of_increments as NaNs
- different number of NaNs in between each given values pair according their distance
round to get 'close to the increment step' with the required_number_of_increments
Code:
import pandas as pd
import numpy as np
pds = pd.Series([19.280, 48.380, 51.240, 58.603, 60.380, 203.300], dtype='float64')
pds_filled = pd.Series(dtype='float64')
step_value = 4
for i in range(pds.size):
pds_filled = pd.concat([pds_filled, pd.Series(pds[i], dtype='float64')],
ignore_index = True)
# Note pd.append is deprecated
if i == len(pds)-1:
break # break after concat of the last element
no_inserts = round(((pds.shift(-1)[i] - pds[i])) / step_value ) - 1
# print(f"i= {i}, no_inserts= {no_inserts}")
for j in (range(0,no_inserts)): # not executed when no_insterts = 0
pds_filled = pd.concat([pds_filled, pd.Series(np.NaN, dtype='float64')],
ignore_index = True)
# print(pds_filled)
# print(pds_filled) # check the filled NaNs
pds_filled.interpolate(inplace=True)
# pd.interpolate() replaces NaNs with interpolated values
print(pds_filled) # final pd.series!
## print options
# print(pds_filled.tolist())
# print([f'{item:.3f}' for item in pds_filled.tolist()])
Result list:
['19.280', '23.437', '27.594', '31.751', '35.909', '40.066', '44.223', '48.380', '51.240', '54.922', '58.603', '60.380', '64.350', '68.320', '72.290', '76.260', '80.230', '84.200', '88.170', '92.140', '96.110', '100.080', '104.050', '108.020', '111.990', '115.960', '119.930', '123.900', '127.870', '131.840', '135.810', '139.780', '143.750', '147.720', '151.690', '155.660', '159.630', '163.600', '167.570', '171.540', '175.510', '179.480', '183.450', '187.420', '191.390', '195.360', '199.330', '203.300']
Notes:
no_insetrs calculation sets the steps, you can fine tune that to adapt the step.
- Activate the commented print options in the code to cross check the interims steps and list output instead of pd series