我正在尝试通过熊猫帧提取时间差(秒)。我通过文本文件读取数据。但是在应用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 ASK MKT_BID MKT_ASK date PRICE TIME_STAMP PRICE
2018-01-22 08:30:01.306 2.769 3409 0.0 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 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'
答案 0 :(得分:1)
我认为需要将列use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
/**
* @inheritdoc
*/
public function configureFormFields(FormMapper $formMapper) {
parent::configureFormFields($formMapper);
$subject = $this->getSubject();
if (null === $subject->getId()) {
$subject->setType('permanent');
}
$formMapper->add('type', ChoiceType::class, [
'label' => 'config.label_type',
'choices' => [
'config.label_permanent' => 'permanent',
'config.label_automatic' => 'automatic',
'config.label_temporary' => 'temporary'
],
'required' => false
]);
}
转换为date
s-最好在read_csv
:
datetime
或按to_datetime
转换列:
data = pd.read_csv(file, sep='\t', lineterminator='\n', paarse_dates=['TIME_STAMP'])