如何将月份列添加到 python 中的日期列?

How to add month column to a date column in python?

date['Maturity_date'] = data.apply(lambda data: relativedelta(months=int(data['TRM_LNTH_MO'])) + data['POL_EFF_DT'], axis=1)

也试过这个:

date['Maturity_date'] = date['POL_EFF_DT'] + date['TRM_LNTH_MO'].values.astype("timedelta64[M]")

TypeError: 'type' object does not support item assignment

import pandas as pd
import datetime

#Convert the date column to date format
date['date_format'] = pd.to_datetime(date['Maturity_date'])

#Add a month column
date['Month'] = date['date_format'].apply(lambda x: x.strftime('%b'))

如果您正在使用 Pandas,您可以使用名为“频率别名”的资源。开箱即用的东西:

# For "periods": 1 (is the current date you have) and 2 the result, plus 1, by the frequency of 'M' (month).

import pandas as pd

_new_period = pd.date_range(_existing_date, periods=2, freq='M')

现在您可以准确地获得您想要的第二个元素返回的时间段:

# The index for your information is 1. Index 0 is the existing date.

_new_period.strftime('%Y-%m-%d')[1]

# You can format in different ways. Only Year, Month or Day. Whatever.

Consult this link for further information