-1

Lets say I have a dataframe:

1027   2019-01-01 07:17:00
479    2019-01-01 07:10:00
480    2019-01-01 06:10:00

and I have a variable:

x=datetime.time(8,0)

How I can get the difference (in minutes) between each row and x?

I've tried:

row['date'].time() - x

But got:

TypeError: unsupported operand type(s) for -: 'datetime.time' and 'datetime.time'

enter image description here

Taazar
  • 1,545
  • 18
  • 27
  • It this a typo: should `miniute=0` be `minute=0` ? – SherylHohman Jan 01 '20 at 17:55
  • 1
    yes you are right there is a type here but not in the code. – Tamir Shwartz Jan 01 '20 at 17:56
  • Please show your timedelta and substraction code, as well as the exact error message. Also, did you convert `datetime` or `timestamp` to be the same type before calculations? – SherylHohman Jan 01 '20 at 18:01
  • ive converted the rows in the df to: the row['date'].time() so now both are datetime.time (hour, minute) – Tamir Shwartz Jan 01 '20 at 18:28
  • Please include the code you tried that did not work, and include the "errors" (messages) it produced. – SherylHohman Jan 01 '20 at 18:31
  • Welcome to SO. Most questions require a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example), so please review this topic, and include all pertinent info we might need to help solve your issue. Please also correct punctuation/capitalization/grammar/typos in your posts, to keep the quality of SO high, and so other volunteers do not have to do it for you. Proper grammar shows attention to detail, respect, and volunteers are more likely to their time to *your post* when it is well formed. Some posting guidelines: https://stackoverflow.com/help/how-to-ask. – SherylHohman Jan 01 '20 at 18:51
  • Is this post helpful? https://stackoverflow.com/questions/13703720/converting-between-datetime-timestamp-and-datetime64 – SherylHohman Jan 01 '20 at 19:27

2 Answers2

1

time doesn't support subtractions, use datetime instead

In [14]: x = datetime(2019, 1, 1, 8, 00, 00)

In [15]: y = datetime(2019, 1, 1, 7, 17, 00)

In [16]: x
Out[16]: datetime.datetime(2019, 1, 1, 8, 0)

In [17]: y
Out[17]: datetime.datetime(2019, 1, 1, 7, 17)

In [18]: x - y
Out[18]: datetime.timedelta(seconds=2580)
Jason
  • 11,263
  • 21
  • 87
  • 181
  • thanks but i can't do that. Y is date value from a data frame so I cannot put any date in x and i can only change Y into Y.time() and then i get (7,17) – Tamir Shwartz Jan 01 '20 at 18:04
  • 2
    then it would be helpful to include that information into your original question. You don't specify anything about pandas or data frames in your question, which means I thought you were working with straight Python. – Jason Jan 01 '20 at 18:07
1

Using df['date'].dt.minute will return a value of the minutes (type integer), then you can subtract your variable from that value.

x = 8
df['date'].dt.minute - x
0     9
1     2
2     2
Name: date, dtype: int64

I think the solution I presented above is what you asked for, however, I think you might want to investigate whether pandas.to_timedelta() is more helpful for you.!

Breen
  • 11
  • 4
  • Please `edit` to include your final suggested code (the "substraction" line you suggest), to make this a complete answer. Thanks, and Welcome to SO. Your contributions are appreciated :-) – SherylHohman Jan 01 '20 at 19:55
  • I embedded the image, but you need to put code in text/code format on SO, as it isn't as easy to read code in image form (esp on mobile devices), nor can it be copy-pasted into editors. Lines indented by 4 spaces are converted to "code" formatting - see sidebar for additional MarkDown hints. Please review [Help](https://stackoverflow.com/help) for your next posts: [How to Post](https://stackoverflow.com/help/how-to-ask), [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example), and [more](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/). – SherylHohman Jan 01 '20 at 21:35
  • 1
    Thanks, @SherylHohman . Since the code runs and displays the same as my screenshot, I've removed my screenshot. – Breen Jan 01 '20 at 22:18