python将随机时间索引更改为第二个基本索引

时间:2018-04-25 08:05:58

标签: python pandas

我在制定问题时遇到一些困难因此请在下面找一个例子: 我有:

2017-11-23 16:30:52+01:00  20
2017-11-23 16:30:58+01:00  30
2017-11-23 16:31:30+01:00  25 

我想:

2017-11-23 16:30:52+01:00  20
2017-11-23 16:30:53+01:00  20
2017-11-23 16:30:54+01:00  20
...
2017-11-23 16:30:58+01:00  30
2017-11-23 16:30:59+01:00  30
...

实际上,我认为使用pandas resample with pad

这是一件容易的事
df=df.resample('1s').pad()

但是,这将返回以下内容:

2017-11-23 16:30:52+01:00  20
2017-11-23 16:30:53+01:00  NaN
2017-11-23 16:30:54+01:00  NaN
...
2017-11-23 16:30:58+01:00  NaN
2017-11-23 16:30:59+01:00  NaN
...

该列的类型为(float64)..

任何帮助表示赞赏。谢谢!

1 个答案:

答案 0 :(得分:0)

因此您需要在fillna

之后使用resample
df=df.resample('1s').fillna(method = 'pad')

resample将在pandas datetime index上创建1s分隔的行,并将填充其可用索引的值。您需要为其余行填充它。可以使用fillna方法。 method='pad'使用最后一个非空值填充所有值。