Home > Enterprise >  How can I check to see if a range of timestamps are in a specific interval?
How can I check to see if a range of timestamps are in a specific interval?

Time:12-22

Is there a way to return a true or false if a range of timestamps are in a specific interval?

Here's the code I'm using, these timestamps are in 5 minute intervals. So I would expect True to be printed.

I'm pretty sure that range can't be applied to a dataframe, so it would need to be converted to a series. Getting stuck though.

d = {'time': ['1/1/2021 00:00:00',
              '1/1/2021 00:05:00',
              '1/1/2021 00:10:00',
              '1/1/2021 00:15:00']}
df = pd.DataFrame(d)
start_date_inside = df['time'].min()
end_date_inside = df['time'].max()
s = pd.Series(df)

for x in s():
    if x(range(start_date_inside, end_date_inside, 5)):
        print('True')
    else:
        print("false")

This generates this error: ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

CodePudding user response:

d = {'time': ['1/1/2021 00:00:00',
              '1/1/2021 00:05:00',
              '1/1/2021 00:10:00',
              '1/1/2021 00:15:00']}

df = pd.DataFrame(d)

# convert to datetime
df['time'] = pd.to_datetime(df['time'])
# calculate the difference and check to see if the difference is five minutes
df['time'].diff() == pd.Timedelta(minutes=5)

0    False
1     True
2     True
3     True
Name: time, dtype: bool

or you can check if all values are exactly five minute intervals from the previous row

all((df['time'].diff() == pd.Timedelta(minutes=5)).iloc[1:]) # -> True

CodePudding user response:

import pandas as pd


d = {'time': ['1/1/2021 00:00:00',
              '1/1/2021 00:05:00',
              '1/1/2021 00:10:00',
              '1/1/2021 00:15:00']}

df = pd.DataFrame(d)

# Convert the timestamps to datetime objects
df['time'] = pd.to_datetime(df['time'])

start_date = '1/1/2021 00:00:00'
end_date = '1/1/2021 00:15:00'

# Convert the start and end dates to datetime objects
start_date = pd.to_datetime(start_date)
end_date = pd.to_datetime(end_date)

# Use the 'between' method to check if the timestamps are within the specified range
df['in_range'] = df['time'].between(start_date, end_date)

print(df)

Output:

                 time  in_range
0 2021-01-01 00:00:00      True
1 2021-01-01 00:05:00      True
2 2021-01-01 00:10:00      True
3 2021-01-01 00:15:00      True
  • Related