简体   繁体   中英

Error concatenating datetime objects in an array

I'm developing a software to create a matrix with this structure: [[2020-07-14 13:01:58.535695, 9.013799869442407, 989.5936121308639], [2020-07-14 14:40:05.144901, 59.27540855766542, 463.4158524443841]]

To build the first line, the array should contains: [2020-07-14 13:01:58.535695,2020-07-14 14:40:05.144901]

To concatenate these datetime I do this:

x_vet = []
        for z in range(self.n_samples):
            self.x_0 = datetime.now()
            totaltime = self.x_0 + timedelta(milliseconds = self.period)
            x_vet.append(self.x_0 + totaltime)
        self.signals_data.append(x_vet)

The error is:

TypeError: unsupported operand type(s) for +: 'datetime.datetime' and 'datetime.datetime'

So I've tried to do this in the code:

x_vet.append(str(self.x_0) + str(totaltime))

But with str() I get only 1 element like that: '2020-07-14 14:35:44.0007892020-07-14 14:35:44.010789'

I want to get this in x_vet : [2020-07-14 13:01:58.535695,2020-07-14 14:40:05.144901] Because the whole software is build to recognize x_vet as an array of 2 elements

This error appears only with datatime object, because at the start I've tried my program with float and was working and returned:

[[0.0, 9.013799869442407, 989.5936121308639], [0.01, 59.27540855766542, 463.4158524443841]]

Did you mean this?

x_vet = []

for z in range(self.n_samples):
    self.x_0 = datetime.now()
    totaltime = self.x_0 + timedelta(milliseconds = self.period)
    x_vet.append(totaltime)
self.signals_data.append(x_vet)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM