简体   繁体   English

如何在Python中获取N天前的营业日期

[英]How to get the business date N days ago in Python

I am trying to write some code that will let me know the date n business days ago (excluding weekend).我正在尝试编写一些代码,让我知道 n 个工作日前的日期(不包括周末)。 I have the following code that will find the date including weekends but can find a way to return a date that was n business days ago.我有以下代码可以找到包括周末在内的日期,但可以找到一种方法来返回 n 个工作日前的日期。

In the example below I return the date (including weekend) n days ago, but I need the date n business days ago.在下面的示例中,我返回 n 天前的日期(包括周末),但我需要 n 个工作日前的日期。

from datetime import datetime, timedelta

N = 2

date_N_days_ago = datetime.now() - timedelta(days=N)

print datetime.now()
print date_N_days_ago

you need numpy busday offset function.您需要 numpy busday 偏移功能。 The doc is pretty self explanatory.该文档是不言自明的。

https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.busday_offset.html https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.busday_offset.html

You can use pandas as well.您也可以使用熊猫。 Pandas is a good data analysis library. Pandas 是一个很好的数据分析库。

http://pandas.pydata.org/pandas-docs/stable/timeseries.html#custom-business-days-experimental http://pandas.pydata.org/pandas-docs/stable/timeseries.html#custom-business-days-experimental

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM