简体   繁体   中英

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

I'm using pandas.DataFrame.resample on the data set below:

data snapshot

And I applied my own function, which is defined as:

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. I don't know why are there 2 Series in the resample loop. console snapshot

What's more, why don't the lines print(df['close']), print(df['high']), print(df['low']) raise any error? The 2 Series shouldn't contain any of these columns.

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.

Hope I explained you what you wanted to ask. Thank you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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