Python 3.x TypeError:(“无法将类型';时间戳';与类型';str';,';进行比较,发生在索引262224';)

Python 3.x TypeError:(“无法将类型';时间戳';与类型';str';,';进行比较,发生在索引262224';),python-3.x,pandas,Python 3.x,Pandas,我正在尝试为date from 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) def(r): 如果熊猫中的r['balance_dt']是避免循环的最佳方法,

我正在尝试为date from 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)
def(r):

如果熊猫中的r['balance_dt']是避免循环的最佳方法,那么如何在引擎盖下应用

我认为需要将字符串转换为日期时间,然后将掩码转换为
integer
-
True
转换为
1
False
转换为
0
,并更改

样本

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
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
比较
1000
行数据帧:

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('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
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
rng=pd.日期范围('2015-11-01',期间=1000)
df_obctohdfc=pd.DataFrame({'balance_dt':rng})
#打印(df_obctohdfc)
时间戳=pd.to_datetime('2016-11-30')
导入日期时间
def f(r):

如果r['balance_dt']则您得到的错误是因为您将字符串对象与日期时间对象进行比较。您可以将字符串转换为datetime

Ex:

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)
导入日期时间
def f(r):

如果r['balance_dt']这里是什么意思?@Abhaykumar-need
datetime.datetime.strtime('2016-11-30','%Y-%m-%d')
@Abhaykumar。“对不起,那是个打字错误。”耶兹雷尔说。谢谢:)