繁体   English   中英

为什么pandas.DataFrame.resample循环中有Series?

[英]why are there Series in the pandas.DataFrame.resample loop?

我在下面的数据集上使用 pandas.DataFrame.resample:

数据快照

而我应用了自己的function,定义为:

def btc_resample(df):
    if len(df) > 0:
        print(type(df))
        print(df)
        print(df['close'])
        print(df['high'])
        print(df['low'])
        ret = df.head(1).copy()
        ret['close'] = df['close'].values[-1]
        ret['high'] = df['high'].max()
        ret['low'] = df['low'].min()
        print(ret)
        return ret
    else:
        return None

所以重采样的实现是:

data.resample('5min').apply(btc_resample)

如您所见,我在重采样过程中打印了所有循环的 DataFrames,但前两个打印实际上是 Series,其中一个甚至具有时间戳的名称。 我不知道为什么在重采样循环中有 2 个系列。 控制台快照

此外,为什么行 print(df['close'])、print(df['high'])、print(df['low']) 不引发任何错误? 2 系列不应包含任何这些列。

您将获得 2 个系列,因为您正在打印 ret,这意味着当您执行 function 时,它将打印(ret)因此是第一个系列,然后当您执行下一行 return(ret)时它将返回 ret 因此是第二个系列。

希望我向您解释了您想问的问题。 谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM