【问题标题】:TypeError: Cannot compare type 'Timestamp' with type 'int'TypeError:无法将“时间戳”类型与“int”类型进行比较
【发布时间】: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


【解决方案1】:

问题在于索引本身 - master_hrs 是 int64 而 daily_summary 是日期时间。在将两个数据框连接在一起之前包含此内容:

master_hrs.index = pd.to_datetime(master_hrs.index)

为了清楚起见,这是我的final_df 的输出:

            Temperature      Value  ...  high_load_hrs  evening_shoulder_hrs
2019-01-01     0.417517  12.154527  ...            NaN                   NaN
2019-01-02     0.521131  13.811842  ...            NaN                   NaN
2019-01-03     0.583205  12.568966  ...            NaN                   NaN
2019-01-04     0.448225  14.036136  ...            NaN                   NaN
2019-01-05     0.542870  10.738192  ...            NaN                   NaN
                ...        ...  ...            ...                   ...
2024-09-10     0.470421  13.775528  ...            NaN                   NaN
2024-09-11     0.384672  10.473930  ...            NaN                   NaN
2024-09-12     0.527284  14.000231  ...            NaN                   NaN
2024-09-13     0.555646  11.460867  ...            NaN                   NaN
2024-09-14     0.426003   3.763975  ...            NaN                   NaN

[2084 rows x 7 columns]

希望这能满足您的需求。

【讨论】:

  • 嗨,布兰登,如您的回答所示,左侧列的值为 null 为 null。您会推荐合并或连接,以便所有内容都排列在索引时间戳上..?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-30
  • 2020-04-21
  • 2020-08-15
  • 2018-09-01
  • 2023-03-29
  • 1970-01-01
  • 2017-10-09
相关资源
最近更新 更多