【发布时间】:2020-08-13 23:34:04
【问题描述】:
当我尝试将两个数据集连接(或合并/合并)在一起时,这里有一些冗长的代码存在问题,我得到了这个TypeError: Cannot compare type 'Timestamp' with type 'int'
这两个数据集都来自对相同的初始起始数据集进行重采样。 master_hrs df 是一个重采样过程,使用称为 rupters 的变点算法 Python 包。 (pip install ruptures 运行代码)。 daily_summary df 只是使用 Pandas 重新采样每日均值和总和值。但是当我尝试将数据集组合在一起时出现错误。有人有什么建议可以尝试吗?
编造一些虚假数据会产生与我的真实世界数据集相同的错误。我认为我遇到的问题是我正在尝试将 datime 与 numpy 进行一些比较......非常感谢任何提示。谢谢
import ruptures as rpt
import calendar
import numpy as np
import pandas as pd
np.random.seed(11)
rows,cols = 50000,2
data = np.random.rand(rows,cols)
tidx = pd.date_range('2019-01-01', periods=rows, freq='H')
df = pd.DataFrame(data, columns=['Temperature','Value'], index=tidx)
def changPointDf(df):
arr = np.array(df.Value)
#Define Binary Segmentation search method
model = "l2"
algo = rpt.Binseg(model=model).fit(arr)
my_bkps = algo.predict(n_bkps=5)
# getting the timestamps of the change points
bkps_timestamps = df.iloc[[0] + my_bkps[:-1] +[-1]].index
# computing the durations between change points
durations = (bkps_timestamps[1:] - bkps_timestamps[:-1])
#hours calc
d = durations.seconds/60/60
d_f = pd.DataFrame(d)
df2 = d_f.T
return df2
master_hrs = pd.DataFrame()
for idx, days in df.groupby(df.index.date):
changPoint_df = changPointDf(days)
values = changPoint_df.values.tolist()
master_hrs=master_hrs.append(values)
master_hrs.columns = ['overnight_AM_hrs', 'moring_startup_hrs', 'moring_ramp_hrs', 'high_load_hrs', 'evening_shoulder_hrs']
daily_summary = pd.DataFrame()
daily_summary['Temperature'] = df['Temperature'].resample('D').mean()
daily_summary['Value'] = df['Value'].resample('D').sum()
final_df = daily_summary.join(master_hrs)
【问题讨论】:
-
您构建
master_hrs的方式不包括日期时间索引,因为您append只有值,因此是一个没有索引的数组,来自changPoint_df。所以连接不能与具有 datetimeindex 的daily_summary一起使用
标签: python pandas data-science