第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在另一個(gè) pandas TimeIndex 中將系列索引移動(dòng) 1 行?

如何在另一個(gè) pandas TimeIndex 中將系列索引移動(dòng) 1 行?

陪伴而非守候 2023-05-09 11:01:33
我有一個(gè)名為“raw_Ix”的 pd.DatetimeIndex,它包含我正在使用的所有索引和兩個(gè) pandas (Time)series("t1" and "nextloc_ixS")(兩者都具有相同的時(shí)間索引)?!皀extloc_ixS”中的值與 t1.index 和 nextloc_ixS.index 中的相同索引在 raw_Ix 中移動(dòng)了一位。為了更好地理解“nextloc_ixS”是什么:   nextloc_ixS =  t1.index[np.searchsorted(raw_Ix, t1.index)+1]    nextloc_ixS = pd.DataFrame(nextloc_ixS, index = t1.index)這三個(gè)都傳遞給一個(gè)函數(shù),我需要它們的形式如下:我需要?jiǎng)h除 t1.index 不在 raw_Ix 中的 t1 行(以避免錯(cuò)誤,因?yàn)?raw_Ix 可能已被操縱)之后,我現(xiàn)在深入復(fù)制 t1(我們稱之為 t1_copy)。因?yàn)槲倚枰猲extloc_ixS 的值作為 t1_copy 的新 DatetimeIndex。(聽(tīng)起來(lái)很簡(jiǎn)單,但在這里我遇到了困難)但在替換 i 的索引之前,可能需要將 t1_copy 的舊索引保存為 t1_copy 中的一列,用于最后一步(== 第 5 步)。實(shí)際函數(shù)在特定過(guò)程中選擇 t1_copy 的一些索引并返回“結(jié)果”,這是一個(gè) pd.DatetimeIndex,其中包含 t1_copy 的一些索引和重復(fù)項(xiàng)我需要將結(jié)果移回 1,但不是通過(guò) np.searchsorted。(注意:“結(jié)果”仍然人為地向前移動(dòng),所以我們可以通過(guò)在 t1_copy.index 中獲取索引位置然后在步驟 3 的備份列中獲取“舊”索引來(lái)將其設(shè)置為向后。我知道這聽(tīng)起來(lái)有點(diǎn)復(fù)雜,因此這是我處理的低效代碼:def main(raw_Ix, t1, nextloc, nextloc_ixS=None):       t1_copy = t1.copy(deep=True).to_frame()    nextloc_ixS = nextloc_ixS.to_frame()         if nextloc_ixS is not None:                  t1_copy                  = t1_copy.loc[t1_copy.index.intersection(pd.DatetimeIndex(raw_Ix))]         t1_copy                  = t1_copy[~t1_copy.index.duplicated(keep='first')]# somehow duplicates came up, I couldnt explain why        t1_copy["index_old"] = t1_copy.index.copy(deep=True)         temp                     = nextloc_ixS.loc[nextloc_ixS.index.intersection(raw_Ix)].copy(deep=True)         t1_copy.set_index(pd.DatetimeIndex(temp[~temp.index.duplicated(keep='first')].values), inplace=True) # somehow duplicates came up, I couldnt explain why therefore the .duplicated(...)else: # in this case we just should find the intersection        t1_copy = t1_copy.loc[t1.index.intersection(pd.DatetimeIndex(raw_Ix))]        t1_copy = t1_copy[~t1_copy.index.duplicated(keep='first')]  所以簡(jiǎn)而言之:我嘗試前后移動(dòng)索引,同時(shí)避免使用 np.searchsorted() 而是使用兩個(gè) pd.Series (或者更好地將其稱為列,因?yàn)樗鼈兪菑?DataFrame 中單獨(dú)傳遞的)有什么方法可以在代碼線和時(shí)間使用方面有效地做到這一點(diǎn)?(行數(shù)非常多)
查看完整描述

1 回答

?
慕村225694

TA貢獻(xiàn)1880條經(jīng)驗(yàn) 獲得超4個(gè)贊

您的邏輯很復(fù)雜,可以實(shí)現(xiàn)兩件事

  1. 刪除不在列表中的行。我為此使用了一個(gè)技巧,所以我可以使用dropna()

  2. shift()專(zhuān)欄

這表現(xiàn)得很好。數(shù)據(jù)集 > 0.5m 行上的幾分之一秒。

import time

d = [d for d in pd.date_range(dt.datetime(2015,5,1,2), 

                          dt.datetime(2020,5,1,4), freq="128s") 

     if random.randint(0,3) < 2 ] # miss some sample times... 


# random manipulation of rawIdx so there are some rows where ts is not in rawIdx

df = pd.DataFrame({"ts":d, "rawIdx":[x if random.randint(0,3)<=2 

                                     else x + pd.Timedelta(1, unit="s") for x in d], 

                   "val":[random.randint(0,50) for x in d]}).set_index("ts")

start = time.time()

print(f"size before: {len(df)}")

dfc = df.assign(

    # make it float64 so can have nan, map False to nan so can dropna() rows that are not in rawIdx

    issue=lambda dfa: np.array(np.where(dfa.index.isin(dfa["rawIdx"]),True, np.nan), dtype="float64"),

).dropna().drop(columns="issue").assign(

    # this should be just a straight forward shift.  rawIdx will be same as index due to dropna()

    nextloc_ixS=df.rawIdx.shift(-1)

)


print(f"size after: {len(dfc)}\ntime: {time.time()-start:.2f}s\n\n{dfc.head().to_string()}")

輸出


size before: 616264

size after: 462207

time: 0.13s


                                 rawIdx  val         nextloc_ixS

ts                                                              

2015-05-01 02:02:08 2015-05-01 02:02:08   33 2015-05-01 02:06:24

2015-05-01 02:06:24 2015-05-01 02:06:24   40 2015-05-01 02:08:33

2015-05-01 02:10:40 2015-05-01 02:10:40   15 2015-05-01 02:12:48

2015-05-01 02:12:48 2015-05-01 02:12:48   45 2015-05-01 02:17:04

2015-05-01 02:17:04 2015-05-01 02:17:04   14 2015-05-01 02:21:21


查看完整回答
反對(duì) 回復(fù) 2023-05-09
  • 1 回答
  • 0 關(guān)注
  • 115 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)