Home > Mobile >  How to add timedelta on a subset of a dataframe
How to add timedelta on a subset of a dataframe

Time:11-17

I am trying to add a timedelta of 1 hour to a subset of my dataframe. I use df['2022-06-01 02:00:00':'2022-06-01 04:00:00'] to slice it and add pd.Timedelta(hours=1) but I get an error.

I want to add a timedelta only on `2022-06-01 02:00:00':'2022-06-01 04:00:00'. How can I achieve that? Solution can be either as datetime as index or as column.

This is the datetime in the dataframe:

2022-06-01 00:30:00, 
2022-06-01 01:00:00,
2022-06-01 01:30:00, 
2022-06-01 02:00:00,
2022-06-01 02:30:00, 
2022-06-01 03:00:00,
2022-06-01 03:30:00,
2022-06-01 04:00:00,
2022-06-01 04:30:00,
2022-06-01 05:00:00,
2022-06-01 05:30:00,
2022-06-01 06:00:00,
2022-11-16 06:30:00,
2022-11-16 07:00:00,
2022-11-16 07:30:00, 
2022-11-16 08:00:00

CodePudding user response:

Solutions for DatetimeIndex:

You can create mask and rename values of index by Index.where:

mask = (df.index >= '2022-06-01 02:00:00') & (df.index <= '2022-06-01 04:00:00')
df.index = df.index.where(~mask, df.index   pd.Timedelta(hours=1))

Or get indices and use DataFrame.rename by dictionary:

i = df['2022-06-01 02:00:00':'2022-06-01 04:00:00'].index

df = df.rename(dict(zip(i, i   pd.Timedelta(hours=1))))

Solutions for date column:

Use Series.between for boolean mask and DataFrame.loc for set new values:

mask = df['date'].between('2022-06-01 02:00:00','2022-06-01 04:00:00')

df.loc[mask, 'date']  = pd.Timedelta(hours=1)

Or Series.mask:

df['date'] = df['date'].mask(mask, df['date']   pd.Timedelta(hours=1))
  • Related