Python 如何用None-pandas 0.24.1替换NaN和NaT

Python 如何用None-pandas 0.24.1替换NaN和NaT,python,pandas,Python,Pandas,我需要将pandas.Series中的所有NaN和NaT替换为None 我试过这个: def replaceMissing(ser): return ser.where(pd.notna(ser), None) 但它不起作用: import pandas as pd NaN = float('nan') NaT = pd.NaT floats1 = pd.Series((NaN, NaN, 2.71828, -2.71828)) floats2 = pd.Series((2.718

我需要将
pandas.Series
中的所有
NaN
NaT
替换为
None

我试过这个:

def replaceMissing(ser):
    return ser.where(pd.notna(ser), None)
但它不起作用:

import pandas as pd

NaN = float('nan')
NaT = pd.NaT

floats1 = pd.Series((NaN, NaN, 2.71828, -2.71828))
floats2 = pd.Series((2.71828, -2.71828, 2.71828, -2.71828))
dates = pd.Series((NaT, NaT, pd.Timestamp("2019-07-09"), pd.Timestamp("2020-07-09")))


def replaceMissing(ser):
    return ser.where(pd.notna(ser), None)


print(pd.__version__)
print(80*"-")
print(replaceMissing(dates))
print(80*"-")
print(replaceMissing(floats1))
print(80*"-")
print(replaceMissing(floats2))
如您所见,
NaT
未被替换:

0.24.1
--------------------------------------------------------------------------------
0          NaT
1          NaT
2   2019-07-09
3   2020-07-09
dtype: datetime64[ns]
--------------------------------------------------------------------------------
0       None
1       None
2    2.71828
3   -2.71828
dtype: object
--------------------------------------------------------------------------------
0    2.71828
1   -2.71828
2    2.71828
3   -2.71828
dtype: float64
然后我尝试了这个额外的步骤:

def replaceMissing(ser):
    ser = ser.where(pd.notna(ser), None)
    return ser.replace({pd.NaT: None})
但它仍然不起作用。由于某种原因,它会返回
NaN
s:

0.24.1
--------------------------------------------------------------------------------
0                   None
1                   None
2    2019-07-09 00:00:00
3    2020-07-09 00:00:00
dtype: object
--------------------------------------------------------------------------------
0        NaN
1        NaN
2    2.71828
3   -2.71828
dtype: float64
--------------------------------------------------------------------------------
0    2.71828
1   -2.71828
2    2.71828
3   -2.71828
dtype: float64
我还尝试将该系列转换为
对象

def replaceMissing(ser):
    return ser.astype("object").where(pd.notna(ser), None)
但现在,最后一个系列也是
对象
,即使它没有缺失值:

0.24.1
--------------------------------------------------------------------------------
0                   None
1                   None
2    2019-07-09 00:00:00
3    2020-07-09 00:00:00
dtype: object
--------------------------------------------------------------------------------
0       None
1       None
2    2.71828
3   -2.71828
dtype: object
--------------------------------------------------------------------------------
0    2.71828
1   -2.71828
2    2.71828
3   -2.71828
dtype: object
我希望它保持
float64
。因此,我添加了
推断对象

def replaceMissing(ser):
    return ser.astype("object").where(pd.notna(ser), None).infer_objects()
但它再次带来了
NaN
s:

0.24.1
--------------------------------------------------------------------------------
0                   None
1                   None
2    2019-07-09 00:00:00
3    2020-07-09 00:00:00
dtype: object
--------------------------------------------------------------------------------
0        NaN
1        NaN
2    2.71828
3   -2.71828
dtype: float64
--------------------------------------------------------------------------------
0    2.71828
1   -2.71828
2    2.71828
3   -2.71828
dtype: float64

我觉得必须有一个简单的方法来做到这一点。有人知道吗?

对于我来说,您的第二个解决方案的工作变更单在
0.24.2
中进行了测试,但是
dtype
s被更改为object,因为混合类型-
None
s和
float
s或
timestamp
s:

def replaceMissing(ser):
    return ser.replace({pd.NaT: None}).where(pd.notna(ser), None)

print(pd.__version__)
print(80*"-")
print(replaceMissing(dates))
print(80*"-")
print(replaceMissing(dates).apply(type))
print(80*"-")
print(replaceMissing(floats1))
print(80*"-")
print(replaceMissing(floats1).apply(type))
print(80*"-")
print(replaceMissing(floats2))

0.24.2
--------------------------------------------------------------------------------
0无
1无
2    2019-07-09 00:00:00
3    2020-07-09 00:00:00
数据类型:对象
--------------------------------------------------------------------------------
0
1.

2对于我来说,您的第二个解决方案的工作变更单在
0.24.2
中进行了测试,但是
dtype
s更改为object,因为混合类型-
None
s与
float
s或
timestamp
s:

def replaceMissing(ser):
    return ser.replace({pd.NaT: None}).where(pd.notna(ser), None)

print(pd.__version__)
print(80*"-")
print(replaceMissing(dates))
print(80*"-")
print(replaceMissing(dates).apply(type))
print(80*"-")
print(replaceMissing(floats1))
print(80*"-")
print(replaceMissing(floats1).apply(type))
print(80*"-")
print(replaceMissing(floats2))

0.24.2
--------------------------------------------------------------------------------
0无
1无
2    2019-07-09 00:00:00
3    2020-07-09 00:00:00
数据类型:对象
--------------------------------------------------------------------------------
0
1.


2抱歉,这不起作用,
None
是一个python对象数据类型,因此您必须将列的数据类型转换为
object
,为了做到这一点,您试图通过这样做实现什么?问题是@EdChum为什么不?如果一个序列只有非缺失的浮点,它可以保持为
float64
序列。
None
NaN
不同,因此它不能用
float
dtype表示,因此一旦插入
None
它就会立即将dtype更改为
object
。问题仍然是,如果一系列
float64
值没有
NaN
,那么它应该保持一系列
float64
,而不是变成一系列
对象。不,我问你为什么要首先用
None
替换
NaN
NaT
?对不起,这行不通,
None
是python对象的数据类型,所以你必须将列的数据类型转换为
对象
,才能这样做,问题是你想通过这样做来实现什么?@EdChum为什么不呢?如果一个序列只有非缺失的浮点,它可以保持为
float64
序列。
None
NaN
不同,因此它不能用
float
dtype表示,因此一旦插入
None
它就会立即将dtype更改为
object
。问题仍然是,如果一系列
float64
值没有
NaN
,那么它应该保持一系列
float64
,而不是变成一系列
对象。不,我在问你为什么要首先用
None
替换
NaN
NaT
?这对我也适用,但我不明白为什么如果你切换顺序,它就不起作用了。@spiderface-我想因为熊猫最好使用相同的数据类型-列中的所有值都是float或datetime(因此
NaN
NaT
),这是一种有点黑客行为;)我想不太黑客行为的方式是使用一个条件:
def replacemission(ser):if ser.isna().any():返回ser.astype(“object”)。其中(pd.notna(ser),None)其他:返回ser
@spiderface-是,但
如果其他情况下
不需要-
def replacemission(ser):return ser.astype(“object”)。where(pd.notna(ser),None)
但我希望尽可能保留该数据类型。这对我也适用,但我不明白为什么如果您切换顺序,它就不起作用。@spiderface-我想因为熊猫最好使用相同的数据类型-列中的所有值都是float或datetime(因此
NaN
NaT
),这是一种有点黑客行为;)我想不太黑客行为的方式是使用一个条件:
def replacemission(ser):if ser.isna().any():返回ser.astype(“object”)。其中(pd.notna(ser),None)其他:返回ser
@spiderface-是,但
如果其他情况下
不需要-
def replacemission(ser):返回ser.astype(“object”)。其中(pd.notna(ser),None)
但我希望尽可能保留数据类型。