简体   繁体   English

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

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

I'm using pandas.DataFrame.resample on the data set below:我在下面的数据集上使用 pandas.DataFrame.resample:

data snapshot数据快照

And I applied my own function, which is defined as:而我应用了自己的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

So the implementation of the resample is:所以重采样的实现是:

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

As you can see, I printed out all the looped DataFrames in the resample process, but the first two prints are actually Series, one of which even has a name of a timestamp.如您所见,我在重采样过程中打印了所有循环的 DataFrames,但前两个打印实际上是 Series,其中一个甚至具有时间戳的名称。 I don't know why are there 2 Series in the resample loop.我不知道为什么在重采样循环中有 2 个系列。 console snapshot控制台快照

What's more, why don't the lines print(df['close']), print(df['high']), print(df['low']) raise any error?此外,为什么行 print(df['close'])、print(df['high'])、print(df['low']) 不引发任何错误? The 2 Series shouldn't contain any of these columns. 2 系列不应包含任何这些列。

You are getting 2 series because in you are printing ret which means that when you execute function it will print(ret) hence first series and then when you execute next line return(ret) it will return ret hence, second series.您将获得 2 个系列,因为您正在打印 ret,这意味着当您执行 function 时,它将打印(ret)因此是第一个系列,然后当您执行下一行 return(ret)时它将返回 ret 因此是第二个系列。

Hope I explained you what you wanted to ask.希望我向您解释了您想问的问题。 Thank you谢谢

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

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