Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 熊猫:应用函数:类型错误:不支持的操作数类型-:“unicode”和“unicode”_Python_Python 2.7_Pandas - Fatal编程技术网

Python 熊猫:应用函数:类型错误:不支持的操作数类型-:“unicode”和“unicode”

Python 熊猫:应用函数:类型错误:不支持的操作数类型-:“unicode”和“unicode”,python,python-2.7,pandas,Python,Python 2.7,Pandas,我有以下数据帧df: 然后,我使用以下代码在event_time字段中查找这两个元素的持续时间: 但是,我得到了以下错误: TypeError unsupported operand type(s) for -: 'unicode' and 'unicode' TypeErrorTraceback (most recent call last) <ipython-input-7-7a191c6f2678> in <module>() ----> 1 df['dur

我有以下数据帧df:

然后,我使用以下代码在event_time字段中查找这两个元素的持续时间:

但是,我得到了以下错误:

TypeError unsupported operand type(s) for -: 'unicode' and 'unicode' 
TypeErrorTraceback (most recent call last)
<ipython-input-7-7a191c6f2678> in <module>()
----> 1 df['duration'] = df.event_time.apply(lambda x:x[1][1]-x[0][1])

/opt/conda/envs/python2/lib/python2.7/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
   2218         else:
   2219             values = self.asobject
-> 2220             mapped = lib.map_infer(values, f, convert=convert_dtype)
   2221 
   2222         if len(mapped) and isinstance(mapped[0], Series):

pandas/src/inference.pyx in pandas.lib.map_infer (pandas/lib.c:62658)()

<ipython-input-7-7a191c6f2678> in <lambda>(x)
----> 1 df['duration'] = df.event_time.apply(lambda x:x[1][1]-x[0][1])

TypeError: unsupported operand type(s) for -: 'unicode' and 'unicode'

知道我做错了什么吗?谢谢

我相信您需要通过str[]选择值,转换并减去:

s1 = pd.to_datetime(df['event_time'].str[1].str[1])
s2 = pd.to_datetime(df['event_time'].str[0].str[1])
df['duration'] =  s1 - s2
print (df)
    name                                         event_time         duration
0   Mary  [(S, 2017-12-03T03:40:20.000Z), (V, 2017-12-07...  3 days 23:11:12
1  Peter  [(S, 2017-11-02T01:11:10.000Z), (V, 2017-11-19... 17 days 06:12:02
2   Andy  [(S, 2017-12-01T10:31:15.000Z), (V, 2017-12-09...  8 days 01:59:55

每个df.event\u时间的类型是什么?它是unicode字符串吗?看起来您正在尝试计算时差。x[1][1]和x[0][1]是lambda中的unicode,不支持-运算符。看起来您正在从df中提取两个字符串格式的不同日期,然后尝试从另一个日期中减去一个日期,这是这些unicode字符串无法做到的。您可能需要将长字符串日期格式转换为一些数值或数据结构,然后进行区分,如果您需要这样做的话。
TypeError unsupported operand type(s) for -: 'unicode' and 'unicode' 
TypeErrorTraceback (most recent call last)
<ipython-input-7-7a191c6f2678> in <module>()
----> 1 df['duration'] = df.event_time.apply(lambda x:x[1][1]-x[0][1])

/opt/conda/envs/python2/lib/python2.7/site-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
   2218         else:
   2219             values = self.asobject
-> 2220             mapped = lib.map_infer(values, f, convert=convert_dtype)
   2221 
   2222         if len(mapped) and isinstance(mapped[0], Series):

pandas/src/inference.pyx in pandas.lib.map_infer (pandas/lib.c:62658)()

<ipython-input-7-7a191c6f2678> in <lambda>(x)
----> 1 df['duration'] = df.event_time.apply(lambda x:x[1][1]-x[0][1])

TypeError: unsupported operand type(s) for -: 'unicode' and 'unicode'
s1 = pd.to_datetime(df['event_time'].str[1].str[1])
s2 = pd.to_datetime(df['event_time'].str[0].str[1])
df['duration'] =  s1 - s2
print (df)
    name                                         event_time         duration
0   Mary  [(S, 2017-12-03T03:40:20.000Z), (V, 2017-12-07...  3 days 23:11:12
1  Peter  [(S, 2017-11-02T01:11:10.000Z), (V, 2017-11-19... 17 days 06:12:02
2   Andy  [(S, 2017-12-01T10:31:15.000Z), (V, 2017-12-09...  8 days 01:59:55