【问题标题】:How can I return a Pandas DataFrame, if the row is between certain weekday and time?如果该行在某个工作日和时间之间,我如何返回 Pandas DataFrame?
【发布时间】:2022-01-25 12:09:51
【问题描述】:

我正在尝试制作一个仅在 CME 比特币期货开盘和收盘期间运行的交易机器人。周日至周五,从下午 5 点开始。到下午 4 点中部时间 (CT)。但是,我希望机器人从下午 5 点开始运行。周五至下午 4 点星期日。我希望机器人在此期间一直运行,即使是在下午 5 点以外。和下午 4 点仅当在周六和周日。我希望我的措辞可以理解。

我的数据框有一个名为“时间”的列,并且是 unix 时间戳。我已经设法对其进行了转换,但似乎无法找到一种方法来返回介于这些时间段之间的数据帧。

import pandas as pd
import datetime as dt

def is_cme_time(df):
    # convert unix time stamp to datetime
    df['time'] = df['time'].astype(int)
    df['time'] = df['time'].apply(lambda x: dt.datetime.utcfromtimestamp(x))

    # check if a time is during the cme bitcoin futures contract open and close
    cme_open = None
    cme_close = None


    return df[(df['time'] > cme_open) & (df['time'] < cme_close)]

【问题讨论】:

  • 转换为日期时间,最好使用df['time'] = pd.to_datetime(df['time'], unit='s')

标签: python pandas datetime


【解决方案1】:

使用下面的代码:

# Initialize your min_time and max_time values
df[df['time'] > min_time and df['time'] < max_time]

【讨论】:

  • 您能否在回答中提供更多详细信息?
猜你喜欢
  • 2022-12-11
  • 2017-11-16
  • 1970-01-01
  • 2023-02-02
  • 2020-01-08
  • 2021-04-16
  • 2018-10-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多