首页 文章

Pandas中的错误以秒为单位计算时间差

提问于
浏览
0

我试图通过熊猫框架提取时差(秒) . 我在via文本文件中读取数据 . 但是在我应用diff函数后对数据进行分组后,我收到了错误 .

#load data
# this format loads file when there is a 'tab' delimiter in the text file
data = pd.read_csv(file, sep='\t', lineterminator='\n')

# filter data by desired field, traded venues are XLON_SET1, _BATE, _CHIX, _TRQX, XOFF_SET1 etc
dataFil = data[data['VENUE'] == "XLON_SET1"]
# then we need to group them by time-stamp to be sure, to clean up the time-series. This will cause TIME_STAMP and PRICE to become index instead of columns with data
dataFil = dataFil.groupby(['TIME_STAMP', 'PRICE']).sum()
#dataFil = dataFil.groupby(['TIME_STAMP']).sum()

dataFil['date'] = dataFil.index.get_level_values('TIME_STAMP')
dataFil['PRICE'] = dataFil.index.get_level_values('PRICE')
dataFil.head() #or dataFil

我得到以下数据

QUANTITY BID询问MKT_BID MKT_ASK日期价格TIME_STAMP价格2018-01-22 08:30:01.306 2.769 3409 0.0 0.0 0.0 0.0 2018-01-22 08:30:01.306 2.769 2018-01-22 08:30:04.306 2.769 2691 0.0 0.0 0.0 0.0 2018-01-22 08:30:04.306 2.769 2018-01-22 08:30:11.306 2.769 2000 0.0 0.0 0.0 0.0 2018-01-22 08:30:11.306 2.769 2018-01-22 08:30: 51.065 2.769 572 0.0 0.0 0.0 0.0 2018-01-22 08:30:51.065 2.769 2018-01-22 08:31:26.068 2.768 649 0.0 0.0 0.0 0.0 2018-01-22 08:31:26.068 2.768

但是当我使用:(检查过这个帖子:Pandas calculate time difference

df = dataFil
df.assign(seconds=df.date.diff().dt.seconds)

我有以下错误

TypeError                                 Traceback (most recent call last)
<ipython-input-170-3be32e0aad41> in <module>()
      1 df = dataFil
----> 2 df.assign(seconds=df.date.diff().dt.seconds)

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\series.py in diff(self, periods)
   1525         diffed : Series
   1526         """
-> 1527         result = algorithms.diff(_values_from_object(self), periods)
   1528         return self._constructor(result, index=self.index).__finalize__(self)
   1529 

C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\algorithms.py in diff(arr, n, axis)
   1545             out_arr[res_indexer] = result
   1546         else:
-> 1547             out_arr[res_indexer] = arr[res_indexer] - arr[lag_indexer]
   1548 
   1549     if is_timedelta:

TypeError: unsupported operand type(s) for -: 'str' and 'str'

1 回答

  • 1

    我认为需要将 date 转换为 datetime s - 最好在read_csv

    data = pd.read_csv(file, sep='\t', lineterminator='\n', paarse_dates=['TIME_STAMP'])
    

    或者通过to_datetime转换列:

    df.assign(seconds=pd.to_datetime(df.date).diff().dt.seconds)
    

相关问题