t64与object和int64列的不同条件的时间序列进行上采样

t64与object和int64列的不同条件的时间序列进行上采样

本文介绍了对float64与object和int64列的不同条件的时间序列进行上采样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与此类似的df:

print(df)
                        A     B  C
DATE_TIME
2016-10-08 13:57:00   in   5.61  0
2016-10-08 14:02:00   in   8.05  0
2016-10-08 14:07:00  out   7.92  0
2016-10-08 14:12:00   in   7.98  1
2016-10-08 14:17:00  out   8.18  0
2016-10-08 14:22:00  out   7.59  0

print (df.dtypes)
A     object
B    float64
C      int64
dtype: object

我想将此df重新采样为1S频率,以便可以将其与另一个df连接起来.我无法解决的问题是对于objectint64列,我希望对新创建的时间行重复相同的值,这可以通过以下功能完成:

I want to resample this df to a 1S frecuency, so that I can concatenate it with another df.The problem I can't solve is that for the columns type object and int64 I want the same value repeated for the newly created time rows, which could be done by this function:

df=df.resample('S', fill_method='pad')

而对于float64列,我正在寻找:

whereas for the float64 columns I am looking for this:

df=df.interpolate()

我曾考虑过要使用IF语句,但是我也想,我首先必须在插值步骤之前执行重采样步骤.当我仅按df=df.resample('S')重新采样时,我便可以进行插值,该方法适用于float64列,但不适用于objectInt64列.有人可以帮我吗?谢谢.

I thought about applying an IF statement, but I also figuered that I first have to do the resample step before the interpolation step. When I resample just by df=df.resample('S') I can interpolate afterwards, which works for the float64 columns but not for the object and Int64 ones.Could anybody help me please? Thanks.

推荐答案

以下是使用reindex的方法:

index = pd.date_range(df.index[0], df.index[-1], freq="s")
df2 = df.reindex(index)
for col, s in df2.iteritems():
    if s.dtype == float:
        s.interpolate(inplace=True)
    else:
        s.ffill(inplace=True)

这篇关于对float64与object和int64列的不同条件的时间序列进行上采样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 17:18