If want plot timedelta
, now it is not implemented, so possible solution is convert time
column to total_seconds
:
import pandas as pd
from pandas.compat import StringIO
temp=u"""time;Watt
00:00:00;0
01:00:00;0
02:00:00;0
03:00:00;0
04:00:00;0
05:00:00;45
06:00:00;56
07:00:00;88"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
converter = {'time':lambda x: pd.to_timedelta(x)}
df = pd.read_csv(StringIO(temp), sep=";", converters=converter)
df['time_in_sec'] = df['time'].dt.total_seconds()
print (df)
time Watt time_in_sec
0 00:00:00 0 0.0
1 01:00:00 0 3600.0
2 02:00:00 0 7200.0
3 03:00:00 0 10800.0
4 04:00:00 0 14400.0
5 05:00:00 45 18000.0
6 06:00:00 56 21600.0
7 07:00:00 88 25200.0
df.plot(x='time_in_sec', y='Watt')
Another solution:
import pandas as pd
from pandas.compat import StringIO
temp=u"""time;Watt
00:00:00;0
01:00:00;0
02:00:00;0
03:00:00;0
04:00:00;0
05:00:00;45
06:00:00;56
07:00:00;88"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp), sep=";")
df['time_in_sec'] = pd.to_timedelta(df['time']).dt.total_seconds()
print (df)
time Watt time_in_sec
0 00:00:00 0 0.0
1 01:00:00 0 3600.0
2 02:00:00 0 7200.0
3 03:00:00 0 10800.0
4 04:00:00 0 14400.0
5 05:00:00 45 18000.0
6 06:00:00 56 21600.0
7 07:00:00 88 25200.0
df.plot(x='time_in_sec', y='Watt')
Solution with parsing timedelta
as datetime
in read_csv
and then convert to time
:
import pandas as pd
import numpy as np
from pandas.compat import StringIO
temp=u"""time;Watt
00:00:00;0
01:00:00;0
02:00:00;0
03:00:00;0
04:00:00;0
05:00:00;45
06:00:00;56
07:00:00;88"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp), sep=";", parse_dates=['time'])
df['time'] = df['time'].dt.time
print (df)
time Watt
0 00:00:00 0
1 01:00:00 0
2 02:00:00 0
3 03:00:00 0
4 04:00:00 0
5 05:00:00 45
6 06:00:00 56
7 07:00:00 88
df.plot(x='time', y='Watt')
---
Another possible solution is set index in read_csv
:
import pandas as pd
from pandas.compat import StringIO
temp=u"""time;Watt
00:00:00;0
01:00:00;0
02:00:00;0
03:00:00;0
04:00:00;0
05:00:00;45
06:00:00;56
07:00:00;88"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp), sep=";", index_col=['time'])
df.index = pd.to_timedelta(df.index).total_seconds()
print (df)
Watt
time
0.0 0
3600.0 0
7200.0 0
10800.0 0
14400.0 0
18000.0 45
21600.0 56
25200.0 88
df['Watt'].plot()
import pandas as pd
from pandas.compat import StringIO
temp=u"""time;Watt
00:00:00;0
01:00:00;0
02:00:00;0
03:00:00;0
04:00:00;0
05:00:00;45
06:00:00;56
07:00:00;88"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp), sep=";", parse_dates=True, index_col=['time'])
df.index = df.index.time
print (df)
Watt
00:00:00 0
01:00:00 0
02:00:00 0
03:00:00 0
04:00:00 0
05:00:00 45
06:00:00 56
07:00:00 88
df['Watt'].plot()