Skip to main content
 首页 » 编程设计

python之从 python 中的 timedelta64 对象创建箱线图

2024年12月31日8bhlsheji

我是 python 的新手,以前使用 R。

通常我会创建时间增量的数值向量并构建箱线图。在 python 中,它似乎有点复杂。 这是我得到的列表的摘录。

1502    4 days 17:51:16 
1503    4 days 17:51:57 
1504    4 days 17:48:24 
1505    4 days 17:34:16 
1506    4 days 17:32:58 
1507    4 days 19:21:27 
1508    4 days 19:52:43 
1509    4 days 19:37:17 
1510    4 days 21:00:30 
1511    5 days 00:56:52 
1512    3 days 00:56:04 
1513                NaT 
Length: 1514, dtype: timedelta64[ns] 

我在列表中尝试了这个:

    # Create a figure instance 
fig = plt.figure(1, figsize=(9, 6)) 
 
# Create an axes instance 
ax = fig.add_subplot(111) 
 
# Create the boxplot 
bp = ax.boxplot(timediff) 
 
# Save the figure 
fig.savefig('fig1.png', bbox_inches='tight') 

我确实得到了一个输出,但它似乎是完全错误的。有人能帮我吗?数据类型有误吗?



请您参考如下方法:

目前,您的箱线图使用 Unix 时间(自纪元以来经过的秒数,1970-01-01 00:00:00)代表您的时差,timedelta64[ns],值。因此,y 轴单位采用非常大的整数比例:1e19。

考虑以您需要的单位转换时差值:带小数点的天。然后绘制系列。

timediff = timediff_raw.dt.days + \ 
              (timediff_raw.dt.seconds//3600) / 24 + \ 
              ((timediff_raw.dt.seconds//60)%60) / (24*60) 
 
print(timediff.head(10)) 

注意:非常低的离群值将保持不变,因为相同的图形将呈现但具有不同的 y 轴单位。


使用可重现的随机种子示例进行演示:

数据 (50 个元素的系列)

import numpy as np 
import pandas as pd 
import datetime as dt 
import time 
import matplotlib.pyplot as plt 
 
# CURRENT TIME STAMP 
epoch_time = int(time.time()) 
 
np.random.seed(81618) 
time1 = pd.Series([dt.datetime.fromtimestamp(np.random.randint(1530000000, epoch_time))  
                   for _ in range(50)]) 
time2 = pd.Series([dt.datetime.fromtimestamp(np.random.randint(1530000000, epoch_time))  
                   for _ in range(50)]) 
 
print(time1.head()) 
# 0   2018-07-29 04:12:07 
# 1   2018-07-02 07:48:08 
# 2   2018-08-17 05:04:59 
# 3   2018-08-06 21:37:45 
# 4   2018-07-15 10:27:10 
# dtype: datetime64[ns] 
 
print(time2.head()) 
# 0   2018-07-25 09:11:39 
# 1   2018-08-15 07:05:39 
# 2   2018-07-06 08:19:05 
# 3   2018-07-13 19:08:30 
# 4   2018-07-24 11:13:06 
# dtype: datetime64[ns] 

时差转换 (使用 pandas.Series.dt )

timediff_raw = (time1 - time2) 
 
timediff = timediff_raw.dt.days + \ 
              (timediff_raw.dt.seconds / (60*60*24))  # NUMBER OF SECONDS IN A DAY 
 
print(timediff.head(10)) 
# 0     3.791991 
# 1   -43.970498 
# 2    41.865208 
# 3    24.103646 
# 4    -9.031898 
# dtype: float64 

图表

# Create a figure instance 
fig = plt.figure(figsize=(9, 6)) 
 
# Create an axes instance 
ax = fig.add_subplot(111) 
 
# Create the boxplot 
ax.boxplot(timediff) 
plt.xlabel('Single Series') 
plt.ylabel('Time Difference (Days)') 
 
plt.show() 
plt.clf() 
plt.close('all')