Number of days between today and a certain date (Pandas)

huangapple go评论90阅读模式
英文:

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']

huangapple
  • 本文由 发表于 2023年3月3日 23:52:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629244.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定