尝试在名称列表中前进的for循环中按名称对df进行子集化:
names = ['Name','Name','Name']
>>> type(names[0])
<type 'str'>
df有一个名字&#39;包含字符串的对象类型的列,当在本地运行时,表达式
for i in names:
csum = time_data.ix[:,(time_data['Name'].values==i)]
或
csum = time_data.loc[time_data['Name']==i]
工作得很好。但是,一旦将应用程序部署到GAE灵活,pandas就会抛出
KeyError: False
表示第一个表达式和
TypeError: invalid type comparison
第二个。这是Pandas版本0.22和python 2.7.14。谁能告诉我发生了什么以及如何解决它?!
df看起来像:
>>> time_data.head()
Date Name In AM Out AM In PM Out PM
0 2018-01-04 TESTY TEST 7:03:24 AM 12:06:24 PM 12:38:06 PM 5:34:30 PM
1 2018-01-07 TESTY TEST 3:14:25 PM NaN NaN NaN
>>> time_data.dtypes
Date datetime64[ns]
Name object
In AM object
Out AM object
In PM object
Out PM object
dtype: object
>>> type(time_data.Name[0])
<type 'str'>
完整追溯:
/env/local/lib/python2.7/site-packages/pandas/core/ops.py:816:
FutureWarning: elementwise comparison failed; returning scalar instead, but
in the future will perform elementwise comparison
Traceback (most recent call last): File "/env/local/lib/python2.7/site-
packages/flask/app.py", line 1982, in wsgi_app response =
self.full_dispatch_request() File "/env/local/lib/python2.7/site-
packages/flask/app.py", line 1614, in full_dispatch_request rv =
self.handle_user_exception(e) File "/env/local/lib/python2.7/site-
packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb) File "/env/local/lib/python2.7/site-
packages/flask/app.py", line 1612, in full_dispatch_request rv =
self.dispatch_request() File "/env/local/lib/python2.7/site-
packages/flask/app.py", line 1598, in dispatch_request return
self.view_functions[rule.endpoint](**req.view_args) File
"/home/vmagent/app/timecard_report/views.py", line 192, in preview mask =
(time_data['Name']==i) File "/env/local/lib/python2.7/site-
packages/pandas/core/ops.py", line 879, in wrapper res = na_op(values,
other) File "/env/local/lib/python2.7/site-packages/pandas/core/ops.py",
line 818, in na_op raise TypeError("invalid type comparison") TypeError:
invalid type comparison
Traceback (most recent call last): File "/env/local/lib/python2.7/site-
packages/flask/app.py", line 1982, in wsgi_app response =
self.full_dispatch_request() File "/env/local/lib/python2.7/site-
packages/flask/app.py", line 1614, in full_dispatch_request rv =
self.handle_user_exception(e) File "/env/local/lib/python2.7/site-
packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb) File "/env/local/lib/python2.7/site-
packages/flask/app.py", line 1612, in full_dispatch_request rv =
self.dispatch_request() File "/env/local/lib/python2.7/site-
packages/flask/app.py", line 1598, in dispatch_request return
self.view_functions[rule.endpoint](**req.view_args) File
"/home/vmagent/app/timecard_report/views.py", line 192, in preview csum =
time_data.ix[:,(time_data['Name'].values==i)] File
"/env/local/lib/python2.7/site-packages/pandas/core/indexing.py", line 121,
in __getitem__ return self._getitem_tuple(key) File
"/env/local/lib/python2.7/site-packages/pandas/core/indexing.py", line 858,
in _getitem_tuple return self._getitem_lowerdim(tup) File
"/env/local/lib/python2.7/site-packages/pandas/core/indexing.py", line 991,
in _getitem_lowerdim section = self._getitem_axis(key, axis=i) File
"/env/local/lib/python2.7/site-packages/pandas/core/indexing.py", line 1108,
in _getitem_axis return self._get_label(key, axis=axis) File
"/env/local/lib/python2.7/site-packages/pandas/core/indexing.py", line 145,
in _get_label return self.obj._xs(label, axis=axis) File
"/env/local/lib/python2.7/site-packages/pandas/core/generic.py", line 2335,
in xs return self[key] File "/env/local/lib/python2.7/site-
packages/pandas/core/frame.py", line 2139, in __getitem__ return
self._getitem_column(key) File "/env/local/lib/python2.7/site-
packages/pandas/core/frame.py", line 2146, in _getitem_column return
self._get_item_cache(key) File "/env/local/lib/python2.7/site-
packages/pandas/core/generic.py", line 1842, in _get_item_cache values =
self._data.get(item) File "/env/local/lib/python2.7/site-
packages/pandas/core/internals.py", line 3843, in get loc =
self.items.get_loc(item) File "/env/local/lib/python2.7/site-
packages/pandas/core/indexes/base.py", line 2527, in get_loc return
self._engine.get_loc(self._maybe_cast_indexer(key)) File
"pandas/_libs/index.pyx", line 117, in
pandas._libs.index.IndexEngine.get_loc File "pandas/_libs/index.pyx", line
139, in pandas._libs.index.IndexEngine.get_loc File
"pandas/_libs/hashtable_class_helper.pxi", line 1265, in
pandas._libs.hashtable.PyObjectHashTable.get_item File
"pandas/_libs/hashtable_class_helper.pxi", line 1273, in
pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: False
答案 0 :(得分:0)
出于某种原因,GAE熊猫只会接受:
csum = time_data.query('Name=='+ "'"+i+"'")
_(ツ)_ /¯