Home > database >  How to get the index of a Pandas series
How to get the index of a Pandas series

Time:06-06

s is of type Series:

print(type(s))

<class 'pandas.core.series.Series'>

print(s)

Date
2022-06-03    145.380005
Name: Close, dtype: float64

I am iterating over a rolling calculation:

resampleStr = '180D'
dfRollSeries = df['Close'].rolling(resampleStr)
for s in dfRollSeries:

if I say

s.iat[0]

I get the Close

If I say

s.index

I get

<class 'pandas.core.indexes.datetimes.DatetimeIndex'>

How do I get the value of the index? I need the Date part, 2022-06-03?

CodePudding user response:

the reset_index() method (docs) should do the trick, it will return the index of the series or dataframe.

s.reset_index()['Date']

CodePudding user response:

this will get you the value of the index, i.e., the date

s.index[0]
  • Related