Eu tenho a tabela
data ativo valor ajuste arquivo
17/07/2020 teste1 4,35 nan 9032800000190015197.txt
16/07/2020 teste1 nan nan 9032800000190015197.txt
15/07/2020 teste1 3,12 nan 9032800000190015197.txt
14/07/2020 teste1 nan nan 9032800000190015197.txt
13/07/2020 teste1 nan 6367.55 9032800000190015197.txt
17/07/2020 teste2 3,7 -1462.825 9032800000190015197.txt
16/07/2020 teste2 nan nan 9032800000190015197.txt
15/07/2020 teste2 nan nan 9032800000190015197.txt
14/07/2020 teste2 nan -235.825 9032800000190015197.txt
13/07/2020 teste2 nan nan 9032800000190015197.txt
17/07/2020 teste3 5 nan 9032800000190015197.txt
16/07/2020 teste3 nan nan 9032800000190015197.txt
15/07/2020 teste3 6,7 nan 9032800000190015197.txt
14/07/2020 teste3 nan nan 9032800000190015197.txt
13/07/2020 teste3 nan nan 9032800000190015197.txt
e preciso obter o seguinte resultado
date ativo valor ajuste arquivo
17/07/2020 teste1 4,35 nan 9032800000190015197.txt
16/07/2020 teste1 4,35 nan 9032800000190015197.txt
15/07/2020 teste1 3,12 nan 9032800000190015197.txt
14/07/2020 teste1 3,12 nan 9032800000190015197.txt
13/07/2020 teste1 3,12 6367.55 9032800000190015197.txt
17/07/2020 teste2 3,7 -1462.825 9032800000190015197.txt
16/07/2020 teste2 3,7 nan 9032800000190015197.txt
15/07/2020 teste2 3,7 nan 9032800000190015197.txt
14/07/2020 teste2 3,7 -235.825 9032800000190015197.txt
13/07/2020 teste2 3,7 nan 9032800000190015197.txt
17/07/2020 teste3 5 nan 9032800000190015197.txt
16/07/2020 teste3 5 nan 9032800000190015197.txt
15/07/2020 teste3 6,7 nan 9032800000190015197.txt
14/07/2020 teste3 6,7 nan 9032800000190015197.txt
13/07/2020 teste3 6,7 nan 9032800000190015197.txt
Tenho valores ausentes no campo valor e preciso preencher com os valores anteriores da mesma tabela, considerando que o valor deve ser do mesmo ativo e data mais baixo. E o valor não é o mais alto nem o mais baixo, mas sim o do dia anterior que tem um valor diferente de zero.
Não consegui uma solução que atendesse a essas condições. Alguém tem ideia de como fazer? Usando pandas.DataFrame.backfill, não consegui.
Tentei em SQL, e não consegui, fiz esse código
select a.dt_data, a.co_ativo,
nullif(a.vr_valor, (select b.vr_valor from tabela b
where b.co_ativo = a.co_ativo and b.dt_data between '2020-07-01' and '2020-07-31' and b.vr_valor is not null order by b.dt_data desc limit 1))
from tabela a
where a.dt_data between '2020-07-01' and '2020-07-31'
order by a.dt_data desc
sem exito.