有条件地更新 pandas 数据框中的日期列
Conditional update to a date column in pandas dataframe
对于给定的 table:
df = pd.DataFrame( {
'datetime': ['2015-01-01', '2015-04-01', '2015-07-01', '2015-12-01', '2015-01-01', '2015-04-01', '2015-07-01', '2015-12-01'],
})
df['datetime'] = pd.to_datetime(df['datetime'])
我想将所有属于周末的日期(所以 weekday==5 或 weekday==6)更改为之前的星期五,所以像这样:
def adjust_exp_date(x):
if x.weekday()==5:
x.weekday() -= 1
if x.weekday()==6:
x.weekday() -= 2
df['datetime'].apply(adjust_exp_date)
我能够结合使用为日期分配一天和使用 np.select() 根据日期时间列来 timedelta 7 天
df = pd.DataFrame( {
'datetime': ['2015-01-01', '2015-01-02', '2015-04-01', '2015-07-01', '2015-12-01', '2015-01-01', '2015-04-01', '2015-07-01', '2015-12-01'],
})
df['datetime'] = pd.to_datetime(df['datetime'])
df['dayOfWeek'] = df['datetime'].dt.day_name()
condition_list = [df['dayOfWeek'] == 'Friday', df['dayOfWeek'] == 'Saturday']
choice_list = [df['datetime'] - datetime.timedelta(days=7), df['datetime'] - datetime.timedelta(days=8)]
df['datetime'] = np.select(condition_list, choice_list, df['datetime'])
df
此外,我添加了一个日期 ('2015-01-02'),因为您的原始示例不包括任何星期五日期
您可以子 pd.offsets.Week
调整到最近的星期五(工作日 4)
m = df['datetime'].dt.weekday.isin([5,6])
df['adjust'] = df['datetime'].mask(m, df['datetime'] - pd.offsets.Week(weekday=4))
datetime week weekday adjust adjustweek
0 2015-01-01 Thursday 3 2015-01-01 Thursday
1 2015-01-02 Friday 4 2015-01-02 Friday
2 2015-01-03 Saturday 5 2015-01-02 Friday
3 2015-01-04 Sunday 6 2015-01-02 Friday
4 2015-01-05 Monday 0 2015-01-05 Monday
5 2015-01-06 Tuesday 1 2015-01-06 Tuesday
6 2015-01-07 Wednesday 2 2015-01-07 Wednesday
7 2015-01-08 Thursday 3 2015-01-08 Thursday
8 2015-01-09 Friday 4 2015-01-09 Friday
9 2015-01-10 Saturday 5 2015-01-09 Friday
10 2015-01-11 Sunday 6 2015-01-09 Friday
11 2015-01-12 Monday 0 2015-01-12 Monday
12 2015-01-13 Tuesday 1 2015-01-13 Tuesday
13 2015-01-14 Wednesday 2 2015-01-14 Wednesday
14 2015-01-15 Thursday 3 2015-01-15 Thursday
15 2015-01-16 Friday 4 2015-01-16 Friday
对于给定的 table:
df = pd.DataFrame( {
'datetime': ['2015-01-01', '2015-04-01', '2015-07-01', '2015-12-01', '2015-01-01', '2015-04-01', '2015-07-01', '2015-12-01'],
})
df['datetime'] = pd.to_datetime(df['datetime'])
我想将所有属于周末的日期(所以 weekday==5 或 weekday==6)更改为之前的星期五,所以像这样:
def adjust_exp_date(x):
if x.weekday()==5:
x.weekday() -= 1
if x.weekday()==6:
x.weekday() -= 2
df['datetime'].apply(adjust_exp_date)
我能够结合使用为日期分配一天和使用 np.select() 根据日期时间列来 timedelta 7 天
df = pd.DataFrame( {
'datetime': ['2015-01-01', '2015-01-02', '2015-04-01', '2015-07-01', '2015-12-01', '2015-01-01', '2015-04-01', '2015-07-01', '2015-12-01'],
})
df['datetime'] = pd.to_datetime(df['datetime'])
df['dayOfWeek'] = df['datetime'].dt.day_name()
condition_list = [df['dayOfWeek'] == 'Friday', df['dayOfWeek'] == 'Saturday']
choice_list = [df['datetime'] - datetime.timedelta(days=7), df['datetime'] - datetime.timedelta(days=8)]
df['datetime'] = np.select(condition_list, choice_list, df['datetime'])
df
此外,我添加了一个日期 ('2015-01-02'),因为您的原始示例不包括任何星期五日期
您可以子 pd.offsets.Week
调整到最近的星期五(工作日 4)
m = df['datetime'].dt.weekday.isin([5,6])
df['adjust'] = df['datetime'].mask(m, df['datetime'] - pd.offsets.Week(weekday=4))
datetime week weekday adjust adjustweek
0 2015-01-01 Thursday 3 2015-01-01 Thursday
1 2015-01-02 Friday 4 2015-01-02 Friday
2 2015-01-03 Saturday 5 2015-01-02 Friday
3 2015-01-04 Sunday 6 2015-01-02 Friday
4 2015-01-05 Monday 0 2015-01-05 Monday
5 2015-01-06 Tuesday 1 2015-01-06 Tuesday
6 2015-01-07 Wednesday 2 2015-01-07 Wednesday
7 2015-01-08 Thursday 3 2015-01-08 Thursday
8 2015-01-09 Friday 4 2015-01-09 Friday
9 2015-01-10 Saturday 5 2015-01-09 Friday
10 2015-01-11 Sunday 6 2015-01-09 Friday
11 2015-01-12 Monday 0 2015-01-12 Monday
12 2015-01-13 Tuesday 1 2015-01-13 Tuesday
13 2015-01-14 Wednesday 2 2015-01-14 Wednesday
14 2015-01-15 Thursday 3 2015-01-15 Thursday
15 2015-01-16 Friday 4 2015-01-16 Friday