如何在FastAPI上应用多个过滤器

问题描述 投票:0回答:1

我正在尝试在我的 FastApi 代码上应用第二个过滤器,当前的单个过滤器工作正常,但是当我尝试添加另一个过滤器代码时,fastapi 服务器继续运行无结果。

当前代码:

@app.get("/Orderid")
def read_data(id: str = None,
              token: str = Depends(oauth2_scheme)):
    try:
        user = fake_decode_token(token)
        if not user:
            raise HTTPException(
                status_code=status.HTTP_401_UNAUTHORIZED,
                detail= "Invalid authentication credentials",
                headers={"WWW-Authenticate": "Bearer"},
            )
        if id:
            filtered_df  = sale[sale["Order ID"].str.contains(id, case=False)]
            return filtered_df.to_dict(orient='records')
        else:
            return sale.to_dict(orient='records')
    except Exception as e:
        return {"error" : str(e)}

下面的代码我尝试过但有错误

@app.get("/Orderid")
def read_data(id: str = None,
              date: str = None,
              token: str = Depends(oauth2_scheme)):
    try:
        user = fake_decode_token(token)
        if not user:
            raise HTTPException(
                status_code=status.HTTP_401_UNAUTHORIZED,
                detail= "Invalid authentication credentials",
                headers={"WWW-Authenticate": "Bearer"},
            )
        if id and date:
            filtered_df  = (sale[sale["Order ID"].str.contains(id, case=False)] and sale[sale["Order Date"].str.contains(id, case=False)])
            return filtered_df.to_dict(orient='records')
        else:
            return sale.to_dict(orient='records')
    except Exception as e:
        return {"error" : str(e)}

我使用了以下链接中的示例数据 https://excelbianalytics.com/wp/wp-content/uploads/2017/07/100-Sales-Records.zip

python fastapi
1个回答
0
投票

这似乎与用于将多个过滤器应用于数据帧的逻辑有关。括号内的表达式错误地使用了 and 运算符。在 pandas 中,您应该使用按位运算符(& - 和、| - 或)来组合多个条件。

您可以单独添加 ID 和日期过滤器来处理提供其中一个或两者的情况。

@app.get("/Orderid")
def read_data(id: str = None,
              date: str = None,
              token: str = Depends(oauth2_scheme)):
    try:
        user = fake_decode_token(token)
        if not user:
            raise HTTPException(
                status_code=status.HTTP_401_UNAUTHORIZED,
                detail="Invalid authentication credentials",
                headers={"WWW-Authenticate": "Bearer"},
            )

        filtered_df = sale

        if id:
            filtered_df = filtered_df[filtered_df["Order ID"].str.contains(id, case=False)]
        
        if date:
            filtered_df = filtered_df[filtered_df["Order Date"].str.contains(date, case=False)]

        return filtered_df.to_dict(orient='records')
        
    except Exception as e:
        return {"error": str(e)}

或者使用&运算符组合多个条件。

@app.get("/Orderid")
def read_data(id: str = None,
              date: str = None,
              token: str = Depends(oauth2_scheme)):
    try:
        user = fake_decode_token(token)
        if not user:
            raise HTTPException(
                status_code=status.HTTP_401_UNAUTHORIZED,
                detail="Invalid authentication credentials",
                headers={"WWW-Authenticate": "Bearer"},
            )

                condition = pd.Series([True] * len(sale))
        
        if id:
            condition &= sale["Order ID"].str.contains(id, case=False)
        
        if date:
            condition &= sale["Order Date"].str.contains(date, case=False)
        
        filtered_df = sale[condition]

        return filtered_df.to_dict(orient='records')
        
    except Exception as e:
        return {"error": str(e)}
© www.soinside.com 2019 - 2024. All rights reserved.