Python 在使用pandas读取文件时的Datetime标头

Python 在使用pandas读取文件时的Datetime标头,python,datetime,pandas,Python,Datetime,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

到目前为止,我有一个类似这样的数据集:

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
import pandas as pd
import glob

filename = glob.glob('1511??.mnd')
data_nov15_hereford = pd.DataFrame()
frames = []
dates = []

for i in filename:
   f_nov15_hereford = pd.read_csv(i, skiprows = 32, sep='\s+')
counter = 1
for line in i:
   if counter % 31 == 0:
   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)

hub_wspd = data_nov15_hereford[data_nov15_hereford['#'] == 80].z
在这种情况下,一整天中每隔十分钟,它就会被分成这样的数据块,并带有日期-时间标题。我想读入这些标题并将其存储为时间变量。到目前为止,我是这样读的:

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
import pandas as pd
import glob

filename = glob.glob('1511??.mnd')
data_nov15_hereford = pd.DataFrame()
frames = []
dates = []

for i in filename:
   f_nov15_hereford = pd.read_csv(i, skiprows = 32, sep='\s+')
counter = 1
for line in i:
   if counter % 31 == 0:
   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)

hub_wspd = data_nov15_hereford[data_nov15_hereford['#'] == 80].z

skiprows语句目前正在跳过这些时间头。我有一个变量hub_wspd,我对它和时间头都感兴趣。有没有一种方法可以读入这些时间头并将它们存储为变量,而不会弄乱我已经做过的事情?谢谢

如果要将它们存储为datetime,可以编写一个小方法,在解析标题行后返回
datetime
变量

像这样的

import 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]
    min = split_string[1].split(':')[1]
    second = split_string[1].split(':')[2]

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

编辑-完全取消计数器,只需查找以“20”开头的行:

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]
    minute = 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(minute), second=int(second))

filename = open('./data.txt', 'r')
frames = []
dates = []
counter = 1

for line in filename:
    # print(counter % 31)
    if line.startswith('20'):
        print(line)
        dates.append(parse_date(line))
    # frames.append(f_nov15_hereford) 

print(dates)

好的,那么对于我的程序,我现在跳过循环中读取文件的那一行。所以我可以跳过32行而不是33行,但是我如何告诉函数读取第一行的字符串呢?如果你逐行读取一个文件,那么你可以将第一行传递给该方法。这种方法有什么好运气吗?老实说,我遇到了一些困难。这主要是因为我通过循环读取多个文件。所以我想我不是一行一行地读。因此,我很难弄清楚如何在循环中读取的每个文件中获取第一行,然后设置chunksize,我猜这是因为在循环中读取的每个文件中,日期时间头都以块的形式出现(我的示例数据文件如上图所示)。这就是我现在被困的地方。这有意义吗?基本上,我不想只读第一行,我想读第一行,然后跳过30行,读那一行,跳过30行,读那一行……等等。然后将这些行存储为变量,我可以将其读入datetime函数以转换为日期。