在python中创建一个时间戳数组

时间:2019-03-25 18:05:14

标签: python python-3.x datetime time

我一直试图在python 3.x中创建一个时间矢量,以绘制风速与时间的关系。
从00:00开始到23:55结束,每5分钟采样一次风速(即每天288个样本)。

我使用pandas读取数据。我将速度变量存储在numpy向量中。我使用numpy.array(met1.loc[ini:fin, 'm/s Wind'])保存了变量,并且非常适合风速。当我尝试对datetime变量执行此操作时,如numpy.array(met1.loc[ini:fin, 'Measurement Time'])向我展示了下一个错误:

'the label [Measurement Time] is not in the [columns]'(请参阅后面的代码以获取更多详细信息)

met1.head()看起来像这样:

 .                  m/s Wind  m/s Gusts  ° Direction        RH  °C Temp  \
Measurement Time                                                           
2018-11-13 11:20:00      6.77        9.5          268  0.068590     12.6   
2018-11-13 11:25:00      6.90       10.4          284  0.067255     12.9   
2018-11-13 11:30:00      6.59       11.3          279  0.066381     13.1   
2018-11-13 11:35:00      7.10       10.1          279  0.073019     13.1   
2018-11-13 11:40:00      6.01        9.8          273  0.073498     13.0

代码如下:

import numpy as np
import pandas as pd

met1 = pd.read_excel('database.xls', sheet_name=0, skiprows=2, index_col = 'Measurement Time', parse_dates=[0]) #reading data base

def variables (ini, fin):
# Variables for MET-1
    wind1 = np.array(met1.loc[ini:fin, 'm/s Wind'])
    dir1 = np.array(met1.loc[ini:fin, '° Direction'])
    rh1 = np.array(met1.loc[ini:fin, 'RH']) # 0.123
    temp1 = np.array(met1.loc[ini:fin, '°C Temp'])
    p1 = np.array(met1.loc[ini:fin, 'kPa Pressure'])
    tiempo = np.array(met1.loc[ini:fin, 'Measurement Time'])
    print(wind1)
    print(tiempo)

variables('2018-11-15','2018-11-15')
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py in _validate_key(self, key, axis)
   1789                 if not ax.contains(key):
-> 1790                     error()
   1791             except TypeError as e:

~/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py in error()
   1784                                .format(key=key,
-> 1785                                        axis=self.obj._get_axis_name(axis)))
   1786 

KeyError: 'the label [Measurement Time] is not in the [columns]'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-182-130dc8c7095b> in <module>
      1 # variables('2018-11-13','2018-11-13') # Mount day
      2 # variables('2018-11-14','2018-11-14') # Mount day
----> 3 variables('2018-11-15','2018-11-15')
      4 variables('2018-11-16','2018-11-16')
      5 variables('2018-11-17','2018-11-17')

<ipython-input-181-2a7e1e919435> in variables(ini, fin)
     17     temp1 = np.array(met1.loc[ini:fin, '°C Temp'])
     18     p1 = np.array(met1.loc[ini:fin, 'kPa Pressure'])
---> 19     tiempo = np.array(met1.loc[ini:fin, 'Measurement Time'])
     20     print(tiempo)
     21 

~/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py in __getitem__(self, key)
   1470             except (KeyError, IndexError):
   1471                 pass
-> 1472             return self._getitem_tuple(key)
   1473         else:
   1474             # we by definition only have the 0th axis

~/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py in _getitem_tuple(self, tup)
    868     def _getitem_tuple(self, tup):
    869         try:
--> 870             return self._getitem_lowerdim(tup)
    871         except IndexingError:
    872             pass

~/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py in _getitem_lowerdim(self, tup)
    996         for i, key in enumerate(tup):
    997             if is_label_like(key) or isinstance(key, tuple):
--> 998                 section = self._getitem_axis(key, axis=i)
    999 
   1000                 # we have yielded a scalar ?

~/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py in _getitem_axis(self, key, axis)
   1909 
   1910         # fall thru to straight lookup
-> 1911         self._validate_key(key, axis)
   1912         return self._get_label(key, axis=axis)
   1913 

~/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py in _validate_key(self, key, axis)
   1796                 raise
   1797             except:
-> 1798                 error()
   1799 
   1800     def _is_scalar_access(self, key):

~/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py in error()
   1783                 raise KeyError(u"the label [{key}] is not in the [{axis}]"
   1784                                .format(key=key,
-> 1785                                        axis=self.obj._get_axis_name(axis)))
   1786 
   1787             try:

KeyError: 'the label [Measurement Time] is not in the [columns]'

我需要的时间向量应如下所示!: ['00:00' '00:05' ..... '23:50' '23:55']用于日期'2018-11-15'

0 个答案:

没有答案