Search code examples
pythontime-seriesseabornaxis-labels

ValueError: could not convert string to float - sns.tsplot, time - using strings to label x axis


file 'input' (wrong columns, which is why I use header =0 in read_csv)

meter   overnight   breakfast   daytime evening std-year    season-score    week-score
0   1002.0  0.56    1.16    1.14    1.32    5.04    0.5 1.26
1   1003.0  0.74    0.96    0.87    1.44    2.62    0.19    1.08
2   1004.0  0.65    0.53    0.85    1.71    3.32    0.48    0.36
3   1005.0  0.49    1.22    1.09    1.42    3.57    0.56    2.54
4   1008.0  0.87    0.76    0.95    1.28    3.36    0.42    1.69

code

toc = time.time()
#header = 0 replaces the headers with names specified
read = pd.read_csv('input', sep='\t', index_col= 0, header =0, names =['meter', 'overn_PR', 'breakf_PR', 'day_PR', 'evng_PR', 'std_year', 'week_score', 'season_score'], encoding= 'utf-8')
read.drop('meter', 1, inplace=True)
read['std_year'] = round(read['std_year']/4, 2)
print(read.columns.values.tolist())

input = read.as_matrix()

sns.tsplot(data=input, err_style='unit_traces',\
       value = 'energy use [kWh]',\
        time = read.columns.values.tolist())
plt.show()

tic = time.time()
print(tic-toc)

enter image description here Currently the plot only shows numbers in sequence along the x axis. It's not entirely correct to label y axis as what's shown currently as the values corresponding to x = 4, 5, 6 are standard deviations.. but leaving that aside, is there a way to label x axis with strings? I noticed it is possible to do so with date/times.

Following are the error I receive at the moment. It is odd that the ValueError would be incurred at the 6th item of the read.columns.values.tolist() list..but nonetheless this is not working. Has anyone got insight into the matter? I'd deeply value, thanks.

Traceback (most recent call last):
  File "/Users/hsl/work_dir/eclipse/ISSDA/src/GMM_ppr.py", line 32, in <module>
  time = read.columns.values.tolist())
  File "/anaconda/lib/python3.5/site-packages/seaborn/timeseries.py", line 280, in tsplot
  x = df_c.columns.values.astype(np.float)
ValueError: could not convert string to float: 'week_score'

Solution

  • Time just didn't accept the strings in the list. I did find a workaround using xticks in order to use strings as labels for the x-axis.

    toc = time.time()
    #header = 0 replaces the headers with names specified
    read = pd.read_csv('input', sep='\t', index_col= 0, header =0, names =['meter', 'overn_PR', 'breakf_PR', 'day_PR', 'evng_PR', 'std_year', 'week_score', 'season_score'], encoding= 'utf-8')
    read.drop('meter', 1, inplace=True)
    read['std_year'] = round(read['std_year']/4, 2)
    print(read.columns.values.tolist())
    
    input = read.as_matrix()
    string_val = read.columns.values.tolist()
    
    sns.tsplot(data=input, err_style='unit_traces',\
           value = 'energy use [kWh]')
    plt.xticks(range(7),string_val) #sets the x axis with the supplied list
    
    plt.show()
    
    tic = time.time()
    print(tic-toc)