pandas numpy.where: TypeError: 无效的类型提升
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45793044/
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
numpy.where: TypeError: invalid type promotion
提问by Johny Vaknin
I know there are other questions with the same error name, yet none of them match the np.where statement, and also I couldn't find the answer to my problem in them
我知道还有其他问题具有相同的错误名称,但没有一个与 np.where 语句匹配,而且我也找不到我的问题的答案
So I made a pandas DataFrame
called data
and created a Series
out of it called dates
, which is:
所以我做了一个大PandasDataFrame
被称为data
开创了Series
出来叫dates
,那就是:
dates= pd.to_datetime(pd.to_timedelta(data.a_date, unit= 'D') + pd.datetime(1960,1,1),
errors= 'coerse')
I need to clear some of the dates because they do not match with an indicator of them in data
, so I tried to adjust that while keeping the indexes correct using numpy.where
,
Yet I had gotten this error:
我需要清除一些日期,因为它们与 中的指示符不匹配data
,所以我尝试调整它,同时使用 保持索引正确numpy.where
,
但我收到了这个错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-18-2b83ed2b2468> in <module>()
----> 1 np.where(((dates.notnull()) & (data.a_IND == 0)), np.nan, dates)
TypeError: invalid type promotion
回答by Jiaqi
If you want to keep the date type, substitute np.nan
with np.datetime64('NaT')
:
如果要保留日期类型,请替换np.nan
为np.datetime64('NaT')
:
np.where(((dates.notnull()) & (data.a_IND == 0)), np.datetime64('NaT'), dates)
回答by Shiva
The documentation of np.where(cond, x, y)
says that the second and third arguments - x and y - need to be array or array_like. Also, I believe x and y must be of the same shape.
的文档np.where(cond, x, y)
说第二个和第三个参数 - x 和 y - 需要是 array 或 array_like。另外,我相信 x 和 y 必须具有相同的形状。
Your x is a scalar (np.nan
) and y is an array_like object (dates
). Maybe that's the problem.
您的 x 是一个标量 ( np.nan
) 而 y 是一个类似数组的对象 ( dates
)。也许这就是问题所在。
回答by tsando
I got a similar problem and managed to fix it by getting the date
property from the index, i.e. this works:
我遇到了类似的问题并设法通过date
从索引中获取属性来修复它,即这有效:
np.where(condition, df.x, df.index.date)
np.where(condition, df.x, df.index.date)
And this doesn't work:
这不起作用:
np.where(condition, df.x, df.index)
np.where(condition, df.x, df.index)
when the index has dtype='datetime64[ns]'
当索引有 dtype='datetime64[ns]'
Hope that helps!
希望有帮助!