pandas基于日期条件自动递增

9rygscc1  于 2023-06-04  发布在  其他
关注(0)|答案(1)|浏览(119)

我想自动增加一个新的列我有下面的代码。

df = pd.DataFrame({'Date': ['2020-02-29', '2020-03-01', '2020-10-01', '2020-10-02', '2020-10-03', '2020-10-04', '2021-10-01', '2021-10-02', '2021-10-03', '2021-10-04']})

conditions = [((df['Date'].dt.day == 1) & (df['Date'].dt.month == 10)), ((df['Date'].dt.day == 29) & (df['Date'].dt.month == 2))]
r_choice = [1, 151]

df['Oct_year_day'] = np.select(conditions, r_choice, np.nan)
df['Oct_year_day'] = df['Oct_year_day'].fillna(1).cumsum()

# the below code does not work...
reset_condition = (df['Date'].dt.day == 1) & (df['Date'].dt.month == 10)
df.loc[reset_condition, 'Oct_year_day'] = 1

我想要的是:

Date  Oct_year_day
0  2020-02-29          151.0
1  2020-03-01          152.0 
2  2020-10-01            1.0
3  2020-10-02            2.0
4  2020-10-03            3.0
5  2020-10-04            4.0
6  2021-10-01            1.0
7  2021-10-02            2.0
8  2021-10-03            3.0
9  2021-10-04            4.0
  • 编辑增量。*

因此,递增将从每年10月1日开始,并在以后的每个日期增加1。把它想象成df['Date'].dt.dayofyear。另一部分是闰日将是151,并且每个后续日期将增加1。

iecba09b

iecba09b1#

IIUC,您需要从当前年份的第一个1ctober开始的年份:

def counter(sr):
    year = sr.name
    oct1 = pd.Timestamp(f'{year}-10-01')
    m = sr < oct1
    return pd.concat([sr[m].sub(oct1-pd.DateOffset(years=1)).dt.days,
                      sr[~m].sub(oct1-pd.DateOffset(days=1)).dt.days])

df['Oct_year_day'] = df.groupby(df['Date'].dt.year)['Date'].transform(counter)

输出:

>>> df
         Date  Oct_year_day
0  2020-02-29           151
1  2020-03-01           152
2  2020-10-01             1
3  2020-10-02             2
4  2020-10-03             3
5  2020-10-04             4
6  2021-10-01             1
7  2021-10-02             2
8  2021-10-03             3
9  2021-10-04             4
10 2022-02-28           150
11 2022-03-01           151

相关问题