79099708

Date: 2024-10-17 20:19:19
Score: 1
Natty:
Report link

You can also use the standard Series.between function along with the time object from the datetime library. Piggybacking off of @unutbu's example:

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

N = 100
df = pd.DataFrame(
    {
        "date": pd.date_range("2000-1-1", periods=N, freq="H"),
        "value": np.random.random(N),
    }
)
df.loc[df["date"].dt.time.between(time(20, 0), time(21, 0))]

In this way you can still use the "inclusive" parameter and you don't have to mess with indexes.

Reasons:
  • Has code block (-0.5):
  • User mentioned (1): @unutbu's
  • Low reputation (0.5):
Posted by: Ruben