如何使用 Howard Hinnant 的 date.h 打印带有单位的“chrono”持续时间?

问题描述

我有以下玩具代码


# (I)
df1 = pd.melt(df,id_vars = ['level'],var_name = 'time',value_name = 'count') #

# (II)

df1['time'] = pd.to_datetime(df1['time'],format= '%H:%M:%s' ).dt.time

OR

df1['time'] = pd.to_timedelta(df1['time'],unit='m')


# (III)

plt.figure(figsize=(10,5))
plt.plot(df1)
for timex in range(30,180):
    plt.axvline(datetime(timex,1,1),color='k',linestyle='--',alpha=0.3)

# Perform STL Decomp
stl = STL(df1)
result = stl.fit()

seasonal,trend,resid = result.seasonal,result.trend,result.resid

plt.figure(figsize=(8,6))

plt.subplot(4,1)
plt.plot(df1)
plt.title('Original Series',fontsize=16)

plt.subplot(4,2)
plt.plot(trend)
plt.title('Trend',3)
plt.plot(seasonal)
plt.title('Seasonal',4)
plt.plot(resid)
plt.title('Residual',fontsize=16)

plt.tight_layout()

estimated = trend + seasonal
plt.figure(figsize=(12,4))
plt.plot(df1)
plt.plot(estimated)

plt.figure(figsize=(10,4))
plt.plot(resid)

# Anomaly detection 

resid_mu = resid.mean()
resid_dev = resid.std()

lower = resid_mu - 3*resid_dev
upper = resid_mu + 3*resid_dev

anomalies = df1[(resid < lower) | (resid > upper)] # returns the datapoints with the anomalies
anomalies


plt.plot(df1)
for timex in range(30,alpha=0.6)
    
plt.scatter(anomalies.index,anomalies.count,color='r',marker='D')


但是,它不起作用,因为:

#include "date/date.h"
#include <iostream>

using namespace std;
using namespace chrono;

int main() {
    cout << microseconds(100);
};

但是 C2679: binary '<<': no operator found which takes a right-hand operand of type 'std::chrono::milliseconds' (or there is no acceptable conversion) docs 列出了规范:

date.h

将使用 template <class CharT,class Traits,class Rep,class Period> std::basic_ostream<CharT,Traits>& operator<<(std::basic_ostream<CharT,Traits>& os,const std::chrono::duration<Rep,Period>& d); 输出适当的单位。

那么,我该如何正确使用这个重载的 get_units 运算符?

解决方法

那个 operator<< 位于 date 命名空间内。由于操作数类型都不是来自这个命名空间,因此依赖于参数的查找不会找到它。

要使用它,您需要 using namespace dateusing date::operator<<

您代码中的另一个问题是 microseconds 只能从整数类型构造,不能从浮点类型构造。