简体   繁体   English

Pandas 上两列的日期差异

[英]Date difference of two columns on Pandas

The fields 'now', 'EndDate' and 'CreateDate' are dates.字段“now”、“EndDate”和“CreateDate”是日期。 With the latest date of EndDate/CreateDate, 'diff_m' outputs the month difference between that field and 'now' (rounded down).使用 EndDate/CreateDate 的最新日期,'diff_m' 输出该字段与 'now' 之间的月差(向下取整)。

now=dt.now()
df['MaxDate'] = df[['EndDate', 'CreateDate']].max(axis=1)
df['diff_months'] = now - df['MaxDate']
df['diff_months']=df['diff_months']/np.timedelta64(1,'M')
df['diff_m']=df['diff_months'].apply(np.floor)

However, I'm getting the following error:但是,我收到以下错误:

TypeError unsupported operand type(s) for -: 'datetime.datetime' and 'float'

I see that CreateDate/EndDate are dtype: datetime64[ns]我看到 CreateDate/EndDate 是 dtype: datetime64[ns]

The problem lies in the line问题出在线路上

df['diff_months'] = now - df['MaxDate']

When you find the max, the type of the newly made column changes and thus you can address that by simply using pd.to_datetime当您找到最大值时,新列的类型会发生变化,因此您只需使用pd.to_datetime即可解决该pd.to_datetime

df['diff_months'] = now - pd.to_datetime(df['MaxDate'])

I tried a small example, here is the result:我尝试了一个小例子,结果如下:

In [36]: new_df['temp_date'] = new_df[['date','new_date']].max(axis=1)

In [37]: now = datetime.now()

In [38]: new_df['diff_mon'] = now-new_df['temp_date']

TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str'

However, on converting it to datetime但是,将其转换为日期时间

In [39]: new_df['diff_mon'] = now-pd.to_datetime(new_df['temp_date'])

In [40]: new_df['diff_mon']
Out[40]: 
0    7487 days 19:20:37.060114
1    7547 days 19:20:37.060114

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM