Pythonで2つの日付の間の月数を探します

Shanoo:

私は以下のように日付オブジェクトと日付の列「日付1」内のパンダのデータフレーム「DF」を持っています:

date = '202107'

df
    date1 
0   2021-07-01   
1   2021-08-01   
2   2021-09-01   
3   2021-10-01   
4   2021-11-01

私はDFどこに列「ヶ月」を作成したいです

months = (date1 + 1month) - date

私の出力データフレームは、以下のようになります。

df
    date1        months
0   2021-07-01   1
1   2021-08-01   2
2   2021-09-01   3
3   2021-10-01   4
4   2021-11-01   5
YOBEN_S:

デイモン

s=(df.date1-pd.to_datetime(date,format='%Y%m'))//np.timedelta64(1, 'M')+1
Out[118]: 
0    1
1    2
2    3
3    4
4    5
Name: date1, dtype: int64
df['months']=s

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=24061&siteId=1