0

Would anyone know how to calculate the time delta of the time stamp of the index?

import pandas as pd
import numpy as np

# simulate some data
# ===================================
np.random.seed(0)
dt_rng = pd.date_range('2015-03-02 00:00:00', '2015-07-19 23:00:00', freq='T')
dt_idx = pd.DatetimeIndex(np.random.choice(dt_rng, size=2000, replace=False))
df = pd.DataFrame(np.random.randn(2000), index=dt_idx, columns=['col']).sort_index()

df

Am I on track using df['elapsed_time'] = pd.TimedeltaIndex(df) at all with this?

This will throw an error: ValueError: Wrong number of items passed 2000, placement implies 1

bbartling
  • 3,288
  • 9
  • 43
  • 88
  • You can't pass a DataFrame. Only a Series. `df['elapsed_time'] = pd.TimedeltaIndex(df.index)` – Henry Ecker Nov 06 '21 at 18:41
  • It's a bit unclear what you're trying to do though because this will just take the underlying ns datetime and _force_ it into a time delta. It won't do any timedelta comparison. – Henry Ecker Nov 06 '21 at 18:42
  • It seems like you're likely looking for `df['elapsed_time'] = df.index.to_series().diff()` [Difference pandas.DateTimeIndex without a frequency](https://stackoverflow.com/q/49277932/15497888) – Henry Ecker Nov 06 '21 at 18:43

1 Answers1

0

This answer is beautiful!

This will create another pandas column which I called time_td where then I can cast it as a timedelta64 where m stands for minutes which I am looking for.

df['time_td'] = df.index.to_series().diff().astype('timedelta64[m]')

I can then sum this time_td column with:

df.time_td.sum()

bbartling
  • 3,288
  • 9
  • 43
  • 88