{
  "name": "DataFrame.query() expression forms and their boolean-mask equivalents in pandas 3.1.1",
  "description": "Compares query-string syntax elements with the equivalent boolean-mask or isin code, compiled from the pandas user guide and API reference for DataFrame.query (version 3.1.1), with the engine behaviour the documentation states for each.",
  "page": "https://casrai.org/guides/pandas-query",
  "publisher": "CASRAI",
  "license": "https://creativecommons.org/licenses/by/4.0/",
  "dateModified": "2026-11-13T06:01:00Z",
  "temporalCoverage": "pandas 3.1.1",
  "spatialCoverage": null,
  "columns": [
    {
      "name": "Query element",
      "unit": "",
      "description": ""
    },
    {
      "name": "query() expression",
      "unit": "",
      "description": ""
    },
    {
      "name": "Equivalent in plain pandas",
      "unit": "",
      "description": ""
    },
    {
      "name": "Evaluation note in the documentation",
      "unit": "",
      "description": ""
    }
  ],
  "rows": [
    {
      "Query element": "Chained boolean condition",
      "query() expression": "df.query('(a < b) & (b < c)')",
      "Equivalent in plain pandas": "df[(df['a'] < df['b']) & (df['b'] < df['c'])]",
      "Evaluation note in the documentation": "The docs show 'a < b & b < c' and 'a < b and b < c' as equivalent under the default parser"
    },
    {
      "Query element": "Chained comparison",
      "query() expression": "df.query('a < b < c')",
      "Equivalent in plain pandas": "df[(df['a'] < df['b']) & (df['b'] < df['c'])]",
      "Evaluation note in the documentation": "Listed in the docs as equivalent to the explicit form"
    },
    {
      "Query element": "Membership",
      "query() expression": "df.query('a in b')",
      "Equivalent in plain pandas": "df[df['a'].isin(df['b'])]",
      "Evaluation note in the documentation": "Evaluated in Python; numexpr has no equivalent"
    },
    {
      "Query element": "Negated membership",
      "query() expression": "df.query('a not in b')",
      "Equivalent in plain pandas": "df[~df['a'].isin(df['b'])]",
      "Evaluation note in the documentation": "Evaluated in Python; numexpr has no equivalent"
    },
    {
      "Query element": "Equality with a list",
      "query() expression": "df.query('b == [\"a\",\"b\",\"c\"]')",
      "Equivalent in plain pandas": "df[df['b'].isin(['a','b','c'])]",
      "Evaluation note in the documentation": "The docs state == with a list behaves like in"
    },
    {
      "Query element": "Boolean column negation",
      "query() expression": "df.query('~bools')",
      "Equivalent in plain pandas": "df[~df['bools']]",
      "Evaluation note in the documentation": "'not bools' is documented as an alternative spelling"
    },
    {
      "Query element": "Local Python variable",
      "query() expression": "df.query('A > @local_var')",
      "Equivalent in plain pandas": "df[df['A'] > local_var]",
      "Evaluation note in the documentation": "@ refers to a variable in the local or global namespace"
    },
    {
      "Query element": "Column name with special characters",
      "query() expression": "df.query('B == `C&C`')",
      "Equivalent in plain pandas": "df[df['B'] == df['C&C']]",
      "Evaluation note in the documentation": "Backticks are required for names with spaces or operator characters"
    }
  ],
  "sources": [
    {
      "title": "pandas user guide: Indexing and selecting data, The query() Method",
      "url": "https://pandas.pydata.org/docs/user_guide/indexing.html",
      "accessed": "2026-11-13"
    },
    {
      "title": "pandas.DataFrame.query API reference (3.1.1)",
      "url": "https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.query.html",
      "accessed": "2026-11-13"
    }
  ]
}