dvreed77
dvreed77

Reputation: 2387

Operations on Pandas DataFrame Index

How can I easily perform an operation on a Pandas DataFrame Index? Lets say I create a DataFrame like so:

df = DataFrame(rand(5,3), index=[0, 1, 2, 4, 5])

and I want to find the mean sampling rate. The way I do this now doesn't seem quite right.

fs = 1./np.mean(np.diff(df.index.values.astype(np.float)))

I feel like there must be a better way to do this, but I can't figure it out.

Thanks for any help.

Upvotes: 1

Views: 5038

Answers (1)

Jeff
Jeff

Reputation: 129018

@BrenBarn is correct, better to make a column in a frame, but you can do this

In [2]: df = DataFrame(np.random.rand(5,3), index=[0, 1, 2, 4, 5])

In [3]: df.index.to_series()
Out[3]: 
0    0
1    1
2    2
4    4
5    5
dtype: int64

In [4]: s = df.index.to_series()

In [5]: 1./s.diff().mean()
Out[5]: 0.80000000000000004

Upvotes: 2

Related Questions