我正在尝试使用 Pandas 来计算经过的业务秒数。我在 Pandas 数据框中有一列,它在纽约时区有一堆时间戳。这是我到目前为止的代码:
import pandas as pd
import datetime
times = pd.DataFrame([datetime.datetime.now(timezone('America/New_York')),datetime.datetime.now(timezone('America/New_York'))],columns=['timestamp'])
time.sleep(2)
times['difference'] = (datetime.datetime.now(timezone('America/New_York')) - times)
times['difference'] = times['difference'].dt.seconds
Run Code Online (Sandbox Code Playgroud)
这按预期工作,并在“差异”列中给出答案为 2。但现在我只想包括营业时间(比如上午 9 点到下午 5 点)。所以昨天下午 5 点到今天早上 9 点之间的输出为零。我已经阅读了关于时间偏移的 Pandas 文档并寻找了类似的问题,但没有找到任何有效的例子。
我有一个按计划每分钟运行一次的函数。第一步是从 API 中提取数据。有时这有效,有时它超时。如果它有效,我想对数据做一堆事情然后保存它。如果它不起作用,我只想跳到函数的末尾而不做任何事情。所以这是它的工作原理:
def job():
try:
a = requests.get("API address that sometimes work and sometimes doesn't")
except:
print('connection error')
#a bunch of code that transforms the data and then saves it
Run Code Online (Sandbox Code Playgroud)
这是调度程序:
schedule.every().minute.at(":01").do(job)
while 1:
schedule.run_pending()
time.sleep(1)
Run Code Online (Sandbox Code Playgroud)
我可以通过将#a bunch of code线移到 try 中来实现我想要的,但是其他潜在的错误也将被捕获在“连接错误”中。
有没有办法让异常跳到函数的末尾?然后因为它在一分钟的调度程序上它只是稍后再试?
我知道它不是可重复的代码,但这很简单,没有必要。