This is the code
flx['mg_smoothed'] = flx['mg']
increment_period = 14
processed = set()
for i in range(len(flx.index) - increment_period):
if i not in processed:
mg = flx.loc[i, 'mg']
next_mg = flx.loc[i+1, 'mg']
if mg < next_mg:
interpolation = np.linspace(mg, next_mg, increment_period)
indices = np.arange(i, i + increment_period)
flx.loc[indices, 'mg_smoothed'] = interpolation
processed.update(indices)
else:
flx.loc[i, 'mg_smoothed'] = mg
processed.add(i)
sns.lineplot(flx, x='date', y='mg_smoothed')
flx is a dataframe with a continuous date column by day and mg as a value. I’m trying to make it ramp up and down when there’s a value change to make the change gradual. At the moment I’m just trying to get upward to work.
I’ve added print statements to check after the flx.loc[indices] line and it works there, but somehow when it comes out mg_smoothed is just equal to mg and I’m so lost…
The processed check is also working fine and indices are not being repeated over.
Here is code for generating the table for testing:
flx = pd.DataFrame(columns=['date', 'mg'])
flx['date'] = pd.date_range(drinks['date'].min(), drinks['date'].max())
periods = [
(pd.date_range('2023-05-22', '2023-11-16'), 20),
(pd.date_range('2024-03-22', '2024-05-26'), 20),
(pd.date_range('2024-05-27', '2024-07-05'), 10),
(pd.date_range('2024-07-06', '2024-07-19'), 5),
(pd.date_range('2024-07-20', '2024-07-26'), 3.33)
]
def fill_table(x):
for period in periods:
dates = period[0]
mg = period[1]
if x in dates:
return mg
flx['mg'] = flx['date'].apply(fill_table).fillna(0)
Hersillon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.