I have a data frame that looks as follows:
df = pd.DataFrame({"A":[10,0,30,40,0,60,70,80,90]}, index = pd.date_range(start='1/1/2020', end='1/09/2020'))
df
A
2020-01-01 10
2020-01-02 0
2020-01-03 30
2020-01-04 40
2020-01-05 0
2020-01-06 60
2020-01-07 70
2020-01-08 80
2020-01-09 90
I want to loop over the data frame to get t0 and t1 which represent the first and last dates, respectively (i.e. index value) for each range of non-zero values of A.
In the table above, I want to get the following values for t0 and t1:
t0 = 2020-01-01 , t1 = 2020-01-01
t0 = 2020-01-03 , t1 = 2020-01-04
t0 = 2020-01-06 , t1 = 2020-01-09
Is there a simple way to do this within Pandas?