TypeError :("无法比较类型'时间戳'类型' str'",'发生在索引262224')

时间:2018-03-19 08:28:35

标签: python-3.x pandas

我正在尝试从datetime列创建日期标志。但在应用以下功能后出错。

def f(r):
    if r['balance_dt'] <= '2016-11-30':
        return 0
    else:
        return 1
df_obctohdfc['balance_dt_flag'] = df_obctohdfc.apply(f,axis=1)

2 个答案:

答案 0 :(得分:1)

在熊猫中最好避免循环,如何在引擎盖下工作apply

我认为需要将字符串转换为日期时间,然后将掩码转换为integer - True1False0并更改<= } >

timestamp = pd.to_datetime('2016-11-30')
df_obctohdfc['balance_dt_flag'] = (df_obctohdfc['balance_dt'] > timestamp).astype(int)

<强>示例

rng = pd.date_range('2016-11-27', periods=10)
df_obctohdfc = pd.DataFrame({'balance_dt': rng})  
#print (df_obctohdfc)

timestamp = pd.to_datetime('2016-11-30')
df_obctohdfc['balance_dt_flag'] = (df_obctohdfc['balance_dt'] > timestamp).astype(int)
print (df_obctohdfc)

  balance_dt  balance_dt_flag
0 2016-11-27                0
1 2016-11-28                0
2 2016-11-29                0
3 2016-11-30                0
4 2016-12-01                1
5 2016-12-02                1
6 2016-12-03                1
7 2016-12-04                1
8 2016-12-05                1
9 2016-12-06                1

比较1000行DataFrame:

In [140]: %timeit df_obctohdfc['balance_dt_flag1'] = (df_obctohdfc['balance_dt'] > timestamp).astype(int)
1000 loops, best of 3: 368 µs per loop

In [141]: %timeit df_obctohdfc['balance_dt_flag2'] = df_obctohdfc.apply(f,axis=1)
10 loops, best of 3: 91.2 ms per loop

<强>设置

rng = pd.date_range('2015-11-01', periods=1000)
df_obctohdfc = pd.DataFrame({'balance_dt': rng})  
#print (df_obctohdfc)

timestamp = pd.to_datetime('2016-11-30')

import datetime
def f(r):
    if r['balance_dt'] <= datetime.datetime.strptime('2016-11-30', '%Y-%m-%d'):
        return 0
    else:
        return 1

答案 1 :(得分:1)

您获得的错误是因为您正在将字符串对象与datetime对象进行比较。您可以将字符串转换为日期时间。

<强>实施例

import datetime
def f(r):
    if r['balance_dt'] <= datetime.datetime.strptime('2016-11-30', '%Y-%m-%d'):
        return 0
    else:
        return 1
df_obctohdfc['balance_dt_flag'] = df_obctohdfc.apply(f,axis=1)

注意:最好采用 jezrael 提及的方式。这是正确的方法