目前,我的脚本正在用我在名为“Creation”的 Dataframe 列中的时间减去我的当前时间,生成一个包含差异天数的新列。我得到了这段代码的不同天数:
df['Creation']= pandas.to_datetime(df["Creation"],dayfirst="True")
#Generates new column with the days.
df['Difference'] = df.to_datetime('now') - df['Creation']
我现在想要的是让它给我像他给我的日子,但不要计算周六和周日。我该怎么做?
您可以使用numpy
的busday_count,例如:
import pandas as pd
import numpy as np
# some dummy data
df = pd.DataFrame({'Creation': ['2021-03-29', '2021-03-30']})
# make sure we have datetime
df['Creation'] = pd.to_datetime(df['Creation'])
# set now to a fixed date
now = pd.Timestamp('2021-04-05')
# difference in business days, excluding weekends
# need to cast to datetime64[D] dtype so that np.busday_count works
df['busday_diff'] = np.busday_count(df['Creation'].values.astype('datetime64[D]'),
np.repeat(now, df['Creation'].size).astype('datetime64[D]'))
df['busday_diff'] # since I didn't define holidays, potential Easter holiday is excluded:
0 5
1 4
Name: busday_diff, dtype: int64
如果您需要输出为 dtype timedelta
,您可以通过
df['busday_diff'] = pd.to_timedelta(df['busday_diff'], unit='d')
df['busday_diff']
0 5 days
1 4 days
Name: busday_diff, dtype: timedelta64[ns]
注意: np.busday_count
还允许您设置自定义周掩码(排除周六和周日以外的天数)或假期列表。请参阅我在顶部链接的文档。
相关: 在python中计算不包括周末的两个日期之间的差异?,如何将 (np.busday_count) 与 pandas.core.series.Series 一起使用
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句