2014-03-18 173 views
2

數據 - 這是我的數據在熊貓數據幀ValueError異常:未轉換的數據保留在熊貓據幀

CallDateAndTimeStart 
01/01/2010 00:26:28.003613 MST 
01/01/2010 00:28:54.230713 MST 
01/02/2008 14:12:11 MST 
05/19/2010 09:12:32.080728 MST 

我試圖列D型改爲datetime64 [NS]

df['CallDateAndTimeStart'] = pandas.to_datetime(df['CallDateAndTimeStart'], 
    format='%m/%d/%Y %H:%M:%S') 

錯誤消息 - 未清理數據時,出現以下錯誤:

File "C:\Python27\lib\site-packages\pandas\tseries\tools.py", line 308, in _convert_listlike raise e 
ValueError: unconverted data remains: .003613 MST 

問題

我怎麼會糾正我的數據框列,以便它可以轉換爲datetime類型?我發佈了我的答案,但有更好的答案嗎?謝謝。

回答

1

代碼

我應用自定義功能上數據幀柱(convert_time)

df['CallDateAndTimeStart'] = df['CallDateAndTimeStart'].apply(convert_time) 

def convert_time(mytime): 
""" Fix DateTime by removing details after . and timezones """ 

    # Remove on period and after 
    try: 
     mytime = str(mytime).split(".")[0] 
    except ValueError: 
     print "Not able to split ." 

    # Remove Timeframe (E.g. MST) 
    mytime = str(mytime).split(" ")[0] + " " + str(mytime).split(" ")[1] 

return mytime 

df['CallDateAndTimeStart'] = pandas.to_datetime(df['CallDateAndTimeStart'], 
    format='%m/%d/%Y %H:%M:%S') 

輸出

CallDateAndTimeStart 
2010-01-01 00:26:28 
2010-01-01 00:28:54 
2010-05-19 09:12:32 
2008-01-02 14:12:11 
2010-01-01 00:39:41 
0

我encounterd同樣的問題,我使用的相同像你這樣解決它(應用一個函數來刪除不必要的數據)

我想你可以使用標準的接口,以避免此問題:

>>> now = time.time() # get current time in second 
>>> now_format = time.ctime(now) # get formatted time, like 'Thu May 21 17:43:46 2015' 

,然後使用time.strptime()來得到一個標準的時間結構:

>>> standard_time_struct = time.strptime(now_format,"%a %B %d %X %Y") 

你可以得到最終結果如下:

>>> standard_time_struct 
>>> time.struct_time(tm_year=2015, tm_mon=5, tm_mday=21, tm_hour=17, tm_min=49, tm_sec=10, tm_wday=3, tm_yday=141, tm_isdst=-1)