pandas.Series.interpolate()不执行任何操作.为什么? [英] pandas.Series.interpolate() does nothing. Why?

查看:209
本文介绍了pandas.Series.interpolate()不执行任何操作.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有DatetimeIndex的数据框.这是列之一:

I have a dataframe with DatetimeIndex. This is one of columns:

>>> y.out_brd
2013-01-01 11:25:00     0.04464286
2013-01-01 11:30:00            NaN
2013-01-01 11:35:00            NaN
2013-01-01 11:40:00    0.005952381
2013-01-01 11:45:00     0.01785714
2013-01-01 11:50:00    0.008928571
Freq: 5T, Name: out_brd, dtype: object

当我尝试在函数上使用interpolate()时,我什么都没有改变:

When I'm trying to use interpolate() on function I get absolutly nothing changes:

>>> y.out_brd.interpolate(method='time')
2013-01-01 11:25:00     0.04464286
2013-01-01 11:30:00            NaN
2013-01-01 11:35:00            NaN
2013-01-01 11:40:00    0.005952381
2013-01-01 11:45:00     0.01785714
2013-01-01 11:50:00    0.008928571
Freq: 5T, Name: out_brd, dtype: object

如何使其工作?

更新: 生成此类数据框的代码.

Update: the code for generating such a dataframe.

time_index = pd.date_range(start=datetime(2013, 1, 1, 3),
                       end=datetime(2013, 1, 2, 2, 59),
                       freq='5T')
grid_columns = [u'in_brd', u'in_alt', u'out_brd', u'out_alt']                           

df = pd.DataFrame(index=time_index, columns=grid_columns)

然后,我用一些数据填充单元格.

After that I fill cells with some data.

我有一个数据框field_data,其中包含有关铁路上下车的调查数据,以及station变量. 我也定义了interval_end函数,如下所示:

I have dataframe field_data with survey data about boarding and alighting on railroad, and station variable. I also have interval_end function defined like this:

interval_end = lambda index, prec_lvl: index.to_datetime() \
                        + timedelta(minutes=prec_lvl - 1,
                                    seconds=59)

代码:

for index, row in df.iterrows():
    recs = field_data[(field_data.station_name == station)
                    & (field_data.arrive_time >= index.time())
                    & (field_data.arrive_time <= interval_end(
                                        index, prec_lvl).time())]
    in_recs_num = recs[recs.orientation == u'in'][u'train_number'].count()
    out_recs_num = recs[recs.orientation == u'out'][u'train_number'].count()

    if in_recs_num:
        df.loc[index, u'in_brd'] = recs[
                recs.orientation == u'in'][u'boarding'].sum()    / \
                (in_recs_num * CAR_CAPACITY)
        df.loc[index, u'in_alt'] = recs[
                recs.orientation == u'in'][u'alighting'].sum()   / \
                (in_recs_num * CAR_CAPACITY)
    if out_recs_num:
        df.loc[index, u'out_brd'] = recs[
                recs.orientation == u'out'][u'boarding'].sum()  / \
                (out_recs_num * CAR_CAPACITY)
        df.loc[index, u'out_alt'] = recs[
                recs.orientation == u'out'][u'alighting'].sum() / \
                (out_recs_num * CAR_CAPACITY)

推荐答案

您需要将Series转换为具有float64的dtype,而不是当前的object.这是一个说明差异的示例.请注意,通常object dtype Series用途有限,最常见的情况是包含字符串的Series.除此之外,它们非常慢,因为它们无法利用任何数据类型信息.

You need to convert your Series to have a dtype of float64 instead of your current object. Here's an example to illustrate the difference. Note that in general object dtype Series are of limited use, the most common case being a Series containing strings. Other than that they are very slow since they cannot take advantage of any data type information.

In [9]: s = Series(randn(6), index=pd.date_range('2013-01-01 11:25:00', freq='5T', periods=6), dtype=object)

In [10]: s.iloc[1:3] = nan

In [11]: s
Out[11]:
2013-01-01 11:25:00   -0.69522
2013-01-01 11:30:00        NaN
2013-01-01 11:35:00        NaN
2013-01-01 11:40:00   -0.70308
2013-01-01 11:45:00    -1.5653
2013-01-01 11:50:00    0.95893
Freq: 5T, dtype: object

In [12]: s.interpolate(method='time')
Out[12]:
2013-01-01 11:25:00   -0.69522
2013-01-01 11:30:00        NaN
2013-01-01 11:35:00        NaN
2013-01-01 11:40:00   -0.70308
2013-01-01 11:45:00    -1.5653
2013-01-01 11:50:00    0.95893
Freq: 5T, dtype: object

In [13]: s.astype(float).interpolate(method='time')
Out[13]:
2013-01-01 11:25:00   -0.6952
2013-01-01 11:30:00   -0.6978
2013-01-01 11:35:00   -0.7005
2013-01-01 11:40:00   -0.7031
2013-01-01 11:45:00   -1.5653
2013-01-01 11:50:00    0.9589
Freq: 5T, dtype: float64

这篇关于pandas.Series.interpolate()不执行任何操作.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆