试图用今天的日期减去created_date列填充一列,但出现以下错误:TypeError:-:'str'和'str'的不支持的操作数类型
import datetime
now = datetime.date.today()
today = '{0:%m/%d/%Y}'.format(now).format(now)
today
data['Aging'] = today
data['Aging'] = data['Aging'].sub(data['Created_Date'], axis=0)
TypeError:--'str'和'str'的不受支持的操作数类型
答案 0 :(得分:1)
我认为需要减去datetime
,因此有必要在date
和now
列中转换Created_Date
,最后将timedelta
s转换为天数dt.days
:
import datetime
now = datetime.date.today()
today = pd.Timestamp(now)
data['Created_Date'] = pd.to_datetime(data['Created_Date'])
data['Aging'] = today
data['Aging'] = data['Aging'].sub(data['Created_Date'], axis=0).dt.days
解决方案应该简化:
data['Created_Date'] = pd.to_datetime(data['Created_Date'])
data['Aging'] = data['Created_Date'].rsub(today, axis=0).dt.days
答案 1 :(得分:0)
也许,请尝试将其作为完整代码:
data['Created_Date'] = pd.to_datetime(data['Created_Date'])
data['Aging'] = pd.to_datetime('now')
data['Aging'] = data['Aging'].sub(data['Created_Date'], axis=0)
您想要什么,也许:
data['Created_Date'] = pd.to_datetime(data['Created_Date'])
data['Aging'] = pd.to_datetime('now').strftime('%m/%d/%Y')
data['Aging'] = data['Aging'].sub(data['Created_Date'], axis=0)