关键词:
【中文标题】TypeError:float() 参数必须是字符串或数字,而不是与散点图相关的“datetime.time”【英文标题】:TypeError: float() argument must be a string or a number, not 'datetime.time' in relation with a scatter plot 【发布时间】:2021-10-29 23:27:57 【问题描述】:我创建了以下 DataFrame: date_merge 作为示例,因为我实际上想显示全天的温度值。
df_time = filtered_df_date["longtime"].dt.time
df_date = filtered_df_date["longtime"].dt.date
filtered_df_date:
index | longtime |
---|---|
52754 | 2020-01-01 00:00:00 |
52755 | 2020-01-01 00:32:00 |
52756 | 2020-01-01 00:33:00 |
... | ... |
53261 | 2020-01-01 23:59:00 |
date_merge = pd.merge(df_time, df_date, left_index=True, right_index=True)
date_merge = pd.merge(date_merge, pickd_column_df, left_index=True, right_index=True)
index | longtime_time | longtime_date | value |
---|---|---|---|
52755 | 00:32:00 | 2020-01-01 | 23.3 |
52757 | 00:34:00 | 2020-01-01 | 23.3 |
52759 | 00:37:00 | 2020-01-01 | NaN |
52760 | 00:38:00 | 2020-01-01 | NaN |
52761 | 00:39:00 | 2020-01-01 | naN |
.... | ... | ... | ... |
53261 | 23:59:00 | 2020-01-01 | 23.9 |
现在我以 x 轴上的 longtime_date 为例:
ax = date_merge.plot(x ="longtime_date" , y="value" , kind="scatter" ,figsize=[15, 5], linewidth=0.1, alpha=0.6, color="#003399")
plt.show()
它没有错误。 如果我现在使用 longtime_time 而不是 longtime_date 作为 x 轴,我会收到以下错误消息
ax = date_merge.plot(x ="longtime_time" , y="value" , kind="scatter" ,figsize=[15, 5], linewidth=0.1, alpha=0.6, color="#003399")
plt.show()
TypeError: float() 参数必须是字符串或数字,而不是 'datetime.time'
更多信息:
print(date_merge["longtime_time"].dtype)
输出:
对象
print(date_merge["longtime_date"].dtype)
输出:
对象
print(date_merge["temperature_ers_lite_1_wermser_0_elsys_0"].dtype)
输出:
float64
【问题讨论】:
你能提供数据集吗? 看起来您需要做的是将longtime_time
列从datetime.time
转换为str
是的,这可以工作
在您的原始帖子中发布
是的,那行得通。您可以将刻度标签更改为仅显示时间。给我几分钟,我会在此处将其添加到解决方案中。
【参考方案1】:
好的,所以我认为问题是您需要将该列转换为 str
。
所以在绘图之前的某个时间点:
date_merge['longtime_time'] = date_merge['longtime_time'].astype(str)
ax = date_merge.plot(x ="longtime_time" , y="value" , kind="scatter" ,figsize=[15, 5], linewidth=0.1, alpha=0.6, color="#003399")
plt.show()
或者可以这样做:
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.dates as mdates
date_merge = pd.DataFrame(
[['2020-01-01 00:32:00' ,23.3],
['2020-01-01 00:34:00' ,23.3],
['2020-01-01 00:37:00' ,np.nan],
['2020-01-01 00:38:00' ,np.nan],
['2020-01-01 00:39:00' ,np.nan],
['2020-01-01 23:59:00' ,23.9]],
columns = ['longtime' ,'value'])
date_merge["longtime"] = pd.to_datetime(date_merge["longtime"])
ax = date_merge.plot(x ="longtime" , y="value" , kind="scatter" ,figsize=[15, 5], linewidth=0.1, alpha=0.6, color="#003399")
timeFmt = mdates.DateFormatter('%H:%M:%S')
ax.xaxis.set_major_formatter(timeFmt)
plt.xticks(rotation=90)
plt.show()
【讨论】:
TypeError:float() 参数必须是字符串或数字,而不是“期间”
】TypeError:float()参数必须是字符串或数字,而不是“期间”【英文标题】:TypeError:float()argumentmustbeastringoranumber,not\'Period\'【发布时间】:2017-08-2914:43:53【问题描述】:我有一个带有这样列的pandas数据框:df.columns=pd.to_datetime(list(... 查看详情
TypeError:float() 参数必须是字符串或数字,而不是“SimpleImputer”
】TypeError:float()参数必须是字符串或数字,而不是“SimpleImputer”【英文标题】:TypeError:float()argumentmustbeastringoranumber,not\'SimpleImputer\'【发布时间】:2022-01-0901:25:27【问题描述】:all_data=pd.concat([train_data,test_data])average=all_data.Age.me 查看详情
TypeError:float() 参数必须是字符串或数字,而不是“时间戳”
】TypeError:float()参数必须是字符串或数字,而不是“时间戳”【英文标题】:TypeError:float()argumentmustbeastringoranumber,not\'Timestamp\'【发布时间】:2019-07-0715:07:18【问题描述】:我有一堆数据,我正在从这样的CSV文件读取到Pandas中,... 查看详情
TypeError:float() 参数必须是字符串或数字,而不是 'tuple' ,ValueError:使用序列设置数组元素
】TypeError:float()参数必须是字符串或数字,而不是\\\'tuple\\\',ValueError:使用序列设置数组元素【英文标题】:TypeError:float()argumentmustbeastringoranumber,not\'tuple\',ValueError:settinganarrayelementwithasequenceTypeError:float()参数必须是字符串或... 查看详情
TypeError: float() 参数必须是字符串或数字,而不是“函数”——Python/Sklearn
】TypeError:float()参数必须是字符串或数字,而不是“函数”——Python/Sklearn【英文标题】:TypeError:float()argumentmustbeastringoranumber,not\'function\'–Python/Sklearn【发布时间】:2018-02-2610:19:22【问题描述】:我有一个名为Flights.py的程序的... 查看详情
Pandas :TypeError: float() 参数必须是字符串或数字,而不是 'pandas._libs.interval.Interval'
】Pandas:TypeError:float()参数必须是字符串或数字,而不是\\\'pandas._libs.interval.Interval\\\'【英文标题】:Pandas:TypeError:float()argumentmustbeastringoranumber,not\'pandas._libs.interval.Interval\'Pandas:TypeError:float()参数必须是字符串或数字,而不是\'pa 查看详情
TypeError:float() 参数必须是字符串或数字,而不是“列表”和 ValueError:使用序列设置数组元素
】TypeError:float()参数必须是字符串或数字,而不是“列表”和ValueError:使用序列设置数组元素【英文标题】:TypeError:float()argumentmustbeastringoranumber,not\'list\'andValueError:settinganarrayelementwithasequence【发布时间】:2022-01-0601:22:32【问... 查看详情
我该如何解决这个错误? TypeError:float() 参数必须是字符串或数字,而不是“时间戳”
】我该如何解决这个错误?TypeError:float()参数必须是字符串或数字,而不是“时间戳”【英文标题】:HowdoIsolvethiserror?TypeError:float()argumentmustbeastringoranumber,not\'Timestamp\'【发布时间】:2021-11-1519:22:43【问题描述】:对于下面的代... 查看详情
TypeError:float() 参数必须是字符串或数字,而不是与散点图相关的“datetime.time”
】TypeError:float()参数必须是字符串或数字,而不是与散点图相关的“datetime.time”【英文标题】:TypeError:float()argumentmustbeastringoranumber,not\'datetime.time\'inrelationwithascatterplot【发布时间】:2021-10-2923:27:57【问题描述】:我创建了以下... 查看详情
LabelEncoder -- TypeError: 参数必须是字符串或数字
】LabelEncoder--TypeError:参数必须是字符串或数字【英文标题】:LabelEncoder--TypeError:argumentmustbeastringornumber【发布时间】:2020-09-1118:07:09【问题描述】:importpandasaspdfromsklearn.preprocessingimportLabelEncoderpd.set_option(\'display.max_colum 查看详情
TypeError:int() 参数必须是字符串或数字,而不是 'datetime.datetime'
】TypeError:int()参数必须是字符串或数字,而不是\\\'datetime.datetime\\\'【英文标题】:TypeError:int()argumentmustbeastringoranumber,not\'datetime.datetime\'TypeError:int()参数必须是字符串或数字,而不是\'datetime.datetime\'【发布时间】:2015-12-0302:28... 查看详情
TypeError:参数必须是字符串或数字列上的字符串或数字
】TypeError:参数必须是字符串或数字列上的字符串或数字【英文标题】:TypeError:argumentmustbeastringornumberoncolumnwithstringsthatarenumbers【发布时间】:2020-10-2713:15:40【问题描述】:我有一个包含类别的数据集。在第4列中,我有2个值(... 查看详情
Gulp TypeError:path.join 的参数必须是字符串
】GulpTypeError:path.join的参数必须是字符串【英文标题】:GulpTypeError:Argumentstopath.joinmustbestrings【发布时间】:2015-03-2406:27:49【问题描述】:我对gulp-ruby-sass有疑问。当我尝试运行watch任务并更改一些.sass文件时,会出现错误:TypeEr... 查看详情
Django TypeError int() 参数必须是字符串或数字,而不是 'QueryDict'
】DjangoTypeErrorint()参数必须是字符串或数字,而不是\\\'QueryDict\\\'【英文标题】:DjangoTypeErrorint()argumentmustbeastringoranumber,not\'QueryDict\'DjangoTypeErrorint()参数必须是字符串或数字,而不是\'QueryDict\'【发布时间】:2014-02-2605:24:14【问题... 查看详情
node-websocket-server TypeError:参数必须是字符串
】node-websocket-serverTypeError:参数必须是字符串【英文标题】:node-websocket-serverTypeError:Argumentmustbeastring【发布时间】:2011-04-2221:32:50【问题描述】:我在ubuntu上跟踪/var/log/logfile,并得到一个TypeError。适用于我的本地Mac机器/var/log/s... 查看详情
为啥在带有字符串和 NaN 的系列上得到“TypeError:'float' 类型的参数不可迭代”?
】为啥在带有字符串和NaN的系列上得到“TypeError:\\\'float\\\'类型的参数不可迭代”?【英文标题】:Whydogeta"TypeError:argumentoftype\'float\'isnotiterable"onaserieswithstringsandNaN?为什么在带有字符串和NaN的系列上得到“TypeError:\'float... 查看详情
TypeError: int() 参数必须是字符串、类似字节的对象或实数,而不是“NoneType”
】TypeError:int()参数必须是字符串、类似字节的对象或实数,而不是“NoneType”【英文标题】:TypeError:int()argumentmustbeastring,abytes-likeobjectorarealnumber,not\'NoneType\'【发布时间】:2021-12-3006:34:09【问题描述】:我正在从这个youtubeecommercet... 查看详情
TypeError [ERR_INVALID_ARG_TYPE]:“from”参数必须是字符串类型
】TypeError[ERR_INVALID_ARG_TYPE]:“from”参数必须是字符串类型【英文标题】:TypeError[ERR_INVALID_ARG_TYPE]:The"from"argumentmustbeoftypestring【发布时间】:2021-12-2409:05:33【问题描述】:我正在尝试运行黄瓜测试,但是出现以下错误TypeE... 查看详情