带有日期时间对象的 Seaborn 回归图

Posted

技术标签:

【中文标题】带有日期时间对象的 Seaborn 回归图【英文标题】:Seaborn regression plot with datetime object 【发布时间】:2021-09-19 06:30:30 【问题描述】:

我正在尝试使用datetime.time 对象在多个神经的振幅和它们在试验中使用的时间之间生成回归/散点图。

>>> df.head()
   Amp strain 8 min [mA] Time in use [hh:mm]
0                    0.1            00:22:00
1                    0.0            00:46:00
2                    0.8            00:18:00
3                    0.3            00:23:00
4                    0.6            00:14:00

但是sns.regplot(y=df.iloc[:, 1], x=df.iloc[:, 0]) 会导致

TypeError: float() argument must be a string or a number, not 'datetime.time'

我尝试了从here 到there 的所有内容,以及我无法再找到但无法找到的其他几个链接。有什么我还没看过的吗?

【问题讨论】:

试试sns.regplot(y=df.iloc[:, 0], x=df.iloc[:, 1]) 虽然这会创建一个空图,但错误仍然存​​在 【参考方案1】:

我解决了

    converting the datetime object to string format replacing the colons converting the series to a numeric value

代码:

df["Time in use [hh:mm]"] = pd.to_numeric(df["Time in use [hh:mm]"].astype(str).str.replace(":", ""))
sns.regplot(y=df["Time in use [hh:mm]"], x=df["Amp strain 8 min [mA]"])

这会产生 SettingWithCopyWarning 和 can be disabled,但我不确定这是否可取。

【讨论】:

以上是关于带有日期时间对象的 Seaborn 回归图的主要内容,如果未能解决你的问题,请参考以下文章