About Pandas indices
This post aims to clarify pandas indices:
df.index
: Returns the index of the dataframedf
. Can be reassigned.df.set_index('column')
: Returns a new dataframe withcolumn
set as its index.df.reindex(idx)
: You can make a new index and call itidx
. The value of the dataframe at the index values are equal to the previous index. The resulting dataframe can be larger or smaller than the original. Returns a new dataframe, withidx
set as its index.
Let’s take an example.
import pandas as pd
beg='2018-01-01'; end='2018-06-30'
idx=pd.date_range(beg,end); values=[k[0] for k in enumerate(idx)]
df=pd.DataFrame({'values':values},index=idx)
time_shift=5; new_idx=idx+pd.Timedelta(f"{time_shift} days")
new_df=df.reindex(new_idx)