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

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 VS2022 C++的相关代码问题咨询
  • ¥15 如果要做一个老年人平板有哪些需求
  • ¥15 k8s生产配置推荐配置及部署方案
  • ¥15 matlab提取运动物体的坐标
  • ¥15 人大金仓下载,有人知道怎么解决吗
  • ¥15 一个小问题,本人刚入门,哪位可以help
  • ¥30 python安卓开发
  • ¥15 使用R语言GD包一直不出结果
  • ¥15 计算机微处理器与接口技术相关问题,求解答图片的这个问题,有多少个端口,端口地址和解答问题的方法和思路,不要AI作答
  • ¥15 如何根据一个截图编写对应的HTML代码