2017-08-09 1 views
1

私は、以下の私のコードテストデータと説明だけで問題に混乱しています。apply.pd.to_date_Time。マッピングされていない/ NaTを扱うことを適用する

test = {"Med to Ind Date": ['', '', 1402531200000000000, '', 1402876800000000000], 
     "Med to Ind Indicator": ['', '', 'Y', '', 'Y']} 
test = pd.DataFrame(test) 
date_fields = ["Med to Ind Date"] 
test.loc[:, date_fields] = test.loc[:, date_fields].apply(pd.to_datetime) 

上記のコードを実行すると、すべての空白の時間フィールドがNaTにマップされます。

if "Med to Ind Indicator" in test.columns: 
    test["Med to Ind Indicator"] = np.where(test["Med to Ind Date"] != '', "Yes", '') 

上記のコードはメッドインディアナ州に日付フィールドを見て、それが空白でない場合、それはYesにインディアナインジケータに列メッドをマップ:罰金である、しかし、それは以下の私のコードを中断しています。 私が行っていた作業は、pd.NaTを ""と置き換えようとしていましたが、date_time変換をundidして元の形式に戻しました。皆さんは代替案をお勧めしますか?また、パンダはNaTフィールドをどのように正確に表示しますか?

+0

'試験[date_fields] = test.loc [:, date_fields] .apply( pd.to_datetime、unit = 'ns'、errors = 'coerce') ' - これはあなたの最初の質問に答えるはずです... – MaxU

答えて

2

使用isnull()(又はnotnull())がNaTをテストする:

np.where(test["Med to Ind Date"].isnull(), '', "Yes") 

結果出力testの:

 Med to Ind Date Med to Ind Indicator 
0     None      
1     None      
2 1402531200000000000     Yes 
3     None      
4 1402876800000000000     Yes 
+0

恐ろしい、ありがとう。それは私の2番目の質問にも答えます。したがって、Pandasはnull時間値をNaTとして扱います。 – Bjc51192

関連する問題