Home > Blockchain >  How to get rid of word 'Timestamp' and tuple while appending datetime from csv?
How to get rid of word 'Timestamp' and tuple while appending datetime from csv?

Time:12-17

I am trying to make a list of datetime from csv to match my result with detected LSTM test.

Whenever I use loop to extract datetime and value I always get non string word 'Timestamp' with datetime.

my code is here:

m = [ 49,  50] #index of results which I have to match with datetime to extract all information

result_details = []
for index, rows in df.iterrows():
    if index in m:
        result_details.append([rows[0],
                                rows[1]])
print(result_details)

My csv:

           datetimeAt             value
0       2021-12-01 00:00:00       0.000
5       2021-12-01 01:00:00       0.000
10      2021-12-01 02:00:00       0.000
15      2021-12-01 03:00:00       0.000
20      2021-12-01 04:00:00       0.000
...             ...                ...
1149    2021-12-10 13:00:00       2.756
1154    2021-12-10 14:00:00       1.297
1159    2021-12-10 15:00:00       1.503
1164    2021-12-10 16:00:00       1.417
1169    2021-12-10 17:00:00       0.084

Whenever I append I get out put like this :

[[Timestamp('2021-12-01 10:00:00'), 13.266044921875], 
[Timestamp('2021-12-01 09:00:00'), 9.5365595703125]]

How do I get output like below? (only date and value without tuple, string and words)

[2021-12-01 10:00:00, 13.266044921875], 
[2021-12-01 09:00:00, 9.5365595703125]]

CodePudding user response:

Because the 'datetimeAt' column as timestamp data-type. You can cast the type to string and then it would work, something like this:

import pandas as pd
import numpy as np
from datetime import datetime, timedelta

m = [49, 50, 60] #index of results which I have to match with datetime to extract all information
date_today = datetime.now()
days = pd.date_range(date_today, date_today   timedelta(7), freq='h')

data = np.random.random(size=len(days))
df = pd.DataFrame({'datetimeAt': days, 'value': data})

result_details = []
for index, rows in df.iterrows():
    if index in m:
        result_details.append([str(rows[0]),
                                rows[1]])
print(result_details)

Note: You should avoid for loops when dealing with dataframes as much as possible, because if you got a bit table then it can get slow. Instead, you can use vectorized methods which is very efficient, e.g.:

import pandas as pd
import numpy as np
from datetime import datetime, timedelta

m = [49, 50] #index of results which I have to match with datetime to extract all information
date_today = datetime.now()
days = pd.date_range(date_today, date_today   timedelta(7), freq='h')

data = np.random.random(size=len(days))
df = pd.DataFrame({'datetimeAt': days, 'value': data})

df['datetimeAt'] = df['datetimeAt'].astype(str)
result_details = df.loc[m,:].values.tolist()
print(result_details)

output:

[['2021-12-18 07:11:58.086250', 0.3699851325750202], 
['2021-12-18 08:11:58.086250', 0.6787871001450321]]

EDIT: Test it with

pandas                    1.3.5
pandas                    0.23.4  
python 3.7 & 3.9
  • Related