0

So i read a *.csv file with Pandas, but if I want to plot it, it shows me the following error,

ValueError: could not convert string to float: '23:00:00'

I read and print the following *.csv file with,

df1 = pd.read_csv(Location_l, sep=";") 
df1

Here the output

I tried to change 'time' to timedelta but it still woulnd't plot it.

How do I convert 'time' in order to plot df1?

Thanks!

mforpe
  • 1,549
  • 1
  • 12
  • 22
harald12345
  • 141
  • 1
  • 9

2 Answers2

0

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()
Community
  • 1
  • 1
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

might be because you have null or N/A data in your dataframe You want to use " df2.fillna(0) "

Fred
  • 1