现在从没有星期六和星期日的数据框列中减去日期时间
Subtract datetime now from a dataframe column without Saturday and Sunday
目前我的脚本正在用我在名为“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
,您可以通过
轻松转换为 dtype
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
还允许您设置自定义周掩码(不包括周六和周日以外的日子)或假期列表。请参阅我在顶部链接的文档。
相关: ,
目前我的脚本正在用我在名为“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
还允许您设置自定义周掩码(不包括周六和周日以外的日子)或假期列表。请参阅我在顶部链接的文档。
相关: