切片和相乘后,时间序列返回NaN

时间:2019-01-04 09:03:01

标签: pandas time-series

摘要:

B

返回:

enter image description here

如果我不切片,则乘法有效:

s = sched.scheduler(time.time)
# Spawn threads which enter A and B into s
while True:
    next_ev = s.run(False)
    if next_ev is not None:
        time.sleep(min(1, next_ev))
    else:
        time.sleep(1)

返回:

enter image description here

所以我的猜测是,当索引不匹配时,Pandas将不会执行任何操作,而只是返回import numpy as np import pandas as pd dr = pd.date_range(start='1984-01-01', end='1984-01-10') df = pd.DataFrame(np.arange(len(dr)), index=dr, columns=["Values"]) df.iloc[:-5,] * df.iloc[5:,]

1 个答案:

答案 0 :(得分:0)

两个切片的DataFrames中的问题都是不同的NaN,可能的解决方案是通过numpy数组进行多重处理,以防止数据对齐不同的索引返回print (df.iloc[:-5,].index) DatetimeIndex(['1984-01-01', '1984-01-02', '1984-01-03', '1984-01-04', '1984-01-05'], dtype='datetime64[ns]', freq='D') print (df.iloc[5:,].index) DatetimeIndex(['1984-01-06', '1984-01-07', '1984-01-08', '1984-01-09', '1984-01-10'], dtype='datetime64[ns]', freq='D')

print (df.iloc[:-5,] * df.iloc[5:,].values)
            Values
1984-01-01       0
1984-01-02       6
1984-01-03      14
1984-01-04      24
1984-01-05      36

print(df.iloc[5:,])
            Values
1984-01-06       5
1984-01-07       6
1984-01-08       7
1984-01-09       8
1984-01-10       9

print(df.iloc[5:,].set_index(df.index[:-5]))
            Values
1984-01-01       5
1984-01-02       6
1984-01-03       7
1984-01-04       8
1984-01-05       9

print(df.iloc[:-5,] * df.iloc[5:,].set_index(df.index[:-5]))
            Values
1984-01-01       0
1984-01-02       6
1984-01-03      14
1984-01-04      24
1984-01-05      36

或在两个切片的DataFrame中创建相同的索引值:

print(pd.DataFrame(df.iloc[:-5,].values * df.iloc[5:,].values, 
                   columns=df.columns,
                   index=df.index[:-5]))
            Values
1984-01-01       0
1984-01-02       6
1984-01-03      14
1984-01-04      24
1984-01-05      36

接下来的方法是在两个DataFrame中都乘以numpy数组,然后使用DataFrame构造函数:

{{1}}