0

Suppose I have a Series with datetimes. As a simple example:

import pandas as pd

data = [
    "2023-08-14 21:56",
    "2023-08-15 23:04",
    "2023-08-16 15:43",
    "2023-08-17 12:05",
    "2023-08-18 22:19",
]
s = pd.Series(data, dtype="datetime64[ns]")

For each datetime I would like to know the difference in hours from a particular time on that day (as an example: 21:00).

So my desired result would be as follows:

0    0.93
1    2.07
2   -5.28
3   -8.92
4    1.32
dtype: float64

What would be the quickest way to accomplish this?

Xukrao
  • 8,003
  • 5
  • 26
  • 52
  • Does this answer your question? [Calculate Time Difference Between Two Pandas Columns in Hours and Minutes](https://stackoverflow.com/questions/22923775/calculate-time-difference-between-two-pandas-columns-in-hours-and-minutes) – PV8 Aug 15 '23 at 09:16
  • @PV8 No. I have only one datetime Series, not two. – Xukrao Aug 15 '23 at 09:30
  • you can apply all the answer just with a `fixed` time – PV8 Aug 15 '23 at 09:31
  • To apply this answer I would need a `todate` datetime Series with my fixed time but varying dates. How would I generate such a Series? – Xukrao Aug 15 '23 at 09:44

1 Answers1

2

By using the difference (-)

"2023-08-14 21:56"

(s - s.dt.floor('D') - specific_time) -> converts into:

0   1 days 18:56:00
1   1 days 20:04:00
2   1 days 12:43:00
3   1 days 09:05:00
4   1 days 19:19:00

and this: .dt.total_seconds() / 3600

transform into hours.


import pandas as pd

data = [
    "2023-08-14 21:56",
    "2023-08-15 23:04",
    "2023-08-16 15:43",
    "2023-08-17 12:05",
    "2023-08-18 22:19",
]

# Convert the list of strings to a datetime Series
s = pd.Series(data, dtype="datetime64[ns]")

# Define the specific time as a timedelta
specific_time = pd.to_timedelta("21:00:00")

# Calculate the time difference in hours for each datetime
time_differences = (s - s.dt.floor('D') + specific_time).dt.total_seconds() / 3600

print(time_differences)

output:

0    0.933333
1    2.066667
2   -5.283333
3    0.083333
4    1.316667
dtype: float64

function:

def calculate_time_differences(data, specific_time):
    # Convert the list of strings to a datetime Series
    s = pd.Series(data, dtype="datetime64[ns]")
    
    # Calculate the time difference in hours for each datetime
    time_differences = (s - s.dt.floor('D') - specific_time).dt.total_seconds() / 3600
    
    return time_differences
Piotr Żak
  • 2,046
  • 5
  • 18
  • 30