Pandas Vectorized Date Offset Operations with Vector of Differing Offsets

前端 未结 2 1164
既然无缘
既然无缘 2021-01-11 22:56

I am trying to do the following but is seems that vectorized operations in this mode are not supported.

import pandas as pd
df=pd.DataFrame([[2017,1,15,1],
          


        
2条回答
  •  醉梦人生
    2021-01-11 23:01

    Consider the following approach:

    In [94]: df['date'] = pd.to_datetime(df[['year','month','day']])
    
    In [95]: df['date_offset'] = df.apply(lambda x: x['date'] + pd.offsets.MonthEnd(x['month_offset']), axis=1)
    
    In [96]: df
    Out[96]:
       year  month  day  month_offset       date date_offset
    0  2017      1   15             1 2017-01-15  2017-01-31
    1  2017      1   15             2 2017-01-15  2017-02-28
    2  2017      1   15             3 2017-01-15  2017-03-31
    3  2017      1   15             4 2017-01-15  2017-04-30
    4  2017      1   15             5 2017-01-15  2017-05-31
    5  2017      1   15             6 2017-01-15  2017-06-30
    6  2017      1   15             7 2017-01-15  2017-07-31
    

提交回复
热议问题