【问题标题】:ValueError: could not convert string to float - sns.tsplot, time - using strings to label x axisValueError:无法将字符串转换为浮点数 - sns.tsplot,时间 - 使用字符串标记 x 轴
【发布时间】:2018-03-12 02:49:47
【问题描述】:

文件“输入”(错误的列,这就是我在read_csv 中使用header =0 的原因)

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

代码

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)

目前,该图仅沿 x 轴按顺序显示数字。 将 y 轴标记为当前显示的值并不完全正确,因为对应于 x = 4、5、6 的值是标准偏差。但撇开这一点不谈,有没有办法用字符串标记 x 轴?我注意到可以使用日期/时间这样做。

以下是我目前收到的错误。奇怪的是,ValueError 会出现在read.columns.values.tolist() 列表的第 6 项。但是这仍然不起作用。有没有人对此事有所了解?我非常重视,谢谢。

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'

【问题讨论】:

  • 您能否添加几行 CSV 文件以便复制问题?
  • 感谢您的想法。我更新了。

标签: python time-series seaborn axis-labels


【解决方案1】:

时间只是不接受列表中的字符串。 我确实找到了使用 xticks 的解决方法,以便将字符串用作 x 轴的标签。

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-12
    • 2019-03-23
    • 2018-06-13
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多