0

how do I convert string to timedelta in order to create a new column within my dataframe?

from  pandas as pd
from numpy as np
from datetime import timedelta

pricetime = pd.DataFrame({'price1':[22.34, 44.68, 52.98], 'time1':['9:48:14', '15:54:33', '13:13:22'],'price2':[28.88, 47.68, 22.32], 'time2':['10:52:44', '15:59:59', '10:12:22']})

pricetime['price_change'] = np.where(pricetime['time1'] < pricetime['time2'], (pricetime['price1'] - pricetime['price2'])/pricetime['price2'], np.nan)
pricetime['time_diff'] = np.where(pricetime['time1'] < pricetime['time2'], pricetime['time2'] - pricetime['time1'], np.nan)

When I do this. I get an error for the time where I'm subtracting the two different times.

I tried to do this but it gave me an error:

pricetime['price_change'] = np.where((datetime.strptime(pricetime['time1'], '%H:%M:%S') < datetime.strptime(pricetime['time2'], '%H:%M:%S')), (pricetime['price1'] - pricetime['price2'])/pricetime['price2'], np.nan)
pricetime['time_diff'] = np.where((datetime.strptime(pricetime['time1'], '%H:%M:%S') < datetime.strptime(pricetime['time2'], '%H:%M:%S'), datetime.strptime(pricetime['time2'], '%H:%M:%S') - datetime.strptime(pricetime['time1'], '%H:%M:%S'), np.nan)

The error it gave is:

TypeError: strptime() argument 1 must be str, not Series
8
  • datetime.strptime work on one string no a pandas series. You need to either use list comprehension or pd.apply to convert the series from strings to datetimes. Commented Jul 14, 2022 at 21:23
  • what is your final goal? what do you expect the output to be? Commented Jul 14, 2022 at 21:31
  • Just a difference in time between time1 and time2. So for the first row time1 = 9:48:14 and time2 = 10:52:44. I want a new column to give me the difference between the two time. 1:04:30 Commented Jul 14, 2022 at 21:36
  • And also anything time related. If time2 > time1 then I want the difference between the prices and put it in as a new column. Commented Jul 14, 2022 at 21:37
  • use pd.to_datetime(pricetime['time1']) to get the times. so time diffs are: pd.to_datetime(pricetime['time1']) - pd.to_datetime(pricetime['time2']) if you can provide an example of an output I could write a function... Commented Jul 14, 2022 at 21:40

2 Answers 2

2

after a discussion with @Marc_Law the answer he looked for is:

pricetime['time_diff'] = pd.to_datetime(pricetime['time2']) - pd.to_datetime(pricetime['time1'])
pricetime.loc[pd.to_datetime(pricetime['time1']) >= pd.to_datetime(pricetime['time2']),'time_diff'] = np.nan
pricetime['time_diff'] = pricetime['time_diff'].apply(lambda x: str(x).split(' ')[-1:][0])

what he needed is to have the difference only if the value in time1 column was smaller than the value in time2 column, otherwise put np.nan. than return it to string without the "X days".

Sign up to request clarification or add additional context in comments.

Comments

0

If you only want to find the difference in time, then you can follow this sample

from datetime import datetime


foo = '9:48:14'
bar = '15:54:33'

foo = datetime.strptime(foo, '%H:%M:%S')
bar = datetime.strptime(bar, '%H:%M:%S')

print(bar - foo)

Output

6:06:19

Further reading

3 Comments

I tried to do that but it gave me an error. Do you know what I did wrong? I'm still new to Python.
@Marc_Law You are passing a whole column, you need to iterate over the rows and pass each of them when comparing
pd.to_datetime() is more suitable for this question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.