问题描述
我想将pandas cut功能应用于包含NaN的系列.理想的行为是将非NaN元素存储到桶中,并为NaN元素返回NaN.
I would like to apply the pandas cut function to a series that includes NaNs. The desired behavior is that it buckets the non-NaN elements and returns NaN for the NaN-elements.
import pandas as pd
numbers_with_nan = pd.Series([3,1,2,pd.NaT,3])
numbers_without_nan = numbers_with_nan.dropna()
对于没有NaN的系列,裁剪效果很好:
The cutting works fine for the series without NaNs:
pd.cut(numbers_without_nan, bins=[1,2,3], include_lowest=True)
0 (2.0, 3.0]
1 (0.999, 2.0]
2 (0.999, 2.0]
4 (2.0, 3.0]
当我剪切包含NaN的序列时,元素3正确返回为NaN,但是最后一个元素分配了错误的bin:
When I cut the series that contains NaNs, element 3 is correctly returned as NaN, but the last element gets the wrong bin assigned:
pd.cut(numbers_with_nan, bins=[1,2,3], include_lowest=True)
0 (2.0, 3.0]
1 (0.999, 2.0]
2 (0.999, 2.0]
3 NaN
4 (0.999, 2.0]
如何获得以下输出?
0 (2.0, 3.0]
1 (0.999, 2.0]
2 (0.999, 2.0]
3 NaN
4 (2.0, 3.0]
推荐答案
这很奇怪.问题不是pd.NaT
,而是您的系列具有object
dtype而不是常规数字系列的事实,例如float
,int
.
This is strange. The problem isn't pd.NaT
, it's the fact your series has object
dtype instead of a regular numeric series, e.g. float
, int
.
一个快速的解决方法是通过fillna
用np.nan
替换pd.NaT
.这会触发从object
到float64
dtype的系列转换,也可能导致更好的性能.
A quick fix is to replace pd.NaT
with np.nan
via fillna
. This triggers series conversion from object
to float64
dtype, and may also lead to better performance.
s = pd.Series([3, 1, 2, pd.NaT, 3])
res = pd.cut(s.fillna(np.nan), bins=[1, 2, 3], include_lowest=True)
print(res)
0 (2, 3]
1 [1, 2]
2 [1, 2]
3 NaN
4 (2, 3]
dtype: category
Categories (2, object): [[1, 2] < (2, 3]]
更通用的解决方案是事先将其显式转换为数字:
A more generalized solution is to convert to numeric explicitly beforehand:
s = pd.to_numeric(s, errors='coerce')
这篇关于 pandas 剪出一系列具有南值的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!