英文:
Number of days between today and a certain date (Pandas)
问题
I have a dataframe df which looks like this. Date is in a text format.
Reference | date |
---|---|
RefA | 2022-11-24 |
RefB | 2021-05-14 |
... | ... |
My question is seemingly simple - I would like to create a new column, with for each Reference the time difference (in days) between the date and today.
I searched and tried several things, which are not working and I am struggling to understand why.
df['date'] = df['date'].dt.strftime('%Y-%m-%d')
df['days_diff_from_today'] = (pd.to_datetime('today')-df.date).dt.days
This returns TypeError: unsupported operand type(s) for -: 'Timestamp' and 'str'
So I tried converting my str in a date but...
df['date'] = df['date'].dt.strftime('%Y-%m-%d')
df['days_diff_from_today'] = (pd.to_datetime('today')-df.date).dt.days
This returns "AttributeError: Can only use .dt accessor with datetimelike values"
What am I doing wrong and what would be the correct syntax here?
Many thanks in advance!
英文:
I have a dataframe df which looks like this. Date is in a text format.
Reference | date |
---|---|
RefA | 2022-11-24 |
RefB | 2021-05-14 |
... | ... |
My question is seemingly simple - I would like to create a new column, with for each Reference the time difference (in days) between the date and today.
I searched and tried several things, which are not working and I am struggling to understand why.
df['date'] = df['date'].dt.strftime('%Y-%m-%d')
df['days_diff_from_today'] = (pd.to_datetime('today')-df.date).dt.days
This returns TypeError: unsupported operand type(s) for -: 'Timestamp' and 'str'
So I tried converting my str in a date but...
df['date'] = df['date'].dt.strftime('%Y-%m-%d')
df['days_diff_from_today'] = (pd.to_datetime('today')-df.date).dt.days
This returns "AttributeError: Can only use .dt accessor with datetimelike values"
What am I doing wrong and what would be the correct syntax here ?
Many thanks in advance !
答案1
得分: 1
你的日期列目前具有字符串数据类型,而不是 datetime64。
df['days_diff_from_today'] = np.datetime64('today') - df['date'].astype('datetime64')
或者,如果你想要将日期列转换为 datetime 类型。
df['date'] = df['date'].astype('datetime64')
df['days_diff_from_today'] = np.datetime64('today') - df['date']
英文:
Your date column currently has data type string instead of datetime64.
df['days_diff_from_today'] = np.datetime64('today') - df['date'].astype('datetime64')
Alternatively, if you want your date column to be in datetime.
df['date'] = df['date'].astype('datetime64')
df['days_diff_from_today'] = np.datetime64('today') - df['date']
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论