I’ve worked a lot with time-indexed data, and when it comes to time series, pre-structuring your DataFrame makes life easier and keeps performance steady. I usually go with this setup when I want to pandas create empty dataframe and fill it row by row:
import pandas as pd
dates = pd.date_range(start="2024-01-01", periods=10)
df = pd.DataFrame(index=dates, columns=["A", "B", "C"])
df = df.fillna(0) # or use np.nan if you prefer
for i in range(1, len(df)):
today = df.index[i]
yesterday = df.index[i - 1]
df.loc[today, 'A'] = df.loc[yesterday, 'A'] + 1
This avoids the cost of dynamically growing the DataFrame inside a loop—something pandas doesn’t handle very efficiently. Pre-indexing keeps your temporal logic tight and predictable.