Guido
Guido

Reputation: 688

Modify timestamp subset column in pandas

I have a Dataframe with a timestamp column:

tij_pd.datetime[0:5]
Out[29]: 
0   2016-01-09 05:27:00
1   2016-01-09 06:49:00
2   2016-01-09 08:05:00
3   2016-01-09 12:09:00
4   2016-01-09 14:54:00
Name: datetime, dtype: datetime64[ns]

I need to select times between '00:00' and '04:00' and add 1Hour.

In[31]:   tij_pd.set_index('datetime').between_time('00:00','04:00').reset_index().datetime
Out[31]: 
0    2016-03-09 01:01:00
1    2016-10-09 00:31:00
...
16   2016-03-09 01:40:00
17   2016-09-23 00:46:00
Name: datetime, dtype: datetime64[ns]

How can I add 1Hour to the datetime column of this subset?

tij_pd['datetime'] = tij_pd['datetime']+pd.to_timedelta(1,'d')

Upvotes: 0

Views: 203

Answers (1)

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210832

IIUC:

tij_pd.loc[(tij_pd.datetime.dt.hour >= 0) & 
           (tij_pd.datetime.dt.hour <= 4), 
          'datetime'] += \
    pd.to_timedelta('1H')

Upvotes: 1

Related Questions