Main Content

在时间表中选择时间

时间表是一种表类型,用于将时间与每一行进行关联。您可以通过几种方式选择基于时间的数据子集:

  • 使用 timerangewithtol 函数查找特定范围内的时间。

  • 使用 datetime 数组的分量匹配重复出现的时间单位,如天或月。

  • 使用 retime 函数对数据进行重采样或分组。

例如,读取示例文件 outages.csv,其中包含 2002 年至 2014 年美国电力中断的数据。行时间向量 OutageTime 表示电力中断发生的时间。readtimetable 函数将其作为 datetime 数组导入。显示前五行。

TT = readtimetable('outages.csv');
head(TT,5)
         OutageTime            Region         Loss     Customers       RestorationTime              Cause       
    ____________________    _____________    ______    __________    ____________________    ___________________

    01-Feb-2002 12:18:00    {'SouthWest'}    458.98    1.8202e+06    07-Feb-2002 16:50:00    {'winter storm'   }
    23-Jan-2003 00:49:00    {'SouthEast'}    530.14    2.1204e+05                     NaT    {'winter storm'   }
    07-Feb-2003 21:15:00    {'SouthEast'}     289.4    1.4294e+05    17-Feb-2003 08:14:00    {'winter storm'   }
    06-Apr-2004 05:44:00    {'West'     }    434.81    3.4037e+05    06-Apr-2004 06:10:00    {'equipment fault'}
    16-Mar-2002 06:18:00    {'MidWest'  }    186.44    2.1275e+05    18-Mar-2002 23:23:00    {'severe storm'   }

在 R2019a 之前,使用 readtable 读取表数据并使用 table2timetable 将其转换为时间表。

选择时间范围

要查找特定范围内的数据,您可以使用 timerange 函数,该函数定义基于时间的下标来进行索引。例如,定义 2008 年夏季,也就是从 6 月 20 日开始到 9 月 21 日结束,的范围。默认情况下,timerange 定义半开区间,左边关闭,右边打开,因此将结束日期指定为 9 月 22 日。

TR = timerange("2008-06-20","2008-09-22")
TR = 
	timetable timerange subscript:

		Select timetable rows with times in the half-open interval:
		[20-Jun-2008 00:00:00, 22-Sep-2008 00:00:00)

找出在该范围内发生的电力中断,然后对受影响的客户数量绘制随时间变化的图。

summer08 = TT(TR,:);
stem(summer08.OutageTime,summer08.Customers)
ylabel("Customers")

Figure contains an axes object. The axes object with ylabel Customers contains an object of type stem.

在此时间范围内的几次电力中断对客户产生很大影响。将范围扩大到 2008 年全年,并寻找类似的高值数字。

TR = timerange("2008","years");
all08 = TT(TR,:);
high08 = all08(all08.Customers > 500000,:);

stem(high08.OutageTime,high08.Customers)
ylabel('Customers')

Figure contains an axes object. The axes object with ylabel Customers contains an object of type stem.

timerange 函数也有助于选择特定日期。通过比较 datetime 值来选择时间可能会产生错误结果,因为所有 datetime 值都包含日期时间分量。但是,当您仅指定 datetime 值的日期分量时,时间分量将设置为午夜。因此,虽然存在从 6 月 26 日以来的数据,但像这样的比较不会返回结果。

any(summer08.OutageTime == datetime("2008-06-26"))
ans = logical
   0

您可以改用 timerange

TR = timerange("2008-06-26","days");
june26 = summer08(TR,:)
june26=1×5 timetable
         OutageTime            Region         Loss     Customers      RestorationTime             Cause      
    ____________________    _____________    ______    _________    ____________________    _________________

    26-Jun-2008 22:36:00    {'NorthEast'}    425.21      93612      27-Jun-2008 06:53:00    {'thunder storm'}

定义范围的另一种方法是使用 withtol 指定一段时间内的容差。例如,查找从 2008 年夏天以来的行,其中 OutageTime 在 9 月 1 日劳动节的三天内。

WT = withtol("2008-09-01",days(3));
nearSep1 = summer08(WT,:)
nearSep1=4×5 timetable
         OutageTime            Region         Loss     Customers      RestorationTime              Cause       
    ____________________    _____________    ______    _________    ____________________    ___________________

    01-Sep-2008 23:35:00    {'SouthEast'}    206.27     2.27e+05                     NaT    {'equipment fault'}
    01-Sep-2008 00:18:00    {'MidWest'  }    510.05        74213    01-Sep-2008 14:07:00    {'thunder storm'  }
    02-Sep-2008 19:01:00    {'MidWest'  }       NaN    2.215e+05    03-Sep-2008 02:58:00    {'severe storm'   }
    29-Aug-2008 20:25:00    {'West'     }       NaN        31624    01-Sep-2008 01:51:00    {'wind'           }

匹配时间单位

您还可以使用 datetime 值的单位(如小时或天)来标识用于逻辑索引的行。这种方法对于指定周期性间隔很有用。

例如,查找 OutageTime 的值,其月份分量的值等于或小于 3,对应于每年的 1 月、2 月和 3 月。使用生成的逻辑数组对 TT 进行索引。

TR = (month(TT.OutageTime) <= 3);
winterTT = TT(TR,:);

head(winterTT,5)
         OutageTime            Region         Loss     Customers       RestorationTime            Cause      
    ____________________    _____________    ______    __________    ____________________    ________________

    01-Feb-2002 12:18:00    {'SouthWest'}    458.98    1.8202e+06    07-Feb-2002 16:50:00    {'winter storm'}
    23-Jan-2003 00:49:00    {'SouthEast'}    530.14    2.1204e+05                     NaT    {'winter storm'}
    07-Feb-2003 21:15:00    {'SouthEast'}     289.4    1.4294e+05    17-Feb-2003 08:14:00    {'winter storm'}
    16-Mar-2002 06:18:00    {'MidWest'  }    186.44    2.1275e+05    18-Mar-2002 23:23:00    {'severe storm'}
    04-Feb-2005 08:18:00    {'MidWest'  }       NaN           NaN    04-Feb-2005 19:51:00    {'attack'      }

创建一个冬季时间原因的饼图。pie 函数只接受数值或 categorical 输入,因此首先将 Cause 转换为 categorical

winterTT.Cause = categorical(winterTT.Cause);
pie(winterTT.Cause)
title("Causes of Outages, January to March");

按时间段分组

retime 函数通过对值进行重采样或分组来调整行时间,以创建指定的间隔。其预定义间隔从秒到年不等,您可以指定如何处理间隔的缺失值或多个值。例如,您可以选择每周的第一个观测值,或对一个季度的观测值进行计数。

对于电力中断数据,您可以使用 retime 来查找每年的总数。首先,创建一个只包含数值变量的时间表。然后,调用 retime 并指定年度间隔,用总和组合多个值。输出中每年对应一个行,包含该年的总损失和受影响的客户总数。

numTT = TT(:,vartype("numeric"));
numTT = retime(numTT,"yearly","sum");
head(numTT,5)
    OutageTime     Loss     Customers 
    ___________    _____    __________

    01-Jan-2002    81335    1.3052e+07
    01-Jan-2003    58036     1.396e+07
    01-Jan-2004    51014    1.5523e+07
    01-Jan-2005    33980    8.7334e+06
    01-Jan-2006    35129    2.5729e+07

创建一个条形图,显示每年受影响的客户数量。

bar(numTT.OutageTime,numTT.Customers)
xlabel("Year")
ylabel("Customers")

Figure contains an axes object. The axes object with xlabel Year, ylabel Customers contains an object of type bar.

使用行时间计算持续时间

您可以将时间表的行时间与其他 datetimeduration 值结合使用来执行计算。例如,计算电力中断数据中列出的停电持续时间。然后计算每月电力中断持续时间的中位数并对其绘图。

首先,通过从 RestorationTime(即停电结束时间)中减去行时间(即停电开始时间),将电力中断持续时间添加到 TT。更改 OutageDuration 的格式,以天为单位显示电力中断的持续时间。显示 TT 的前五行。

TT.OutageDuration = TT.RestorationTime - TT.OutageTime;
TT.OutageDuration.Format = 'd';
head(TT,5)
         OutageTime            Region         Loss     Customers       RestorationTime              Cause           OutageDuration
    ____________________    _____________    ______    __________    ____________________    ___________________    ______________

    01-Feb-2002 12:18:00    {'SouthWest'}    458.98    1.8202e+06    07-Feb-2002 16:50:00    {'winter storm'   }      6.1889 days 
    23-Jan-2003 00:49:00    {'SouthEast'}    530.14    2.1204e+05                     NaT    {'winter storm'   }         NaN days 
    07-Feb-2003 21:15:00    {'SouthEast'}     289.4    1.4294e+05    17-Feb-2003 08:14:00    {'winter storm'   }      9.4576 days 
    06-Apr-2004 05:44:00    {'West'     }    434.81    3.4037e+05    06-Apr-2004 06:10:00    {'equipment fault'}    0.018056 days 
    16-Mar-2002 06:18:00    {'MidWest'  }    186.44    2.1275e+05    18-Mar-2002 23:23:00    {'severe storm'   }      2.7118 days 

创建一个只包含电力中断持续时间的时间表。TT 中某些行的停电结束时间为缺失值 NaT,导致 OutageDuration 中出现 NaN 值。要从 medianTT 中删除 NaN 值,请使用 rmmissing 函数。然后使用 retime 计算每月电力中断持续时间中位数。显示 medianTT 的前五行。

medianTT = TT(:,"OutageDuration");
medianTT = rmmissing(medianTT); 
medianTT = retime(medianTT,'monthly',@median);
head(medianTT,5)
    OutageTime     OutageDuration
    ___________    ______________

    01-Feb-2002      6.1889 days 
    01-Mar-2002      2.7472 days 
    01-Apr-2002         NaN days 
    01-May-2002     0.72917 days 
    01-Jun-2002     0.22431 days 

创建每月电力中断持续时间中位数的阶梯图。

stairs(medianTT.OutageTime,medianTT.OutageDuration)
xlabel("Year")
ylabel("Median Duration (days)")

Figure contains an axes object. The axes object with xlabel Year, ylabel Median Duration (days) contains an object of type stair.

另请参阅

| | | | | | | | | | |

相关主题