2016-05-17 20 views
2

文字列をデータフレームからdatetimeに変換したいと考えています。ValueError:日が月の範囲外です

dfx = df.ix[:,'a'] 
dfx = pd.to_datetime(dfx) 

しかし、それは次のエラー得られます

ValueError: day is out of range for month

は誰でも助けることができるの?

+1

dfx' 'の値は何ですか? – Barmar

+0

可能な複製http://stackoverflow.com/questions/17690738/in-pandas-how-do-i-convert-a-string-of-date-strings-to-datetime-objects-and-put – badgley

答えて

5

たぶん日時のフォーマットが30-01-2016であれば、to_datetimeにパラメータdayfirst=Trueを追加するのに役立ちます。

dfx = '30-01-2016' 

dfx = pd.to_datetime(dfx, format='%d-%m-%Y', errors='coerce') 
print (dfx) 
2016-01-30 00:00:00 

dfx = df.ix[:,'a'] 
dfx = pd.to_datetime(dfx, dayfirst=True) 

ユニバーサルよりは、他のformatNaNへと値を置き換えるためerrors='coerce'で使用するパラメータformatですサンプル:

dfx = pd.Series(['30-01-2016', '15-09-2015', '40-09-2016']) 
print (dfx) 
0 30-01-2016 
1 15-09-2015 
2 40-09-2016 
dtype: object 

dfx = pd.to_datetime(dfx, format='%d-%m-%Y', errors='coerce') 
print (dfx) 
0 2016-01-30 
1 2015-09-15 
2   NaT 
dtype: datetime64[ns] 

フォーマットが標準(例: 01-30-2016または01-30-2016)、のみerrors='coerce'を追加します。

dfx = pd.Series(['01-30-2016', '09-15-2015', '09-40-2016']) 
print (dfx) 
0 01-30-2016 
1 09-15-2015 
2 09-40-2016 
dtype: object 

dfx = pd.to_datetime(dfx, errors='coerce') 
print (dfx) 
0 2016-01-30 
1 2015-09-15 
2   NaT 
dtype: datetime64[ns] 
関連する問題