2016-02-19 61 views
0

我很努力地讀取日期時間字符串並將其存儲爲變量。我有一個數據塊,看起來像這樣:在用熊貓讀數據時解析日期時間字符串

2011-11-01 05:20:00 00:10:00 
# z speed dir  W sigW  bck error 
30 4.76 238.9 0.01 0.13 7.56E+06  0 
40 5.24 237.1 -0.05 0.12 5.99E+06  0 
50 6.33 236.6 -0.01 0.12 7.24E+06  0 
60 7.06 237.3 -0.01 0.12 9.15E+06  0 
70 7.85 238.2 -0.02 0.13 8.47E+06  0 
80 8.85 237.3 -0.03 0.14 1.05E+07  256 

2011-11-01 05:30:00 00:10:00 
# z speed dir  W sigW  bck error 
30 4.40 234.8 0.08 0.12 1.33E+07  0 
40 5.07 234.2 0.11 0.12 5.82E+06  0 
50 5.75 234.3 0.12 0.12 6.61E+06  0 
60 6.56 232.4 0.08 0.13 6.39E+06  0 
70 7.22 233.2 0.10 0.13 5.64E+06  0 
80 8.15 235.3 0.12 0.14 5.87E+06  256 

我的代碼,我需要什麼,除了用於讀取的時間字符串,因爲我不斷收到一個錯誤要做的偉大工程。這裏是我的代碼: 進口大熊貓作爲PD 進口水珠 進口日期時間

def parse_date(string): 
    # Split the string into year/month/date, time, and seconds 
    split_string = string.split() 
    # get year month and date 
    year = split_string[0].split('-')[0] 
    month = split_string[0].split('-')[1] 
    date = split_string[0].split('-')[2] 

    # get hour minute second 
    hour = split_string[1].split(':')[0] 
    mm = split_string[1].split(':')[1] 
    second = split_string[1].split(':')[2] 

    return datetime.datetime(int(year), int(month), int(date), hour=int(hour), minute=int(mm), second=int(second)) 

filename = glob.glob('1511??.mnd') 
data_nov15_hereford = pd.DataFrame() 
frames = [] 
dates = [] 
counter = 1 
for i in filename: 
    f_nov15_hereford = pd.read_csv(i, skiprows = 32, sep='\s+') 
    for line in f_nov15_hereford: 
     if line.startswith('20'): 
      print line 
      dates.append(parse_date(line)) 
      counter = 0 
     else: 
      counter += 1 
    frames.append(f_nov15_hereford) 
data_nov15_hereford = pd.concat(frames,ignore_index=True) 
data_nov15_hereford = data_nov15_hereford.convert_objects(convert_numeric=True) 

我的錯誤與我的分析功能:

 15  # get hour minute second 
---> 16  hour = split_string[1].split(':')[0] 
    17  mm = split_string[1].split(':')[1] 
    18  second = split_string[1].split(':')[2] 

IndexError: list index out of range 

如果有人可以幫我找出這個錯誤,這將是大。謝謝!

回答

2

不要通過創建自己的日期解析函數來重新發明輪子。利用標準庫中的datetime.datetime.strptime函數。

將日期字符串和字符串格式傳遞給strptime函數。

import datetime 
date_string = '2011-11-01 05:20:00' 
date_object = datetime.datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S') 

它看起來像你正在處理一個字符串,有一個日期和時間以及間隔?您可以分析單獨的日期,時間和間隔:

original_string = '2011-11-01 05:20:00 00:10:00' 
date_string, time_string, interval_string = original_string.split() 
date_object = datetime.datetime.strptime(date_string, '%Y-%m-%d') 
time_object = datetime.datetime.strptime(time_string, ' %H:%M:%S') 
interval_object = datetime.datetime.strptime(interval_string, '%H:%M:%S') 

我會檢討docs for parsing and formatting dates

+0

這很奇怪,因爲當我把這個第一個建議到我的代碼我得到一個錯誤:ValueError異常:時間數據「2015年11月1日」不符合格式「%Y-%間% d%H:%M:%S' – HM14

+0

如果您的字符串只是「2015-11-01」,那麼格式爲「%Y-%m-%d」。 – chishaku

+0

'%Y'表示四位數年份,'%m'表示兩位數,零填充月份,'%d'表示兩位數填零日期。 – chishaku

1

你可以簡單地讓時間字符串

thestring = "2011-11-01 05:20:00 00:10:00"` 

然後轉換成時間

aa = thestring.split(" ") 
t =datetime.datetime.strptime(aa[0]+" "+aa[1], "%Y-%m-%d %H:%M:%S") 

最後訪問小時,分鐘等。例如,

t.hour