簡體   English   中英

使用熊貓讀取數據時解析日期時間字符串

[英]Parsing a datetime string when reading in data with pandas

我正在努力讀取日期時間字符串並將其存儲為變量。 我有一個看起來像這樣的數據塊:

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

我的代碼非常適合我需要做的事情,除了讀取datetime字符串外,因為我一直遇到錯誤。 這是我的代碼:將pandas導入為pd import glob導入datetime

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

如果有人可以幫助我弄清楚這個錯誤,那就太好了。 謝謝!

不要通過創建自己的日期解析功能來重新發明輪子。 利用標准庫中的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')

我將檢查文檔以進行解析和格式化

您可以簡單地獲取datetime字符串

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")

最后訪問小時,分鍾等。例如,

時數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM