我希望计算两个日期之间的工作日(不包括星期日和星期六),一个是在数据框列中,另一个是今天的当前日期,但是出现错误:
df = Main_Database['Session1_Date']
Todays_Date = np.datetime64('today', dtype='datetime64[D]')
df['Session1_Date'] = df['Session1_Date'].values.astype('datetime64[D]')
df['Days_Difference'] = np.busday_count(df['Session1_Date'], Todays_Date)
我的代码:
ALTER PROCEDURE SP_REPORT_USD
(
PER SMALLINT
)
RETURNS
(
ACCOUNT_NUMBER CHAR(21),
AMOUNT NUMERIC(15, 4)
)
AS
BEGIN
SELECT
L.ACCOUNT_NUMBER, SUM(CURRROUND(L.DEBIT,2)-CURRROUND(L.CREDIT,2))
FROM
LEDGER L
WHERE
L.LEDGER_ACCOUNT = '31621' AND L.PERIOD = :PER
GROUP BY
L.ACCOUNT_NUMBER
INTO
ACCOUNT_NUMBER, AMOUNT;
SUSPEND;
END
我对为什么这不起作用感到困惑?
答案 0 :(得分:0)
该问题可能是由于数据帧和序列仅将类似日期时间的对象作为dtype datetime64 [ns]的对象而引起的。因此这行:
df['Session1_Date'] = df['Session1_Date'].values.astype('datetime64[D]')
不会将转换存储到您要实现的.datetime64 [D]中。下面的解决方案有效。
import datetime
import pandas as pd
import numpy as np
# Create a toy data frame
dates = pd.date_range(datetime.datetime(2019, 4, 5, 0,
0), datetime.datetime(2019, 4, 20, 7, 0),freq='D')
var_1 = np.random.sample(dates.size)
df = pd.DataFrame(data={'Var_1': var_1, 'Session1_Date': dates})
df = df[['Session1_Date', 'Var_1']]
df.head()
# Calculate today's date and convert to 'M8[D]'
Todays_Date = np.datetime64('today')
# Calculate the business days
df['Days_Difference'] = np.busday_count(df['Session1_Date'].values.astype('M8[D]'), Todays_Date)
df.head()
输出
df.head() # Original data frame
Out[89]:
Session1_Date Var_1
0 2019-04-05 0.625200
1 2019-04-06 0.482555
2 2019-04-07 0.701814
3 2019-04-08 0.876485
4 2019-04-09 0.117023
df.head() # With computed business days
Out[90]:
Session1_Date Var_1 Days_Difference
0 2019-04-05 0.625200 166
1 2019-04-06 0.482555 165
2 2019-04-07 0.701814 165
3 2019-04-08 0.876485 165
4 2019-04-09 0.117023 164