Home > Blockchain >  Add hours to time series
Add hours to time series

Time:02-17

I've got this following time series[1] which have the timezone in UTC, since my timezone is GMT 1 i would like to add 1 hours to all this time. I try different solutions (like convert it to datetime and add a timedelta) but none of that solutions works. How can i solve this problem, I've been working on it all day but can't find a working solution.

[1]:

import pandas as pd


df = pd.read_csv( "streaming_history_New1.csv", encoding = "ISO-8859-1", sep = ';') 

dfNew = pd.DataFrame(df.end_time.str.split(' ',1).tolist(),columns = ['date','time']

print(dfNew['time'])

0       15:49
1       15:53
2       15:54
3       16:41
4       16:54
 ....
2862    19:16
2863    19:19
2864    19:22
2865    19:24
2866    19:27
Name: time, Length: 2867, dtype: object

CodePudding user response:

You should first cast the column time to a datetime object and then perform pd.Timedelta increment.

Try this:

from datetime import datetime

d['time_p'] = (d['time'].apply(lambda x: datetime.strptime(x, "%H:%M"))   pd.Timedelta("1 hour")).apply(lambda y: datetime.strftime(y, "%H:%M"))

Column time_p is your expected time column incremented by 1 hour

CodePudding user response:

I believe it should just work like

series   pd.Timedelta('1 hour')

where series is your pandas series data

  • Related