pandas 类型错误:<class 'datetime.time'> 不能转换为 datetime

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49389358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:20:59  来源:igfitidea点击:

TypeError: <class 'datetime.time'> is not convertible to datetime

pandasdate

提问by TylerNG

The problem is somewhat simple. My objective to to compute the days difference between two dates, say A and B.
These are my attempts:

问题有点简单。我的目标是计算两个日期之间的天数差异,比如 A 和 B。
这些是我的尝试:

df['daydiff'] = df['A']-df['B']

df['daydiff'] = ((df['A']) - (df['B'])).dt.days

df['daydiff'] = (pd.to_datetime(df['A'])-pd.to_datetime(df['B'])).dt.days

These works for me before but for some reason, I'm keep getting this error this time:

这些以前对我有用,但由于某种原因,这次我不断收到此错误:

TypeError: class 'datetime.time' is not convertible to datetime

类型错误:类“datetime.time”不可转换为日期时间

When I export the df to excel and the date works just fine. Any thought?

当我将 df 导出到 excel 并且日期工作正常时。任何想法?

回答by emmet02

Use pd.Timestamp to handle the awkward differences in your formatted times.

使用 pd.Timestamp 处理格式化时间中的尴尬差异。

df['A'] = df['A'].apply(pd.Timestamp)  # will handle parsing
df['B'] = df['B'].apply(pd.Timestamp)  # will handle parsing
df['day_diff'] = (df['A'] - df['B']).dt.days

Of course, if you don't want to change the format of the df['A'] and df['B'] within the DataFrame that you are outputting, you can do this in a one-liner.

当然,如果您不想在输出的 DataFrame 中更改 df['A'] 和 df['B'] 的格式,则可以在单行中执行此操作。

df['day_diff'] = (df['A'].apply(pd.Timestamp) - df['B'].apply(pd.Timestamp)).dt.days

This will give you the days between as an integer.

这将为您提供整数之间的天数。

回答by Leo Wang

When I applied the solution offered by emmet02, I got TypeError: Cannot convert input [00:00:00] of type as well. It's basically saying that the dataframe contains missing timestamp values which are represented as [00:00:00], and this value is rejected by pandas.Timestampfunction.

当我应用 emmet02 提供的解决方案时,我得到了类型错误:无法转换类型的输入 [00:00:00]。它基本上是说数据帧包含缺失的时间戳值,这些值表示为 [00:00:00],并且该值被pandas.Timestamp函数拒绝。

To address this, simply apply a suitable missing-value strategy to clean your data set, before using

为了解决这个问题,在使用之前简单地应用一个合适的缺失值策略来清理你的数据集

df.apply(pd.Timestamp)