簡體   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