pandas 使用 period_range 重新索引时数据帧为 NaN

pandas dataframe NaN when reindexing with period_range

我希望代码添加数据框中不存在的新行。使用 period_range 重新索引时,我得到 NaN 值。我得到正确的 period_range 但 NaN 而不是保留列 'A' 的可用值。下面显示了代码示例:

我猜问题是因为使用了 PeriodIndex 和 DatetimeIndex 对象。

                      A
2018-10-31 14:08:26 NaN
2018-10-31 14:08:27 NaN
2018-10-31 14:08:28 NaN
2018-10-31 14:08:29 NaN
2018-10-31 14:08:30 NaN


import pandas as pd

data=[['2018-10-31 14:08:26', 1],
      ['2018-10-31 14:08:28', 2],
      ['2018-10-31 14:08:30', 3]]

df = pd.DataFrame(data=data, columns=['time','A'])
df.time = pd.to_datetime(df.time)
ts = df.time
idx = pd.period_range(min(ts), max(ts),freq='s')
df = df.set_index('time',drop=True)
df = df.reindex( idx )

需要将 DatetimeIndex 更改为 PeriodIndex:

df = df.set_index('time',drop=True)
df.index=df.index.to_period('S')
df = df.reindex( idx )

                       A
2018-10-31 14:08:26  1.0
2018-10-31 14:08:27  NaN
2018-10-31 14:08:28  2.0
2018-10-31 14:08:29  NaN
2018-10-31 14:08:30  3.0
data = [['2018-10-31 14:08:26', 1],
        ['2018-10-31 14:08:28', 2],
        ['2018-10-31 14:08:30', 3]]

df = pd.DataFrame(data=data, columns=['time','A'])
df['time'] = pd.to_datetime(df['time'])
df.set_index('time').resample('S').asfreq()

输出

>>> df
                        A
time    
2018-10-31 14:08:26     1.0
2018-10-31 14:08:27     NaN
2018-10-31 14:08:28     2.0
2018-10-31 14:08:29     NaN
2018-10-31 14:08:30     3.0