我正在尝试从数据框中减去日期并返回之间的天数。我正在使用的数据框看起来像这样
incidentBeginDate incidentEndDate
1978-02-06T00:00:00.000Z 1978-02-08T00:00:00.000Z
1980-08-15T00:00:00.000Z 1980-08-22T00:00:00.000Z
1981-12-19T00:00:00.000Z 1983-01-08T00:00:00.000Z
1982-03-12T00:00:00.000Z 1982-03-23T00:00:00.000Z
1982-08-23T00:00:00.000Z 1982-09-13T00:00:00.000Z
1983-01-21T00:00:00.000Z 1983-03-30T00:00:00.000Z
我试图减去日期并返回以天为单位的差额
df['difference'] = df(['incidentBeginDate'] - df['incidentEndDate']).astype(dt.timedelta)
但是我遇到错误
TypeError: unsupported operand type(s) for -: 'list' and 'str'
我觉得这应该相对简单,但似乎无法使它起作用
编辑:我意识到日期被存储为字符串。一旦使用[]
df['incidentBeginDate'] = pd.to_datetime(df['incidentBeginDate'])
df['incidentEndDate'] = pd.to_datetime(df['incidentEndDate'])
效果很好
我正在尝试从数据框中减去日期并返回之间的天数。我正在使用的数据帧看起来像这个eventBeginDate EventEndDate ...
这应该工作