2015-09-17 48 views
0

我有一个函数,输出从RINEX(GPS)文件生成的数据帧。目前,我将数据帧输出到分离的卫星(1-32)文件中。我想在第一列访问(无论是当它仍然是一个数据帧或在这些新文件),以日期格式的时间戳秒,如下图所示:时间戳从日期和格式在熊猫或csv

Epochs     Epochs 
2014-04-27 00:00:00 -> 00000 
2014-04-27 00:00:30 -> 00030 
2014-04-27 00:01:00 -> 00060 

这需要剥离的日期然后将hh:mm:ss转换为秒。我试图弄清楚如何最好地访问第一列(Epochs),然后在整个列上进行转换。我一直在努力的代码是:

def read_data(self, RINEXfile): 
    obs_data_chunks = [] 

    while True: 
     obss, _, _, epochs, _ = self.read_data_chunk(RINEXfile) 

     if obss.shape[0] == 0: 
      break 

     obs_data_chunks.append(pd.Panel(
      np.rollaxis(obss, 1, 0), 
      items=['G%02d' % d for d in range(1, 33)], 
      major_axis=epochs, 
      minor_axis=self.obs_types 
     ).dropna(axis=0, how='all').dropna(axis=2, how='all')) 

     obs_data_chunks_dataframe = obs_data_chunks[0] 

     for sv in range(32): 
      sat = obs_data_chunks_dataframe[sv, :] 
      print "sat_columns: {0}".format(sat.columns[0]) #list header of first column: L1 
      sat.to_csv(('SV_{0}').format(sv+1), index_label="Epochs", sep='\t') 

难道我就执行“坐”,或使用“to_csv”后的文件数据帧即内这种转换?我在这里有点失落。格式化列的相同问题。请参阅下面的不那么好听格式化列:

Epochs L1 L2 P1 P2 C1 S1 S2 
2014-04-27 00:00:00 669486.833 530073.33 24568752.516 24568762.572 24568751.442 43.0 38.0 
2014-04-27 00:00:30 786184.519 621006.551 24590960.634 24590970.218 24590958.374 43.0 38.0 
2014-04-27 00:01:00 902916.181 711966.252 24613174.234 24613180.219 24613173.065 42.0 38.0 
2014-04-27 00:01:30 1019689.006 802958.016 24635396.428 24635402.41 24635395.627 42.0 37.0 
2014-04-27 00:02:00 1136478.43 893962.705 24657620.079 24657627.11 24657621.828 42.0 37.0 

UPDATE: 说,我已经打了一堵墙,试图找出如何最好地访问这个第一列(时期),在“” SAT”据帧原本在其头部没有‘阶段’它只是有信号:。

L1 L2 P1 P2 C1 S1 S2 

指数(日期&时间),从头部不见了为了在我的CSV输出来克服这一文件,我“强迫”这个名字:

sat.to_csv(('SV_{0}').format(sv+1), index_label="Epochs", sep='\t') 

我希望在生成csv文件之前,我应该(但不知道如何)能够访问此索引(日期为&时间)列,并简单地将所有日期/时间转换为一次,以便时间戳被输出。

UPDATE: 历元在数据帧中产生在另一个函数为这样:

epochs = np.zeros(CHUNK_SIZE, dtype='datetime64[us]') 

UPDATE:

def read_data_chunk(self, RINEXfile, CHUNK_SIZE = 10000): 
    obss = np.empty((CHUNK_SIZE, TOTAL_SATS, len(self.obs_types)), dtype=np.float64) * np.NaN 
    llis = np.zeros((CHUNK_SIZE, TOTAL_SATS, len(self.obs_types)), dtype=np.uint8) 
    signal_strengths = np.zeros((CHUNK_SIZE, TOTAL_SATS, len(self.obs_types)), dtype=np.uint8) 
    epochs = np.zeros(CHUNK_SIZE, dtype='datetime64[us]') 
    flags = np.zeros(CHUNK_SIZE, dtype=np.uint8) 

    i = 0 
    while True: 
     hdr = self.read_epoch_header(RINEXfile) 
     #print hdr 
     if hdr is None: 
      break 
     epoch, flags[i], sats = hdr 
     epochs[i] = np.datetime64(epoch) 
     sat_map = np.ones(len(sats)) * -1 
     for n, sat in enumerate(sats): 
      if sat[0] == 'G': 
       sat_map[n] = int(sat[1:]) - 1 
     obss[i], llis[i], signal_strengths[i] = self.read_obs(RINEXfile, len(sats), sat_map) 
     i += 1 
     if i >= CHUNK_SIZE: 
      break 

    return obss[:i], llis[:i], signal_strengths[:i], epochs[:i], flags[:i] 

UPDATE:

我的道歉如果我的德定制有点含糊。其实我正在修改已经开发的代码,而且我不是软件开发人员,所以对我来说这也是一个强大的学习曲线。让我进一步解释:在“阶段”是从另一个函数read:

def read_epoch_header(self, RINEXfile): 
      epoch_hdr = RINEXfile.readline() 
      if epoch_hdr == '': 
       return None 

      year = int(epoch_hdr[1:3]) 
      if year >= 80: 
       year += 1900 
      else: 
       year += 2000 
      month = int(epoch_hdr[4:6]) 
      day = int(epoch_hdr[7:9]) 
      hour = int(epoch_hdr[10:12]) 
      minute = int(epoch_hdr[13:15]) 
      second = int(epoch_hdr[15:18]) 
      microsecond = int(epoch_hdr[19:25]) # Discard the least significant digits (use microseconds only). 
      epoch = datetime.datetime(year, month, day, hour, minute, second, microsecond) 

      flag = int(epoch_hdr[28]) 
      if flag != 0: 
       raise ValueError("Don't know how to handle epoch flag %d in epoch header:\n%s", (flag, epoch_hdr)) 

      n_sats = int(epoch_hdr[29:32]) 
      sats = [] 
      for i in range(0, n_sats): 
       if ((i % 12) == 0) and (i > 0): 
        epoch_hdr = RINEXfile.readline() 
       sats.append(epoch_hdr[(32+(i%12)*3):(35+(i%12)*3)]) 

      return epoch, flag, sats 

在上面的read_data功能,这些附加到一个数据帧。我基本上想让这个数据帧通过卫星轴分开,这样每个卫星文件在第一列中就有了这些时代,然后是下面的7个信号。中的read_data文件(如下图)代码的最后一点说明这一点:

for sv in range(32): 
      sat = obs_data_chunks_dataframe[sv, :] 
      print "sat_columns: {0}".format(sat.columns[0]) #list header of first column: L1 
      sat.to_csv(('SV_{0}').format(sv+1), index_label="Epochs", sep='\t') 

The problem here is (1) I want to have the first column as timestamps (so, strip the date, convert so midnight = 00000s and 23:59:59 = 86399s) not as they are now, and (2) ensure the columns are aligned, so I can eventually manipulate these further using a different class to perform other calculations i.e. L1 minus L2 plotted against time, etc. 
+0

对不起,'时代'已经是df中的datetime64了吗? – EdChum

+0

那么你的索引已经是日期时间或日期时间字符串了? – EdChum

+0

是'Epochs'在'sat'数据框中的索引吗? –

回答

0

这将能更快地做到这一点时,它是一个DF,如果D型是datetime64然后就转换成int64然后纳秒除以:

In [241]: 
df['Epochs'].astype(np.int64) // 10**9 

Out[241]: 
0 1398556800 
1 1398556830 
2 1398556860 
3 1398556890 
4 1398556920 
Name: Epochs, dtype: int64 

如果它是一个字符串,然后转换为使用to_datetime,然后执行上面:

df['Epochs'] = pd.to_datetime(df['Epochs']).astype(np.int64) // 10**9 

看到related

+0

索引标签(sat.to_csv(('SV_ {0}').format(sv + 1),index_label =“Epochs”,sep ='\ t'))在从dataframe转换为csv之后分配。我不知何故需要告诉数据框,以使用第一列。这没有索引。 – pymat

+0

我真的不明白你的意思,你可以编辑你的问题与原始数据和代码,创建你的DF和进一步的解释 – EdChum

+0

刚刚编辑好。 – pymat

0

我最终解决了这个问题的一部分:在read_epoch_header函数中,我只是简单地操作了一个变量,它将hh:mm:ss转换为秒,并将其用作时代。不看起来优雅,但它的作品。只需要格式化标题,使其与列对齐(并且它们也对齐)。欢呼声,pymat