简体   繁体   English

pandas.to_datetime:类型错误:类型提升无效

[英]pandas.to_datetime: TypeError: invalid type promotion

I get我得到

TypeError: invalid type promotion类型错误:无效的类型提升

at

df.iloc[1:,0] = pd.to_datetime(np.where(m, df.iloc[1:,0], np.NaN), errors='ignore')

in the below code.在下面的代码中。

It is because at df.iloc[1, 0], the empty value has m = True.这是因为在 df.iloc[1, 0] 处,空值有 m = True。 How do I solve this issue?我该如何解决这个问题?

m = df.iloc[1:,0].apply(lambda v: isinstance(v, datetime))
df1 ['Date'] = df.iloc[1:,0]
df1 ['m'] = m
print(df1)

df.iloc[1:,0] = pd.to_datetime(np.where(m, df.iloc[1:,0], np.NaN), errors='ignore')

**OUTPUT of df1 ** **df1 的输出**


|Date            |  m   |
|                | TRUE |
|2000-01-31      | TRUE |

Alternative选择
I tested the below as well, but I still get same error at我也测试了下面的内容,但我仍然遇到同样的错误

df.iloc[1:,0] = pd.to_datetime(np.where(m, df.iloc[1:,0], np.NaN), errors='ignore')

Regardless if errors='ignore' or errors='coerce'不管 errors='ignore' 还是 errors='coerce'

m = df.iloc[1:,0].apply(lambda v: isinstance(v, datetime) if pd.isnull(v)==False else False)

df1 ['Date'] = df.iloc[1:,0]
df1 ['m'] = m
print(df1)

df.iloc[1:,0] = pd.to_datetime(np.where(m, df.iloc[1:,0], np.NaN), errors='ignore')

**OUTPUT of df1 ** **df1 的输出**


|Date            |  m   |
|                | FALSE |
|2000-01-31      | TRUE |

The column was read in as datetime.该列作为日期时间读入。 Hence np.NaN led to the error in因此np.NaN导致错误

np.where(m, df.iloc[1:,0], np.NaN)

Changing to np.datetime64('NaT') , solved the issue更改为np.datetime64('NaT') ,解决了问题

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

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