Python Forum

Full Version: How to convert time to sec in boucle for
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello i'm new in data science,and i find a good csv data about tv news duration by type.
i ave first to convert time to seconds:
>>> x = time.strptime('00:05:00,000'.split(',')[0],'%H:%M:%S')
>>> datetime.timedelta(hours=x.tm_hour,minutes=x.tm_min,seconds=x.tm_sec).total_seconds()
300.0
but for all my data it's more difficult:
for x in pd.read_csv("/home/me/Documents/ina-barometre-jt-tv-donnees-mensuelles-2005-2020-durees.csv",skiprows=2, usecols="C:H",index_col=0) :
    x = time.strptime(x.split(',')[0],'%H:%M:%S')
    datetime.timedelta(hours=x.tm_hour,minutes=x.tm_min,seconds=x.tm_sec).total_seconds()

ValueError: 'usecols' must either be list-like of all strings, all unicode, all integers or a callable.
    
thanks for your help. Wall
import pandas as pd 
import numpy as np 
import datetime
import time
Chronos=[]
duree =0
sumSec = 0


data = np.loadtxt("C:\\Users\\me\\Documents\\ina-barometre-jt-tv-donnees-mensuelles-2005-2020-durees.csv",skiprows=2,delimiter =';',
dtype=[('Date',np.object),('Theme',np.object),('TF1',np.object),('France_2',np.object),('France_3',np.object),('canal_+',np.object),
('Arte',np.object),('M6',np.object)])

declar = pd.DataFrame(data)

pre = declar ['M6']

for y in pre:
    x = time.strptime(y,'%H:%M:%S')
    duree=datetime.timedelta(hours=x.tm_hour, minutes=x.tm_min, seconds=x.tm_sec).total_seconds()
    Chronos.append(duree/3600)
    sumSec = sumSec + duree
    sumCH = sumSec/3600

#print('sumCH =',sumCH)  
pre2 = pd.Series(Chronos,declar['Theme'])
#print(pre2)

calc=pre2.groupby(['Theme']).sum()

print((calc/sumCH)*100)

calc.plot.pie()
OUTPUT:
[Image: mert.png]

Theme /pourcentage
Catastrophes 6.136841
Culture-loisirs 12.190127
Economie 8.240933
Education 2.635510
Environnement 3.274924
Faits divers 6.546009
Histoire-hommages 1.997193
International 8.648323
Justice 5.734435
Politique France 9.517616
Santé 6.844424
Sciences et techniques 2.222238
Société 19.354383
Sport 6.657045