大熊猫用NaT代替NaN

时间:2016-05-11 15:37:42

标签: python pandas

我有一些我需要用NaT取代的NaN系列。我怎么能这样做?

以下是我迄今为止尝试过的一个简单示例:

>>> s = pd.Series([np.NaN, np.NaN])
>>> s.fillna(pd.NaT)
0   NaN
1   NaN
dtype: float64
>>> s.replace(np.NaN, pd.NaT)
0   NaN
1   NaN
dtype: float64
>>> s.where(pd.notnull(s), pd.NaT)
0    NaN
1    NaN
dtype: object

pandas版本:0.16.2

numpy版本:1.9.2

python版本:2.7.10

1 个答案:

答案 0 :(得分:8)

dtype首先将NaT转换为dtypefloat最初是dtype In [90]: s.astype(np.datetime64).fillna(pd.NaT) Out[90]: 0 NaT 1 NaT dtype: datetime64[ns] 时无意义:

NaN

如果系列中有非to_datetime值,请使用In [97]: s = pd.Series([np.NaN, np.NaN, 1.0]) pd.to_datetime(s) Out[97]: 0 NaT 1 NaT 2 1970-01-01 00:00:00.000000001 dtype: datetime64[ns]

    let str = "Working at Parse is great!"
    let data = str.dataUsingEncoding(NSUTF8StringEncoding)
    let file = PFFile(name:"resume.txt", data:data)
    file.saveInBackgroundWithBlock({
          (succeeded: Bool, error: NSError?) -> Void in
          // Handle success or failure here ...
       }, progressBlock: {(percentDone: Int32) -> Void in

     // Update your progress spinner here. percentDone will be between 0 and 100.

 })