Pandas用单位插入数据

Jul*_*ien 5 python numpy pandas

嗨,大家好,

我一直在寻找Stackoverflow几年,它帮助了我很多,以至于我以前从来没有注册过:)

但今天我仍然坚持使用Python与熊猫和数量的问题(也可能是unum或品脱).我尽力做一个明确的帖子,但由于这是我的第一个,我道歉如果有些令人困惑,并会尝试纠正你会发现的任何错误:)


我想从源导入数据并构建Pandas数据帧,如下所示:

import pandas as pd
import quantities as pq

depth = [0.0,1.1,2.0] * pq.m
depth2 = [0,1,1.1,1.5,2] * pq.m

s1 = pd.DataFrame(
        {'depth' : [x for x in depth]},
        index = depth)
Run Code Online (Sandbox Code Playgroud)

这给出了:

S1=
     depth
0.0  0.0 m
1.1  1.1 m
2.0  2.0 m
Run Code Online (Sandbox Code Playgroud)

现在我想将数据扩展到depth2值:(显然没有指向深度插入深度,但在它变得更复杂之前它是一个测试).

s2 = s1.reindex(depth2)
Run Code Online (Sandbox Code Playgroud)

这给出了:

S2=
      depth
0.0   0.0 m
1.0   NaN
1.1   1.1 m
1.5   NaN
2.0   2.0 m
Run Code Online (Sandbox Code Playgroud)

到目前为止没问题.


但是当我尝试插入缺失的值时:

s2['depth'].interpolate(method='values')
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

C:\Python27\lib\site-packages\numpy\lib\function_base.pyc in interp(x, xp, fp, left, right)
   1067         return compiled_interp([x], xp, fp, left, right).item()
   1068     else:
-> 1069         return compiled_interp(x, xp, fp, left, right)
  1070 
  1071 
TypeError: Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe'
Run Code Online (Sandbox Code Playgroud)

我明白从numpy插值不适用于对象.


但是,如果我现在尝试通过删除单位来插入缺失值,它可以工作:

s3 = s2['depth'].astype(float).interpolate(method='values')
Run Code Online (Sandbox Code Playgroud)

这给出了:

s3 = 
0.0   0
1.0   1
1.1   1.1
1.5   1.5
2.0   2
Name: depth, dtype: object
Run Code Online (Sandbox Code Playgroud)

如何在深度列中取回装置?

我找不到任何把它放回去的技巧......

任何帮助将不胜感激.谢谢

Jul*_*ien 0

好吧,我找到了一个解决方案,可能不是最好的,但对于我的问题来说它工作得很好:

import pandas as pd
import quantities as pq

def extendAndInterpolate(input, newIndex):
""" Function to extend a panda dataframe and interpolate
"""
output = pd.concat([input, pd.DataFrame(index=newIndex)], axis=1)

for col in output.columns:
    # (1) Try to retrieve the unit of the current column
    try:
        # if it succeeds, then store the unit
        unit = 1 * output[col][0].units    
    except Exception, e:
        # if it fails, which means that the column contains string
        # then return 1
        unit = 1

    # (2) Check the type of value.
    if isinstance(output[col][0], basestring):
        # if it's a string return the string and fill the missing cell with this string
        value = output[col].ffill()
    else:
        # if it's a value, to be able to interpolate, you need to:
        #   - (a) dump the unit with astype(float)
        #   - (b) interpolate the value
        #   - (c) add again the unit
        value = [x*unit for x in output[col].astype(float).interpolate(method='values')]
    #
    # (3) Returned the extended pandas table with the interpolated values    
    output[col] = pd.Series(value, index=output.index)
# Return the output dataframe
return output
Run Code Online (Sandbox Code Playgroud)

然后:

depth = [0.0,1.1,2.0] * pq.m
depth2 = [0,1,1.1,1.5,2] * pq.m

s1 = pd.DataFrame(
        {'depth' : [x for x in depth]},
        index = depth)

s2 = extendAndInterpolate(s1, depth2)
Run Code Online (Sandbox Code Playgroud)

结果:

s1
     depth
0.0  0.0 m
1.1  1.1 m
2.0  2.0 m

s2     
     depth
0.0  0.0 m
1.0  1.0 m
1.1  1.1 m
1.5  1.5 m
2.0  2.0 m
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助。