【问题标题】:Applying a monthly level loss factor in the hourly time series using Python pandas使用 Python pandas 在每小时时间序列中应用每月级别损失因子
【发布时间】:2020-06-29 01:23:49
【问题描述】:

我在数据帧 (df) 中有一个由 2 年组成的以下每小时时间序列

date energy 1/1/1997 0:00 37 1/1/1997 1:00 44 1/1/1997 2:00 55 . . . 12/31/1997 22:00 54 12/31/1997 23:00 55 1/1/1998 0:00 35 1/1/1998 1:00 36 1/1/1998 2:00 37 . . . 12/31/1998 23:00 44

我想将以下每月损失应用到能源列:

Monthly Loss 1 3.6158136 2 5.3829265 3 4.4004292 4 4.1649284 5 5.9518338 6 4.5651714 7 6.1399174 8 5.9933625 9 6.4627925 10 6.2534558 11 3.3416914 12 4.5363111

Expected Output
date            energy   new_energy
1/1/1997 0:00     37     35.66   # 37 * (1-3.6158136/100)
1/1/1997 1:00     44     42.41   # 44 * (1-3.6158136/100) 
1/1/1997 2:00     55     53.01   # 55 * (1-3.6158136/100)
.
.
.
12/31/1997 22:00   54     51.55   # 54 * (1-4.5363111/100) 
12/31/1997 23:00   55     52.51   # 55 * (1-4.5363111/100) 
1/1/1998 0:00     35     33.73   # 35 * (1-3.6158136/100)
1/1/1998 1:00     36     34.70   # 36 * (1-3.6158136/100)
1/1/1998 2:00     37     35.66   # 37 * (1-3.6158136/100)
.
.
.
12/31/1997 22:00   54    51.55   # 54 * (1-4.5363111/100)
12/31/1997 23:00   55    52.50   # 55 * (1-4.5363111/100) 

下面是我的代码。我正在尝试找到将每月损失应用于每小时时间序列的最简单方法

monthly_loss = [3.6158136, 5.3829265, 4.4004292, 4.1649284, 5.9518338, 4.5651714, 6.1399174, 5.9933625, 6.4627925, 6.2534558, 3.3416914, 4.5363111]

month = pd.to_datetime(df['date']).dt.month

df.insert(2, 'Month', month)

# converting the energy from hourly to monthly level
df['monthly_resampled_data'] = df.energy.resample('M').mean()

# apply monthly loss by months to the energy values
df['new_energy']=df['monthly_resampled_data']*(1-monthly_loss/100)

遇到错误

TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex'

【问题讨论】:

  • 你不能,除非你想应用于能源列的是两年的月平均。如果不是,您也应该在第二个数据框中提供年份。因为这是一个两年期,所以如果你明白我的意思,第一年和第二年都有可能有第一个月。也许给我们一个预期结果的例子。
  • @wwnde 我在我的问题中添加了预期的结果。我希望每年从时间戳开始应用该月的所有月度损失(意思是 1997 年 1 月和 1998 年的 1 小时应该将 1 月的相同损失应用到 1997 年和 1998 年 1 月 1 小时的能量值)。不知道pandas里面有没有可以轻松搞定的函数。
  • 这样更好。看看我的尝试,让我们知道它是否是你想要的
  • 谢谢!我喜欢您在将数据转换为日期时间后提取月份然后有效地使用 pd.merge 合并它们的方式!
  • 在某些情况下,我会在列表数据结构中而不是在数据框中丢失,所以我正在考虑执行以下操作:monthly_loss = [3.6158136, 5.3829265, 4.4004292, 4.1649284, 5.9518338, 4.5651714, 6.1399174, 5.9933625, 6.4627925, 6.2534558, 3.3416914, 4.5363111] month = [1,2,3,4,5,6,7,8,9,10,11,12] #converting lists into a dataframe df1 = pd.DataFrame(list(zip(monthly_loss, month)), columns = ['Monthly_Loss', 'Month'])

标签: python pandas date datetime time-series


【解决方案1】:

将日期强制转换为日期时间,将其设置为索引并在名为 Monthly 的列中提取月份

df['date']=pd.to_datetime(df['date'])
df.set_index(df['date'], inplace=True)
df['Monthly']=df.index.month

合并列Monthly上的两个数据框

df2= pd.merge(df, df1, on='Monthly', how='left')

应用公式并删除不需要的列

    df2['new_energy']=(df2['Energy']*(1-(df2['Loss']/100))).apply(lambda x:round(x,2))
df2.drop(columns=['Monthly','Loss'], inplace=True)

输出

【讨论】:

    猜你喜欢
    • 2020-05-10
    • 2022-12-11
    • 1970-01-01
    • 2016-07-18
    • 2022-11-21
    • 2020-06-16
    • 2018-06-23
    • 2019-12-27
    • 1970-01-01
    相关资源
    最近更新 更多