Pandas 如何框式绘制熊猫时间戳系列?(时间戳类型错误)

Pandas 如何框式绘制熊猫时间戳系列?(时间戳类型错误),pandas,matplotlib,timestamp,seaborn,boxplot,Pandas,Matplotlib,Timestamp,Seaborn,Boxplot,我正在使用: 熊猫版本0.23.0 Python版本3.6.5 Seaborn版本0.81.1 我想要一列时间戳数据的方框图。我的数据帧不是一个时间序列,索引只是一个整数,但我使用以下方法创建了一列时间戳数据: # create a new column of time stamps corresponding to EVENT_DTM data['EVENT_DTM_TS'] =pd.to_datetime(data.EVENT_DTM, errors='coerce') 我过滤掉强制产生的

我正在使用:

熊猫版本0.23.0

Python版本3.6.5

Seaborn版本0.81.1

我想要一列时间戳数据的方框图。我的数据帧不是一个时间序列,索引只是一个整数,但我使用以下方法创建了一列时间戳数据:

# create a new column of time stamps corresponding to EVENT_DTM
data['EVENT_DTM_TS'] =pd.to_datetime(data.EVENT_DTM, errors='coerce')
我过滤掉强制产生的所有NaT值

dt_filtered_time = data[~data.EVENT_DTM_TS.isnull()]
在这一点上,我的数据看起来不错,我可以确认EVENT_DM_TS列的类型是Timestamp,没有无效值

最后,要生成我调用的单变量方框图:

ax = sns.boxplot(x=dt_filtered_time.EVENT_DTM_TS)
并获取错误:

TypeError:ufunc add无法使用dtype('M8[ns]')和dtype('M8[ns]')类型的操作数

我在谷歌上搜索发现:

这似乎表明数据类型表示存在问题

我还看到了有关pandas版本0.21.0的问题


任何人都有简单的修复建议,或者我需要使用不同的数据类型来绘制方框图。我想获得时间戳数据分布的单一图片。

这是我最后得到的代码:

import time
@plt.FuncFormatter
def convert_to_date_string(x,pos):
    return time.strftime('%Y-%m',time.localtime(x))


plt.figure(figsize=(15,4))
sns.set(style='whitegrid')
temp = dt_filtered_time.EVENT_DTM_TS.astype(np.int64)/1E9
ax = sns.boxplot(x=temp)
ax.xaxis.set_major_formatter(convert_to_date_string)
结果如下:


归功于ImportanceOfBeingErnest,他为我指明了这个解决方案。

这是我最终得到的代码:

import time
@plt.FuncFormatter
def convert_to_date_string(x,pos):
    return time.strftime('%Y-%m',time.localtime(x))


plt.figure(figsize=(15,4))
sns.set(style='whitegrid')
temp = dt_filtered_time.EVENT_DTM_TS.astype(np.int64)/1E9
ax = sns.boxplot(x=temp)
ax.xaxis.set_major_formatter(convert_to_date_string)
结果如下:


这要归功于Beingernest的重要性,他为我指出了这个解决方案。

您应该始终能够使用数字进行绘图。例如,将时间戳转换为浮点数(例如秒),并相应地勾选轴。您应始终能够使用数字进行绘图。即,将时间戳转换为浮点数(例如秒),并相应地勾选轴。