在 Python 中从当前时间到当前时间的(某些)分钟之前获取时间值列表 [英] Get a List of Time Values before a (certain) Minutes from the Current time upto the Current Time in Python

查看:64
本文介绍了在 Python 中从当前时间到当前时间的(某些)分钟之前获取时间值列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要定义一个函数来获取从当前时间之前的 M" 分钟到当前时间(包括当前时间)的时间 str 值的 [列表],以及然后检查是否有任何值与给定 CSV 文件的时间列中的时间值匹配.

I need to define a function to get a [list] of str values of time from "M" minutes before the current time, up to the current time (including current time), and then check whether any of the values match the time value in the Time column of a given CSV file.

我想到了 for 循环 的想法,但不知道如何添加和附加 M 号的列表.次(绝对初学者的素质).因此,我使用以下仅支持 M = 1 的代码进行了调整:

I thought of the idea for a for loop but could not figure out how to add and append the list for M no. of times (Quality of an Absolute Beginner). So, I adjusted with the following code which only supports M = 1 :

def Time_Check_Min(M= 1):
    #Check the current system time
    timestr = datetime.now()
    Check_TIMEnow = timestr.strftime("%H:%M")

    #Add 'M' min to Current Time
    Ahead_Time = (timestr + timedelta(minutes=M))
    Check_TIME = Ahead_Time.strftime("%H:%M")

    #Check if the current time is mentioned in the Dataframe
    if Check_TIME in df_.Time.values or Check_TIMEnow in df_.Time.values:
        return True
    else:
        return False

我需要输出为 ('%H:%M') 格式的列表,以便检查 CSV 中是否存在其中任何一个.例如,假设当前系统时间为 '16:50' 且 M = 3 ,则列表应包含 4 个元素,例如:

I require the output as a list in ('%H:%M') format so as to then check if any of them is present in the CSV. For Example, Taking the current system time to be '16:50' and M = 3 , then the list should contain 4 elements, like :

['16:47', '16:48', '16:49', '16:50']

此外,由于我使用的是pandas,因此我想到了使用时间间隔 方法.但同样,我不知道这是否真的有帮助.

Also, I thought of using the between time method since I am using pandas. But again, I do not know if this would really help.

我需要改变我的方法吗?如果是,那么最好的方法是什么……如果不是,如何获得那个该死的名单?

Do I need to change my approach? If Yes, then which would be the best way to do so... and if not, HOW to get that damn list?

                    !! Thanks for your Time for these Time_Values !!   

推荐答案

当您使用 Pandas 时,我们可以使用 Pandas .date_range 函数和一些方便的列表切片来做到这一点.

as you're using pandas we can do this with a pandas .date_range function and some handy list slicing.

from typing import Optional
import pandas as pd

def get_time_delta_range(time_value : str, M : Optional[int] = 1) -> list:
    t_range = pd.date_range(
                 '01 Jan 2020', '02 Jan 2020',freq='min')\
                 .strftime('%H:%M').tolist()

    idx = t_range.index(time_value)
    return t_range[idx -M : idx + 1]


vals = get_time_delta_range('16:50', M=3)
print(vals)

['16:47', '16:48', '16:49', '16:50']

然后使用 isin 过滤您的列表.

then use isin to filter your list.

df_['Time'].isin(vals)


编辑.

def get_time_delta_range(dataframe : pd.DataFrame,
                         time_value : str, M : Optional[int] = 1) -> bool:
    t_range = pd.date_range(
                 '01 Jan 2020', '02 Jan 2020',freq='min')\
                 .strftime('%H:%M').tolist()

    idx = t_range.index(time_value)
    t_range_slice = t_range[idx -M : idx + 1]
    return dataframe.isin(t_range_slice).sum().astype(bool)[0]


df = pd.DataFrame({'time' : ['16:04','16:05']})
get_time_delta_range(df,'16:04')
True

get_time_delta_range(df,'16:09')
False

这篇关于在 Python 中从当前时间到当前时间的(某些)分钟之前获取时间值列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆