熊猫钥匙错误日期

时间:2016-02-19 04:14:58

标签: python datetime pandas dataframe

df['ts'] = pd.to_datetime(df['_created_at'])
df = df.set_index('ts')

def f(x):
    x = x.reindex(df.index)
    x = x.sort_values('battery')
    x['ts'] = x['ts'].fillna(method='ffill')  
    x['battery'] = x['battery'].combine_first(df['battery'])
    x['model'] = x['model'].combine_first(df['model'])
    x['user'] = x['user'].combine_first(df['user'])
    x['version'] = x['version'].combine_first(df['version'])
    return x

我有上面的代码,当我到达x['ts'] = x['ts'].fillna(method='ffill')行时似乎遇到了错误。当我运行以下命令时会发生这种情况:

df = df.groupby(level=0, sort=False).apply(f).reset_index(level=0, drop=True).reset_index()

我的ts值看起来像是:2013-03-04 13:56:29.662并且是datetime64;我不明白我在做什么错误导致ts上的这个关键错误,因为我认为将它们视为to_datetime会将索引放入pandas理解的格式中。关于如何解决这个问题的想法?

1 个答案:

答案 0 :(得分:1)

我认为您必须省略这个有问题的行,因为列ts设置为index并且填充值为x.reindex(df.index)。我认为您需要按drop删除列_created_at

print df
               _created_at user  battery model  version
0  2013-03-04 13:56:29.662    R        3     A        1
1  2013-03-05 13:56:29.662    S        5     B        3
2  2013-03-06 13:56:29.662    J        6     C        2

df['ts'] = pd.to_datetime(df['_created_at'])

df = df.drop('_created_at', axis=1)

df = df.set_index(['ts'])

def f(x):
    #print x
    x = x.reindex(df.index)
    x = x.sort_values('battery')
    #x['ts'] = x['ts'].fillna(method='ffill')  
    x['battery'] = x['battery'].combine_first(df['battery'])
    x['model'] = x['model'].combine_first(df['model'])
    x['user'] = x['user'].combine_first(df['user'])
    x['version'] = x['version'].combine_first(df['version'])
    return x

df = df.groupby(level=0, sort=False).apply(f).reset_index(level=0, drop=True).reset_index()
print df
                       ts user  battery model  version
0 2013-03-04 13:56:29.662    R        3     A        1
1 2013-03-05 13:56:29.662    S        5     B        3
2 2013-03-06 13:56:29.662    J        6     C        2
3 2013-03-05 13:56:29.662    S        5     B        3
4 2013-03-04 13:56:29.662    R        3     A        1
5 2013-03-06 13:56:29.662    J        6     C        2
6 2013-03-06 13:56:29.662    J        6     C        2
7 2013-03-04 13:56:29.662    R        3     A        1
8 2013-03-05 13:56:29.662    S        5     B        3

但是,对于其他专栏,您可能需要fillna,例如user

df['ts'] = pd.to_datetime(df['_created_at'])

df = df.drop('_created_at', axis=1)

df = df.set_index(['ts'])

def f(x):
    #print x
    x = x.reindex(df.index)
    x = x.sort_values('battery')
    #x['ts'] = x['ts'].fillna(method='ffill')
    x['battery'] = x['battery'].combine_first(df['battery'])
    x['model'] = x['model'].combine_first(df['model'])
    x['user'] = x['user'].fillna(method='ffill')  
    x['version'] = x['version'].combine_first(df['version'])
    return x

df = df.groupby(level=0, sort=False).apply(f).reset_index(level=0, drop=True).reset_index()
print df
                       ts user  battery model  version
0 2013-03-04 13:56:29.662    R        3     A        1
1 2013-03-05 13:56:29.662    R        5     B        3
2 2013-03-06 13:56:29.662    R        6     C        2
3 2013-03-05 13:56:29.662    S        5     B        3
4 2013-03-04 13:56:29.662    S        3     A        1
5 2013-03-06 13:56:29.662    S        6     C        2
6 2013-03-06 13:56:29.662    J        6     C        2
7 2013-03-04 13:56:29.662    J        3     A        1
8 2013-03-05 13:56:29.662    J        5     B        3