I have a Pandas DataFrame from a csv file which indexes are Dates.
df = pd.read_csv('data.csv', index_col=0, parse_dates=True)
df.index
DatetimeIndex(['2010-01-01', '2010-01-04', '2010-01-05', '2010-01-06',
'2010-01-07', '2010-01-08', '2010-01-11', '2010-01-12',
'2010-01-13', '2010-01-14',
...
'2018-06-18', '2018-06-19', '2018-06-20', '2018-06-21',
'2018-06-22', '2018-06-25', '2018-06-26', '2018-06-27',
'2018-06-28', '2018-06-29'],
dtype='datetime64[ns]', name='Date', length=2216, freq=None)
I need to calculate values according to the index numbers for each row, but df.index
returns DatetimeIndex
. How could I get the raw index number series for each row?
Expect:
df.raw_index # return a Series [0, 1, 2, 3, ...]
df['result'] = (df.raw_index + 1) ** 2 ## [1, 4, 9, 16, ...]
I can use pd.Series(range(0, df.shape[0]))
to create a Series by a range, but I think it is not efficient.