代码之家  ›  专栏  ›  技术社区  ›  meow

熊猫重采样->应用第一个分区

  •  1
  • meow  · 技术社区  · 6 年前

    熊猫能用吗 resample 方法,然后只使用第一个(或任何特定的)分区进行应用(或在这方面使用任何其他方法)?

    举例说明:

    def do something(x):
        return [x]
    
    my_timeseries.resample("d").apply(do_something)[0]
    

    现在,这给了我想要的输出,同时也计算了所有其他分区的输出(显然),但是如果我只想从重采样对象中选择第一个分区,它将自动取其平均值并给出警告:

    my_timeseries.resample("d")[0].apply(do_something)
    
    >> FutureWarning: 
    .resample() is now a deferred operation
    You called __getitem__(...) on this deferred object which materialized it into a series
    by implicitly taking the mean.  Use .resample(...).mean() instead
      if __name__ == '__main__':
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Luiz Otavio V. B. Oliveira    6 年前

    在以前的熊猫版本中,这种方法 DataFrame.resample how 用于向下或重新取样,带 mean here

    my_timeseries.resample("d").apply(do_something)[0] do_something 0

    my_timeseries.resample("d") 不可能,而且由于兼容性原因,熊猫呼叫 索引前。你所能做的就是 如你所说。

        2
  •  0
  •   meow    6 年前

    resampler = my_timeseries.resample("d")
    my_timeseries[resampler.indices[list(resampler.indices)[0]]].apply(do_something)
    

    Series .