Python: Adding hours to pandas timestamp

I think you can add to column TDate column Hour converted to_timedelta with unit=”h”: df = pd.DataFrame({‘TDate’:[‘2005-01-03′,’2005-01-04′,’2005-01-05’], ‘Hour’:[4,5,6]}) df[‘TDate’] = pd.to_datetime(df.TDate) print (df) Hour TDate 0 4 2005-01-03 1 5 2005-01-04 2 6 2005-01-05 df[‘TDate’] += pd.to_timedelta(df.Hour, unit=”h”) print (df) Hour TDate 0 4 2005-01-03 04:00:00 1 5 2005-01-04 05:00:00 2 6 2005-01-05 06:00:00

How do I get time from a datetime.timedelta object?

It’s strange that Python returns the value as a datetime.timedelta. It probably should return a datetime.time. Anyway, it looks like it’s returning the elapsed time since midnight (assuming the column in the table is 6:00 PM). In order to convert to a datetime.time, you can do the following:: value = datetime.timedelta(0, 64800) (datetime.datetime.min + value).time() … Read more

Comparing a time delta in python

You’ll have to create a new timedelta with the specified amount of time: d > timedelta(minutes=1) Or this slightly more complete script will help elaborate: import datetime from time import sleep start = datetime.datetime.now() sleep(3) stop = datetime.datetime.now() elapsed = stop – start if elapsed > datetime.timedelta(minutes=1): print “Slept for > 1 minute” if elapsed … Read more

How to add hours to current time in python

from datetime import datetime, timedelta nine_hours_from_now = datetime.now() + timedelta(hours=9) #datetime.datetime(2012, 12, 3, 23, 24, 31, 774118) And then use string formatting to get the relevant pieces: >>> ‘{:%H:%M:%S}’.format(nine_hours_from_now) ’23:24:31′ If you’re only formatting the datetime then you can use: >>> format(nine_hours_from_now, ‘%H:%M:%S’) ’23:24:31′ Or, as @eumiro has pointed out in comments – strftime

How to calculate time difference by group using pandas?

You can use sort_values with groupby and aggregating diff: df[‘diff’] = df.sort_values([‘id’,’time’]).groupby(‘id’)[‘time’].diff() print (df) id time diff 0 A 2016-11-25 16:32:17 NaT 1 A 2016-11-25 16:36:04 00:00:35 2 A 2016-11-25 16:35:29 00:03:12 3 B 2016-11-25 16:35:24 NaT 4 B 2016-11-25 16:35:46 00:00:22 If need remove rows with NaT in column diff use dropna: df = … Read more

How do you convert a datetime/timestamp from one timezone to another timezone?

If you know your origin timezone and the new timezone that you want to convert it to, it turns out to be very straightforward: Make two pytz.timezone objects, one for the current timezone and one for the new timezone e.g. pytz.timezone(“US/Pacific”). You can find a list of all official timezones in pytz library: import pytz; … Read more

Python timedelta issue with negative values

If you are using Python 2.7 or higher you can use timedelta.total_seconds() to get a float representation of the timedelta as a positive or negative number of seconds. >>> datetime.timedelta(-1, 86100).total_seconds() -300.0 You should be able to use this to calculate a number of minutes fairly easily. If you are not using Python 2.7 you … Read more