adamzhl 2021-12-28 16:52 采纳率: 64.7%
浏览 444
已结题

AttributeError: 'PandasArray' object has no attribute '_str_contains

在调试的过程中,table 过滤语法更新,代码检查了没有问题。pandas已经更新到最新版本,1.34版本。


def update_table(page_current, page_size, sort_by, filter):
    """接受当前页码、行数,排列、过滤条件"""
    #拆分组合查询条件
    filtering_expressions = filter.split(' && ')
    #挨个条件处理
    dff = server.expmtHandler.experiment_list_df
    for filter_part in filtering_expressions:
        #将拆分条件传递给拆分函数,得到列,操作符,条件值
        col_name, operator, filter_value = split_filter_part(filter_part)

        if operator in ('eq', 'ne', 'lt', 'le', 'gt', 'ge'):
            # these operators match pandas series operator method names
            dff = dff.loc[getattr(dff[col_name], operator)(filter_value)]
        elif operator == 'contains':
            dff = dff.loc[dff[col_name].str.contains(filter_value)]
        elif operator == 'datestartswith':
            
            # this is a simplification of the front-end filtering logic,
            # only works with complete fields in standard format
            dff = dff.loc[dff[col_name].str.startswith(filter_value)]

    if len(sort_by):
        dff = dff.sort_values(
            [col['column_id'] for col in sort_by],
            ascending=[
                col['direction'] == 'asc'
                for col in sort_by
            ],
            inplace=False
        )

    page = page_current
    size = page_size
    return dff.iloc[page * size: (page + 1) * size].to_dict('records')


# 条件拆分函数
def split_filter_part(filter_part):
    operators = [['ge ', '>='],
             ['le ', '<='],
             ['lt ', '<'],
             ['gt ', '>'],
             ['ne ', '!='],
             ['eq ', '='],
             ['contains '],
             ['datestartswith ']]
    # 循环自定义过滤类型
    for operator_type in operators:
        # 循环每个类型的两个元素
        for operator in operator_type:
            # 循环的元素(操作符)哎传入的过滤符号列表中
            if operator in filter_part:
                # 操作符分割传入的条件,只分割一次
                name_part, value_part = filter_part.split(operator, 1)
                # 取出大括号中的name
                name = name_part[name_part.find('{') + 1: name_part.rfind('}')]
                # 去掉空括号
                value_part = value_part.strip()
                # 特殊字符处理
                v0 = value_part[0]
                if (v0 == value_part[-1] and v0 in ("'", '"', '`')):
                    value = value_part[1: -1].replace('\\' + v0, v0)
                else:
                    try:
                        value = float(value_part)
                    except ValueError:
                        value = value_part

                # 在筛选字符串中,单词运算符后面需要空格,处理后就不需要啦
                # word operators need spaces after them in the filter string,
                # but we don't want these later
                return name, operator_type[0].strip(), value
    # 条件为空返回3个None
    return [None] * 3

img

###### 运行结果及报错内容 


```python

Traceback (most recent call last):
  File "/Users/zhaoliu/Workspace/bee/app.py", line 96, in update_table
    dff = dff[dff[col_name].str.contains(filter_value)]
  File "/usr/local/anaconda3/envs/python37/lib/python3.7/site-packages/pandas/core/strings/accessor.py", line 101, in wrapper
    return func(self, *args, **kwargs)
  File "/usr/local/anaconda3/envs/python37/lib/python3.7/site-packages/pandas/core/strings/accessor.py", line 1111, in contains
    result = self._array._str_contains(pat, case, flags, na, regex)
AttributeError: 'PandasArray' object has no attribute '_str_contains'

AttributeError: 'PandasArray' object has no attribute '_str_contains
File "/usr/local/anaconda3/envs/python37/lib/python3.7/site-packages/pandas/core/strings/accessor.py", line 101, in wrapper
                msg = (
                    f"Cannot use .str.{func_name} with values of "
                    f"inferred dtype '{self._inferred_dtype}'."
                )
                raise TypeError(msg)
            return func(self, *args, **kwargs)
 
        wrapper.__name__ = func_name
        return wrapper
 
    return _forbid_nonstring_types
File "/usr/local/anaconda3/envs/python37/lib/python3.7/site-packages/pandas/core/strings/accessor.py", line 1111, in contains
result = self._array._str_contains(pat, case, flags, na, regex)
AttributeError: 'PandasArray' object has no attribute '_str_contains


  • 写回答

4条回答 默认 最新

  • heianduck 2021-12-29 16:34
    关注

    dff = dff.loc[dff[col_name].str.contains(filter_value)] 把这个".loc"删掉试试?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 1月7日
  • 已采纳回答 12月30日
  • 修改了问题 12月29日
  • 修改了问题 12月29日
  • 展开全部

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集