| <!DOCTYPE html> | |
| <html lang="en" data-content_root="../../" data-theme="auto"> | |
| <head> | |
| <meta charset="utf-8" /> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" /> | |
| <title>Functions — Apache DataFusion in Python documentation</title> | |
| <script data-cfasync="false"> | |
| document.documentElement.dataset.mode = localStorage.getItem("mode") || "auto"; | |
| document.documentElement.dataset.theme = localStorage.getItem("theme") || "auto"; | |
| </script> | |
| <!-- | |
| this give us a css class that will be invisible only if js is disabled | |
| --> | |
| <noscript> | |
| <style> | |
| .pst-js-only { display: none !important; } | |
| </style> | |
| </noscript> | |
| <!-- Loaded before other Sphinx assets --> | |
| <link href="../../_static/styles/theme.css?digest=8878045cc6db502f8baf" rel="stylesheet" /> | |
| <link href="../../_static/styles/pydata-sphinx-theme.css?digest=8878045cc6db502f8baf" rel="stylesheet" /> | |
| <link rel="stylesheet" type="text/css" href="../../_static/pygments.css?v=8f2a1f02" /> | |
| <link rel="stylesheet" type="text/css" href="../../_static/mystnb.11b39860a7a0cbfd473a3ad8a317855267ff0bd372690045ca344a6b62be495e.css" /> | |
| <link rel="stylesheet" type="text/css" href="../../_static/graphviz.css?v=4ae1632d" /> | |
| <link rel="stylesheet" type="text/css" href="../../_static/copybutton.css?v=76b2166b" /> | |
| <link rel="stylesheet" type="text/css" href="../../_static/theme_overrides.css?v=4af573bc" /> | |
| <!-- So that users can add custom icons --> | |
| <script src="../../_static/scripts/fontawesome.js?digest=8878045cc6db502f8baf"></script> | |
| <!-- Pre-loaded scripts that we'll load fully later --> | |
| <link rel="preload" as="script" href="../../_static/scripts/bootstrap.js?digest=8878045cc6db502f8baf" /> | |
| <link rel="preload" as="script" href="../../_static/scripts/pydata-sphinx-theme.js?digest=8878045cc6db502f8baf" /> | |
| <script src="../../_static/documentation_options.js?v=5929fcd5"></script> | |
| <script src="../../_static/doctools.js?v=9bcbadda"></script> | |
| <script src="../../_static/sphinx_highlight.js?v=dc90522c"></script> | |
| <script src="../../_static/clipboard.min.js?v=a7894cd8"></script> | |
| <script src="../../_static/copybutton.js?v=f281be69"></script> | |
| <script>DOCUMENTATION_OPTIONS.pagename = 'user-guide/common-operations/functions';</script> | |
| <script src="../../_static/toc-toggle.js?v=36375f01"></script> | |
| <link rel="icon" href="../../_static/favicon.svg"/> | |
| <link rel="index" title="Index" href="../../genindex.html" /> | |
| <link rel="search" title="Search" href="../../search.html" /> | |
| <link rel="next" title="Spark-Compatible Functions" href="spark-functions.html" /> | |
| <link rel="prev" title="Joins" href="joins.html" /> | |
| <meta name="viewport" content="width=device-width, initial-scale=1"/> | |
| <meta name="docsearch:language" content="en"/> | |
| <meta name="docsearch:version" content="" /> | |
| </head> | |
| <body data-bs-spy="scroll" data-bs-target=".bd-toc-nav" data-offset="180" data-bs-root-margin="0px 0px -60%" data-default-mode="auto"> | |
| <div id="pst-skip-link" class="skip-link d-print-none"><a href="#main-content">Skip to main content</a></div> | |
| <div id="pst-scroll-pixel-helper"></div> | |
| <button type="button" class="btn rounded-pill" id="pst-back-to-top"> | |
| <i class="fa-solid fa-arrow-up"></i>Back to top</button> | |
| <dialog id="pst-search-dialog"> | |
| <form class="bd-search d-flex align-items-center" | |
| action="../../search.html" | |
| method="get"> | |
| <i class="fa-solid fa-magnifying-glass"></i> | |
| <input type="search" | |
| class="form-control" | |
| name="q" | |
| placeholder="Search the docs ..." | |
| aria-label="Search the docs ..." | |
| autocomplete="off" | |
| autocorrect="off" | |
| autocapitalize="off" | |
| spellcheck="false"/> | |
| <span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd>K</kbd></span> | |
| </form> | |
| </dialog> | |
| <div class="pst-async-banner-revealer d-none"> | |
| <aside id="bd-header-version-warning" class="d-none d-print-none" aria-label="Version warning"></aside> | |
| </div> | |
| <header class="bd-header navbar navbar-expand-lg bd-navbar d-print-none"> | |
| <div class="bd-header__inner bd-page-width"> | |
| <button class="pst-navbar-icon sidebar-toggle primary-toggle" aria-label="Site navigation"> | |
| <span class="fa-solid fa-bars"></span> | |
| </button> | |
| <div class="col-lg-3 navbar-header-items__start"> | |
| <div class="navbar-item"> | |
| <a class="navbar-brand logo" href="../../index.html"> | |
| <img src="../../_static/original.svg" class="logo__image only-light" alt="Apache DataFusion in Python"/> | |
| <img src="../../_static/original_dark.svg" class="logo__image only-dark pst-js-only" alt="Apache DataFusion in Python"/> | |
| </a></div> | |
| </div> | |
| <div class="col-lg-9 navbar-header-items"> | |
| <div class="me-auto navbar-header-items__center"> | |
| <div class="navbar-item"> | |
| <nav> | |
| <ul class="bd-navbar-elements navbar-nav"> | |
| <li class="nav-item current active"> | |
| <a class="nav-link nav-internal" href="../index.html"> | |
| User Guide | |
| </a> | |
| </li> | |
| <li class="nav-item "> | |
| <a class="nav-link nav-internal" href="../../extension-guide/index.html"> | |
| Extension Guide | |
| </a> | |
| </li> | |
| <li class="nav-item "> | |
| <a class="nav-link nav-internal" href="../../contributor-guide/index.html"> | |
| Contributor Guide | |
| </a> | |
| </li> | |
| <li class="nav-item "> | |
| <a class="nav-link nav-internal" href="../../autoapi/index.html"> | |
| API Reference | |
| </a> | |
| </li> | |
| <li class="nav-item "> | |
| <a class="nav-link nav-internal" href="../../links.html"> | |
| Links | |
| </a> | |
| </li> | |
| </ul> | |
| </nav></div> | |
| </div> | |
| <div class="navbar-header-items__end"> | |
| <div class="navbar-item navbar-persistent--container"> | |
| <button class="btn search-button-field search-button__button pst-js-only" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip"> | |
| <i class="fa-solid fa-magnifying-glass"></i> | |
| <span class="search-button__default-text">Search</span> | |
| <span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd class="kbd-shortcut__modifier">K</kbd></span> | |
| </button> | |
| </div> | |
| <div class="navbar-item"><ul class="navbar-icon-links" | |
| aria-label="Icon Links"> | |
| <li class="nav-item"> | |
| <a href="https://github.com/apache/datafusion-python" title="GitHub" class="nav-link pst-navbar-icon" rel="noopener" target="_blank" data-bs-toggle="tooltip" data-bs-placement="bottom"><i class="fa-brands fa-github fa-lg" aria-hidden="true"></i> | |
| <span class="sr-only">GitHub</span></a> | |
| </li> | |
| <li class="nav-item"> | |
| <a href="https://docs.rs/datafusion/latest/datafusion/" title="Rust API docs (docs.rs)" class="nav-link pst-navbar-icon" rel="noopener" target="_blank" data-bs-toggle="tooltip" data-bs-placement="bottom"><i class="fa-brands fa-rust fa-lg" aria-hidden="true"></i> | |
| <span class="sr-only">Rust API docs (docs.rs)</span></a> | |
| </li> | |
| </ul></div> | |
| <div class="navbar-item"> | |
| <button class="btn btn-sm nav-link pst-navbar-icon theme-switch-button pst-js-only" aria-label="Color mode" data-bs-title="Color mode" data-bs-placement="bottom" data-bs-toggle="tooltip"> | |
| <i class="theme-switch fa-solid fa-sun fa-lg" data-mode="light" title="Light"></i> | |
| <i class="theme-switch fa-solid fa-moon fa-lg" data-mode="dark" title="Dark"></i> | |
| <i class="theme-switch fa-solid fa-circle-half-stroke fa-lg" data-mode="auto" title="System Settings"></i> | |
| </button></div> | |
| </div> | |
| </div> | |
| <div class="navbar-persistent--mobile"> | |
| <button class="btn search-button-field search-button__button pst-js-only" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip"> | |
| <i class="fa-solid fa-magnifying-glass"></i> | |
| <span class="search-button__default-text">Search</span> | |
| <span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd class="kbd-shortcut__modifier">K</kbd></span> | |
| </button> | |
| </div> | |
| <button class="pst-navbar-icon sidebar-toggle secondary-toggle" aria-label="On this page"> | |
| <span class="fa-solid fa-outdent"></span> | |
| </button> | |
| </div> | |
| </header> | |
| <div class="bd-container"> | |
| <div class="bd-container__inner bd-page-width"> | |
| <dialog id="pst-primary-sidebar-modal"></dialog> | |
| <div id="pst-primary-sidebar" class="bd-sidebar-primary bd-sidebar"> | |
| <div class="sidebar-header-items sidebar-primary__section"> | |
| <div class="sidebar-header-items__center"> | |
| <div class="navbar-item"> | |
| <nav> | |
| <ul class="bd-navbar-elements navbar-nav"> | |
| <li class="nav-item current active"> | |
| <a class="nav-link nav-internal" href="../index.html"> | |
| User Guide | |
| </a> | |
| </li> | |
| <li class="nav-item "> | |
| <a class="nav-link nav-internal" href="../../extension-guide/index.html"> | |
| Extension Guide | |
| </a> | |
| </li> | |
| <li class="nav-item "> | |
| <a class="nav-link nav-internal" href="../../contributor-guide/index.html"> | |
| Contributor Guide | |
| </a> | |
| </li> | |
| <li class="nav-item "> | |
| <a class="nav-link nav-internal" href="../../autoapi/index.html"> | |
| API Reference | |
| </a> | |
| </li> | |
| <li class="nav-item "> | |
| <a class="nav-link nav-internal" href="../../links.html"> | |
| Links | |
| </a> | |
| </li> | |
| </ul> | |
| </nav></div> | |
| </div> | |
| <div class="sidebar-header-items__end"> | |
| <div class="navbar-item"><ul class="navbar-icon-links" | |
| aria-label="Icon Links"> | |
| <li class="nav-item"> | |
| <a href="https://github.com/apache/datafusion-python" title="GitHub" class="nav-link pst-navbar-icon" rel="noopener" target="_blank" data-bs-toggle="tooltip" data-bs-placement="bottom"><i class="fa-brands fa-github fa-lg" aria-hidden="true"></i> | |
| <span class="sr-only">GitHub</span></a> | |
| </li> | |
| <li class="nav-item"> | |
| <a href="https://docs.rs/datafusion/latest/datafusion/" title="Rust API docs (docs.rs)" class="nav-link pst-navbar-icon" rel="noopener" target="_blank" data-bs-toggle="tooltip" data-bs-placement="bottom"><i class="fa-brands fa-rust fa-lg" aria-hidden="true"></i> | |
| <span class="sr-only">Rust API docs (docs.rs)</span></a> | |
| </li> | |
| </ul></div> | |
| <div class="navbar-item"> | |
| <button class="btn btn-sm nav-link pst-navbar-icon theme-switch-button pst-js-only" aria-label="Color mode" data-bs-title="Color mode" data-bs-placement="bottom" data-bs-toggle="tooltip"> | |
| <i class="theme-switch fa-solid fa-sun fa-lg" data-mode="light" title="Light"></i> | |
| <i class="theme-switch fa-solid fa-moon fa-lg" data-mode="dark" title="Dark"></i> | |
| <i class="theme-switch fa-solid fa-circle-half-stroke fa-lg" data-mode="auto" title="System Settings"></i> | |
| </button></div> | |
| </div> | |
| </div> | |
| <div class="sidebar-primary-items__start sidebar-primary__section"> | |
| <div class="sidebar-primary-item"> | |
| <nav class="bd-docs-nav bd-links" aria-label="Section Navigation"> | |
| <p class="bd-links__title" role="heading" aria-level="1">Section Navigation</p> | |
| <div class="bd-toc-item navbar-nav"> | |
| <ul class="current nav bd-sidenav"> | |
| <li class="toctree-l1 current active has-children"><a class="reference internal" href="../index.html">User Guide</a><details open="open"><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul class="current"> | |
| <li class="toctree-l2"><a class="reference internal" href="../introduction.html">Introduction</a></li> | |
| <li class="toctree-l2"><a class="reference internal" href="../basics.html">Concepts</a></li> | |
| <li class="toctree-l2"><a class="reference internal" href="../data-sources.html">Data Sources</a></li> | |
| <li class="toctree-l2 has-children"><a class="reference internal" href="../dataframe/index.html">DataFrames</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul> | |
| <li class="toctree-l3"><a class="reference internal" href="../dataframe/rendering.html">DataFrame Rendering</a></li> | |
| <li class="toctree-l3"><a class="reference internal" href="../dataframe/execution-metrics.html">Execution Metrics</a></li> | |
| </ul> | |
| </details></li> | |
| <li class="toctree-l2 current active has-children"><a class="reference internal" href="index.html">Common Operations</a><details open="open"><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul class="current"> | |
| <li class="toctree-l3"><a class="reference internal" href="views.html">Registering Views</a></li> | |
| <li class="toctree-l3"><a class="reference internal" href="basic-info.html">Basic Operations</a></li> | |
| <li class="toctree-l3"><a class="reference internal" href="select-and-filter.html">Column Selections</a></li> | |
| <li class="toctree-l3"><a class="reference internal" href="expressions.html">Expressions</a></li> | |
| <li class="toctree-l3"><a class="reference internal" href="joins.html">Joins</a></li> | |
| <li class="toctree-l3 current active"><a class="current reference internal" href="#">Functions</a></li> | |
| <li class="toctree-l3"><a class="reference internal" href="spark-functions.html">Spark-Compatible Functions</a></li> | |
| <li class="toctree-l3"><a class="reference internal" href="aggregations.html">Aggregation</a></li> | |
| <li class="toctree-l3"><a class="reference internal" href="windows.html">Window Functions</a></li> | |
| <li class="toctree-l3"><a class="reference internal" href="udf-and-udfa.html">User-Defined Functions</a></li> | |
| </ul> | |
| </details></li> | |
| <li class="toctree-l2 has-children"><a class="reference internal" href="../io/index.html">IO</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul> | |
| <li class="toctree-l3"><a class="reference internal" href="../io/arrow.html">Arrow</a></li> | |
| <li class="toctree-l3"><a class="reference internal" href="../io/avro.html">Avro</a></li> | |
| <li class="toctree-l3"><a class="reference internal" href="../io/csv.html">CSV</a></li> | |
| <li class="toctree-l3"><a class="reference internal" href="../io/json.html">JSON</a></li> | |
| <li class="toctree-l3"><a class="reference internal" href="../io/parquet.html">Parquet</a></li> | |
| <li class="toctree-l3"><a class="reference internal" href="../io/table_provider.html">Custom Table Provider</a></li> | |
| </ul> | |
| </details></li> | |
| <li class="toctree-l2"><a class="reference internal" href="../configuration.html">Configuration</a></li> | |
| <li class="toctree-l2"><a class="reference internal" href="../extensions.html">Using extension libraries</a></li> | |
| <li class="toctree-l2 has-children"><a class="reference internal" href="../distributing-work/index.html">Distributing work</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul> | |
| <li class="toctree-l3"><a class="reference internal" href="../distributing-work/expressions.html">Shipping expressions to workers</a></li> | |
| <li class="toctree-l3"><a class="reference internal" href="../distributing-work/query-engines.html">Distributed query engines</a></li> | |
| </ul> | |
| </details></li> | |
| <li class="toctree-l2"><a class="reference internal" href="../sql.html">SQL</a></li> | |
| <li class="toctree-l2"><a class="reference internal" href="../upgrade-guides.html">Upgrade Guides</a></li> | |
| <li class="toctree-l2"><a class="reference internal" href="../ai-coding-assistants.html">Using AI Coding Assistants</a></li> | |
| </ul> | |
| </details></li> | |
| <li class="toctree-l1 has-children"><a class="reference internal" href="../../extension-guide/index.html">Extension Guide</a><details open="open"><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul> | |
| <li class="toctree-l2"><a class="reference internal" href="../../extension-guide/why-ffi.html">Why FFI</a></li> | |
| <li class="toctree-l2"><a class="reference internal" href="../../extension-guide/capsule-protocol.html">The capsule protocol</a></li> | |
| <li class="toctree-l2"><a class="reference internal" href="../../extension-guide/table-providers.html">Providers and catalogs</a></li> | |
| <li class="toctree-l2"><a class="reference internal" href="../../extension-guide/functions.html">Functions and table functions</a></li> | |
| <li class="toctree-l2"><a class="reference internal" href="../../extension-guide/codecs.html">Extension codecs</a></li> | |
| <li class="toctree-l2"><a class="reference internal" href="../../extension-guide/bundles.html">Extension bundles</a></li> | |
| <li class="toctree-l2"><a class="reference internal" href="../../extension-guide/query-planners.html">Query planners</a></li> | |
| <li class="toctree-l2"><a class="reference internal" href="../../extension-guide/other-components.html">Optimizer rules and configuration</a></li> | |
| <li class="toctree-l2"><a class="reference internal" href="../../extension-guide/sessions.html">Sessions, handles, and lifetimes</a></li> | |
| <li class="toctree-l2"><a class="reference internal" href="../../extension-guide/checklist.html">Extension author checklist</a></li> | |
| </ul> | |
| </details></li> | |
| <li class="toctree-l1 has-children"><a class="reference internal" href="../../contributor-guide/index.html">Contributor Guide</a><details open="open"><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul> | |
| <li class="toctree-l2"><a class="reference internal" href="../../contributor-guide/introduction.html">Introduction</a></li> | |
| <li class="toctree-l2"><a class="reference internal" href="../../contributor-guide/pyo3-guidelines.html">PyO3 binding guidelines</a></li> | |
| <li class="toctree-l2"><a class="reference internal" href="../../contributor-guide/ffi-internals.html">FFI framing internals</a></li> | |
| </ul> | |
| </details></li> | |
| <li class="toctree-l1 has-children"><a class="reference internal" href="../../autoapi/index.html">API Reference</a><details open="open"><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul> | |
| <li class="toctree-l2 has-children"><a class="reference internal" href="../../autoapi/datafusion/index.html">datafusion</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul> | |
| <li class="toctree-l3"><a class="reference internal" href="../../autoapi/datafusion/catalog/index.html">datafusion.catalog</a></li> | |
| <li class="toctree-l3"><a class="reference internal" href="../../autoapi/datafusion/context/index.html">datafusion.context</a></li> | |
| <li class="toctree-l3"><a class="reference internal" href="../../autoapi/datafusion/dataframe/index.html">datafusion.dataframe</a></li> | |
| <li class="toctree-l3"><a class="reference internal" href="../../autoapi/datafusion/dataframe_formatter/index.html">datafusion.dataframe_formatter</a></li> | |
| <li class="toctree-l3"><a class="reference internal" href="../../autoapi/datafusion/expr/index.html">datafusion.expr</a></li> | |
| <li class="toctree-l3"><a class="reference internal" href="../../autoapi/datafusion/extensions/index.html">datafusion.extensions</a></li> | |
| <li class="toctree-l3 has-children"><a class="reference internal" href="../../autoapi/datafusion/functions/index.html">datafusion.functions</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul> | |
| <li class="toctree-l4"><a class="reference internal" href="../../autoapi/datafusion/functions/spark/index.html">datafusion.functions.spark</a></li> | |
| </ul> | |
| </details></li> | |
| <li class="toctree-l3 has-children"><a class="reference internal" href="../../autoapi/datafusion/input/index.html">datafusion.input</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul> | |
| <li class="toctree-l4"><a class="reference internal" href="../../autoapi/datafusion/input/base/index.html">datafusion.input.base</a></li> | |
| <li class="toctree-l4"><a class="reference internal" href="../../autoapi/datafusion/input/location/index.html">datafusion.input.location</a></li> | |
| </ul> | |
| </details></li> | |
| <li class="toctree-l3"><a class="reference internal" href="../../autoapi/datafusion/io/index.html">datafusion.io</a></li> | |
| <li class="toctree-l3"><a class="reference internal" href="../../autoapi/datafusion/ipc/index.html">datafusion.ipc</a></li> | |
| <li class="toctree-l3"><a class="reference internal" href="../../autoapi/datafusion/object_store/index.html">datafusion.object_store</a></li> | |
| <li class="toctree-l3"><a class="reference internal" href="../../autoapi/datafusion/options/index.html">datafusion.options</a></li> | |
| <li class="toctree-l3"><a class="reference internal" href="../../autoapi/datafusion/plan/index.html">datafusion.plan</a></li> | |
| <li class="toctree-l3"><a class="reference internal" href="../../autoapi/datafusion/record_batch/index.html">datafusion.record_batch</a></li> | |
| <li class="toctree-l3"><a class="reference internal" href="../../autoapi/datafusion/substrait/index.html">datafusion.substrait</a></li> | |
| <li class="toctree-l3"><a class="reference internal" href="../../autoapi/datafusion/unparser/index.html">datafusion.unparser</a></li> | |
| <li class="toctree-l3"><a class="reference internal" href="../../autoapi/datafusion/user_defined/index.html">datafusion.user_defined</a></li> | |
| </ul> | |
| </details></li> | |
| </ul> | |
| </details></li> | |
| <li class="toctree-l1 has-children"><a class="reference internal" href="../../links.html">Links</a><details open="open"><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul> | |
| <li class="toctree-l2"><a class="reference external" href="https://github.com/apache/datafusion-python">GitHub and Issue Tracker</a></li> | |
| <li class="toctree-l2"><a class="reference external" href="https://docs.rs/datafusion/latest/datafusion/">Rust API Docs</a></li> | |
| <li class="toctree-l2"><a class="reference external" href="https://github.com/apache/datafusion/blob/main/CODE_OF_CONDUCT.md">Code of Conduct</a></li> | |
| <li class="toctree-l2"><a class="reference external" href="https://github.com/apache/datafusion-python/tree/main/examples">Examples</a></li> | |
| </ul> | |
| </details></li> | |
| </ul> | |
| </div> | |
| </nav></div> | |
| </div> | |
| <div class="sidebar-primary-items__end sidebar-primary__section"> | |
| <div class="sidebar-primary-item"> | |
| <div id="ethical-ad-placement" | |
| class="flat" | |
| data-ea-publisher="readthedocs" | |
| data-ea-type="readthedocs-sidebar" | |
| data-ea-manual="true"> | |
| </div></div> | |
| </div> | |
| </div> | |
| <main id="main-content" class="bd-main" role="main"> | |
| <div class="bd-content"> | |
| <div class="bd-article-container"> | |
| <div class="bd-header-article d-print-none"> | |
| <div class="header-article-items header-article__inner"> | |
| <div class="header-article-items__start"> | |
| <div class="header-article-item"> | |
| <nav aria-label="Breadcrumb" class="d-print-none"> | |
| <ul class="bd-breadcrumbs"> | |
| <li class="breadcrumb-item breadcrumb-home"> | |
| <a href="../../index.html" class="nav-link" aria-label="Home"> | |
| <i class="fa-solid fa-home"></i> | |
| </a> | |
| </li> | |
| <li class="breadcrumb-item"><a href="../index.html" class="nav-link">User Guide</a></li> | |
| <li class="breadcrumb-item"><a href="index.html" class="nav-link">Common Operations</a></li> | |
| <li class="breadcrumb-item active" aria-current="page"><span class="ellipsis">Functions</span></li> | |
| </ul> | |
| </nav> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| <div id="searchbox"></div> | |
| <article class="bd-article"> | |
| <!--- | |
| Licensed to the Apache Software Foundation (ASF) under one | |
| or more contributor license agreements. See the NOTICE file | |
| distributed with this work for additional information | |
| regarding copyright ownership. The ASF licenses this file | |
| to you under the Apache License, Version 2.0 (the | |
| "License"); you may not use this file except in compliance | |
| with the License. You may obtain a copy of the License at | |
| http://www.apache.org/licenses/LICENSE-2.0 | |
| Unless required by applicable law or agreed to in writing, | |
| software distributed under the License is distributed on an | |
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| KIND, either express or implied. See the License for the | |
| specific language governing permissions and limitations | |
| under the License. | |
| --> | |
| <section id="functions"> | |
| <h1>Functions<a class="headerlink" href="#functions" title="Link to this heading">#</a></h1> | |
| <p>DataFusion provides a large number of built-in functions for performing complex queries without requiring user-defined functions. | |
| In here we will cover some of the more popular use cases. If you want to view all the functions go to the <a class="reference internal" href="../../autoapi/datafusion/functions/index.html#module-datafusion.functions" title="datafusion.functions"><code class="xref py py-mod docutils literal notranslate"><span class="pre">Functions</span></code></a> API Reference.</p> | |
| <div class="admonition note"> | |
| <p class="admonition-title">Note</p> | |
| <p>For Apache Spark-compatible versions of these functions (with Spark | |
| NULL-propagation, 1-indexed substrings, HALF_UP rounding, etc.), see | |
| <a class="reference internal" href="spark-functions.html"><span class="std std-doc">Spark-Compatible Functions</span></a>.</p> | |
| </div> | |
| <p>We’ll use the pokemon dataset in the following examples.</p> | |
| <div class="cell docutils container"> | |
| <div class="cell_input docutils container"> | |
| <div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">datafusion</span><span class="w"> </span><span class="kn">import</span> <span class="n">SessionContext</span> | |
| <span class="n">ctx</span> <span class="o">=</span> <span class="n">SessionContext</span><span class="p">()</span> | |
| <span class="n">ctx</span><span class="o">.</span><span class="n">register_csv</span><span class="p">(</span><span class="s2">"pokemon"</span><span class="p">,</span> <span class="s2">"pokemon.csv"</span><span class="p">)</span> | |
| <span class="n">df</span> <span class="o">=</span> <span class="n">ctx</span><span class="o">.</span><span class="n">table</span><span class="p">(</span><span class="s2">"pokemon"</span><span class="p">)</span> | |
| </pre></div> | |
| </div> | |
| </div> | |
| </div> | |
| <section id="mathematical"> | |
| <h2>Mathematical<a class="headerlink" href="#mathematical" title="Link to this heading">#</a></h2> | |
| <p>DataFusion offers mathematical functions such as <a class="reference internal" href="../../autoapi/datafusion/functions/index.html#datafusion.functions.pow" title="datafusion.functions.pow"><code class="xref py py-func docutils literal notranslate"><span class="pre">pow()</span></code></a> or <a class="reference internal" href="../../autoapi/datafusion/functions/index.html#datafusion.functions.log" title="datafusion.functions.log"><code class="xref py py-func docutils literal notranslate"><span class="pre">log()</span></code></a></p> | |
| <div class="cell docutils container"> | |
| <div class="cell_input docutils container"> | |
| <div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">datafusion</span><span class="w"> </span><span class="kn">import</span> <span class="n">col</span><span class="p">,</span> <span class="n">literal</span> | |
| <span class="kn">from</span><span class="w"> </span><span class="nn">datafusion</span><span class="w"> </span><span class="kn">import</span> <span class="n">functions</span> <span class="k">as</span> <span class="n">f</span> | |
| <span class="n">df</span><span class="o">.</span><span class="n">select</span><span class="p">(</span> | |
| <span class="n">f</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="n">col</span><span class="p">(</span><span class="s1">'"Attack"'</span><span class="p">),</span> <span class="n">literal</span><span class="p">(</span><span class="mi">2</span><span class="p">))</span> <span class="o">-</span> <span class="n">f</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="n">col</span><span class="p">(</span><span class="s1">'"Defense"'</span><span class="p">),</span> <span class="n">literal</span><span class="p">(</span><span class="mi">2</span><span class="p">))</span> | |
| <span class="p">)</span><span class="o">.</span><span class="n">limit</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> | |
| </pre></div> | |
| </div> | |
| </div> | |
| <div class="cell_output docutils container"> | |
| <div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>DataFrame() | |
| +------------------------------------------------------------------+ | |
| | power(pokemon.Attack,Int64(2)) - power(pokemon.Defense,Int64(2)) | | |
| +------------------------------------------------------------------+ | |
| | 0.0 | | |
| | -125.0 | | |
| | -165.0 | | |
| | -5129.0 | | |
| | 855.0 | | |
| | 732.0 | | |
| | 972.0 | | |
| | 4579.0 | | |
| | 4732.0 | | |
| | -1921.0 | | |
| +------------------------------------------------------------------+ | |
| </pre></div> | |
| </div> | |
| </div> | |
| </div> | |
| </section> | |
| <section id="conditional"> | |
| <h2>Conditional<a class="headerlink" href="#conditional" title="Link to this heading">#</a></h2> | |
| <p>There 3 conditional functions in DataFusion <a class="reference internal" href="../../autoapi/datafusion/functions/index.html#datafusion.functions.coalesce" title="datafusion.functions.coalesce"><code class="xref py py-func docutils literal notranslate"><span class="pre">coalesce()</span></code></a>, <a class="reference internal" href="../../autoapi/datafusion/functions/index.html#datafusion.functions.nullif" title="datafusion.functions.nullif"><code class="xref py py-func docutils literal notranslate"><span class="pre">nullif()</span></code></a> and <a class="reference internal" href="../../autoapi/datafusion/functions/index.html#datafusion.functions.case" title="datafusion.functions.case"><code class="xref py py-func docutils literal notranslate"><span class="pre">case()</span></code></a>.</p> | |
| <div class="cell docutils container"> | |
| <div class="cell_input docutils container"> | |
| <div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">df</span><span class="o">.</span><span class="n">select</span><span class="p">(</span> | |
| <span class="n">f</span><span class="o">.</span><span class="n">coalesce</span><span class="p">(</span><span class="n">col</span><span class="p">(</span><span class="s1">'"Type 1"'</span><span class="p">),</span> <span class="n">col</span><span class="p">(</span><span class="s1">'"Type 2"'</span><span class="p">))</span><span class="o">.</span><span class="n">alias</span><span class="p">(</span><span class="s2">"dominant_type"</span><span class="p">)</span> | |
| <span class="p">)</span><span class="o">.</span><span class="n">limit</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> | |
| </pre></div> | |
| </div> | |
| </div> | |
| <div class="cell_output docutils container"> | |
| <div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>DataFrame() | |
| +---------------+ | |
| | dominant_type | | |
| +---------------+ | |
| | Grass | | |
| | Grass | | |
| | Grass | | |
| | Grass | | |
| | Fire | | |
| | Fire | | |
| | Fire | | |
| | Fire | | |
| | Fire | | |
| | Water | | |
| +---------------+ | |
| </pre></div> | |
| </div> | |
| </div> | |
| </div> | |
| </section> | |
| <section id="temporal"> | |
| <h2>Temporal<a class="headerlink" href="#temporal" title="Link to this heading">#</a></h2> | |
| <p>For selecting the current time use <a class="reference internal" href="../../autoapi/datafusion/functions/index.html#datafusion.functions.now" title="datafusion.functions.now"><code class="xref py py-func docutils literal notranslate"><span class="pre">now()</span></code></a></p> | |
| <div class="cell docutils container"> | |
| <div class="cell_input docutils container"> | |
| <div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">df</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">now</span><span class="p">())</span> | |
| </pre></div> | |
| </div> | |
| </div> | |
| <div class="cell_output docutils container"> | |
| <div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>DataFrame() | |
| +-------------------------------+ | |
| | now() | | |
| +-------------------------------+ | |
| | 2026-10-12T19:09:11.091449105 | | |
| | 2026-10-12T19:09:11.091449105 | | |
| | 2026-10-12T19:09:11.091449105 | | |
| | 2026-10-12T19:09:11.091449105 | | |
| | 2026-10-12T19:09:11.091449105 | | |
| | 2026-10-12T19:09:11.091449105 | | |
| | 2026-10-12T19:09:11.091449105 | | |
| | 2026-10-12T19:09:11.091449105 | | |
| | 2026-10-12T19:09:11.091449105 | | |
| | 2026-10-12T19:09:11.091449105 | | |
| +-------------------------------+ | |
| Data truncated. | |
| </pre></div> | |
| </div> | |
| </div> | |
| </div> | |
| <p>Convert to timestamps using <a class="reference internal" href="../../autoapi/datafusion/functions/index.html#datafusion.functions.to_timestamp" title="datafusion.functions.to_timestamp"><code class="xref py py-func docutils literal notranslate"><span class="pre">to_timestamp()</span></code></a></p> | |
| <div class="cell docutils container"> | |
| <div class="cell_input docutils container"> | |
| <div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">df</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">to_timestamp</span><span class="p">(</span><span class="n">col</span><span class="p">(</span><span class="s1">'"Total"'</span><span class="p">))</span><span class="o">.</span><span class="n">alias</span><span class="p">(</span><span class="s2">"timestamp"</span><span class="p">))</span> | |
| </pre></div> | |
| </div> | |
| </div> | |
| <div class="cell_output docutils container"> | |
| <div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>DataFrame() | |
| +---------------------+ | |
| | timestamp | | |
| +---------------------+ | |
| | 1970-01-01T00:05:18 | | |
| | 1970-01-01T00:06:45 | | |
| | 1970-01-01T00:08:45 | | |
| | 1970-01-01T00:10:25 | | |
| | 1970-01-01T00:05:09 | | |
| | 1970-01-01T00:06:45 | | |
| | 1970-01-01T00:08:54 | | |
| | 1970-01-01T00:10:34 | | |
| | 1970-01-01T00:10:34 | | |
| | 1970-01-01T00:05:14 | | |
| +---------------------+ | |
| Data truncated. | |
| </pre></div> | |
| </div> | |
| </div> | |
| </div> | |
| <p>Extracting parts of a date using <a class="reference internal" href="../../autoapi/datafusion/functions/index.html#datafusion.functions.date_part" title="datafusion.functions.date_part"><code class="xref py py-func docutils literal notranslate"><span class="pre">date_part()</span></code></a> (alias <a class="reference internal" href="../../autoapi/datafusion/functions/index.html#datafusion.functions.extract" title="datafusion.functions.extract"><code class="xref py py-func docutils literal notranslate"><span class="pre">extract()</span></code></a>)</p> | |
| <div class="cell docutils container"> | |
| <div class="cell_input docutils container"> | |
| <div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">df</span><span class="o">.</span><span class="n">select</span><span class="p">(</span> | |
| <span class="n">f</span><span class="o">.</span><span class="n">date_part</span><span class="p">(</span><span class="s2">"month"</span><span class="p">,</span> <span class="n">f</span><span class="o">.</span><span class="n">to_timestamp</span><span class="p">(</span><span class="n">col</span><span class="p">(</span><span class="s1">'"Total"'</span><span class="p">)))</span><span class="o">.</span><span class="n">alias</span><span class="p">(</span><span class="s2">"month"</span><span class="p">),</span> | |
| <span class="n">f</span><span class="o">.</span><span class="n">extract</span><span class="p">(</span><span class="s2">"day"</span><span class="p">,</span> <span class="n">f</span><span class="o">.</span><span class="n">to_timestamp</span><span class="p">(</span><span class="n">col</span><span class="p">(</span><span class="s1">'"Total"'</span><span class="p">)))</span><span class="o">.</span><span class="n">alias</span><span class="p">(</span><span class="s2">"day"</span><span class="p">)</span> | |
| <span class="p">)</span> | |
| </pre></div> | |
| </div> | |
| </div> | |
| <div class="cell_output docutils container"> | |
| <div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>DataFrame() | |
| +-------+-----+ | |
| | month | day | | |
| +-------+-----+ | |
| | 1 | 1 | | |
| | 1 | 1 | | |
| | 1 | 1 | | |
| | 1 | 1 | | |
| | 1 | 1 | | |
| | 1 | 1 | | |
| | 1 | 1 | | |
| | 1 | 1 | | |
| | 1 | 1 | | |
| | 1 | 1 | | |
| +-------+-----+ | |
| Data truncated. | |
| </pre></div> | |
| </div> | |
| </div> | |
| </div> | |
| </section> | |
| <section id="string"> | |
| <h2>String<a class="headerlink" href="#string" title="Link to this heading">#</a></h2> | |
| <p>In the field of data science, working with textual data is a common task. To make string manipulation easier, | |
| DataFusion offers a range of helpful options.</p> | |
| <div class="cell docutils container"> | |
| <div class="cell_input docutils container"> | |
| <div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">df</span><span class="o">.</span><span class="n">select</span><span class="p">(</span> | |
| <span class="n">f</span><span class="o">.</span><span class="n">char_length</span><span class="p">(</span><span class="n">col</span><span class="p">(</span><span class="s1">'"Name"'</span><span class="p">))</span><span class="o">.</span><span class="n">alias</span><span class="p">(</span><span class="s2">"len"</span><span class="p">),</span> | |
| <span class="n">f</span><span class="o">.</span><span class="n">lower</span><span class="p">(</span><span class="n">col</span><span class="p">(</span><span class="s1">'"Name"'</span><span class="p">))</span><span class="o">.</span><span class="n">alias</span><span class="p">(</span><span class="s2">"lower"</span><span class="p">),</span> | |
| <span class="n">f</span><span class="o">.</span><span class="n">left</span><span class="p">(</span><span class="n">col</span><span class="p">(</span><span class="s1">'"Name"'</span><span class="p">),</span> <span class="n">literal</span><span class="p">(</span><span class="mi">4</span><span class="p">))</span><span class="o">.</span><span class="n">alias</span><span class="p">(</span><span class="s2">"code"</span><span class="p">)</span> | |
| <span class="p">)</span> | |
| </pre></div> | |
| </div> | |
| </div> | |
| <div class="cell_output docutils container"> | |
| <div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>DataFrame() | |
| +-----+---------------------------+------+ | |
| | len | lower | code | | |
| +-----+---------------------------+------+ | |
| | 9 | bulbasaur | Bulb | | |
| | 7 | ivysaur | Ivys | | |
| | 8 | venusaur | Venu | | |
| | 21 | venusaurmega venusaur | Venu | | |
| | 10 | charmander | Char | | |
| | 10 | charmeleon | Char | | |
| | 9 | charizard | Char | | |
| | 25 | charizardmega charizard x | Char | | |
| | 25 | charizardmega charizard y | Char | | |
| | 8 | squirtle | Squi | | |
| +-----+---------------------------+------+ | |
| Data truncated. | |
| </pre></div> | |
| </div> | |
| </div> | |
| </div> | |
| <p>This also includes the functions for regular expressions like <a class="reference internal" href="../../autoapi/datafusion/functions/index.html#datafusion.functions.regexp_replace" title="datafusion.functions.regexp_replace"><code class="xref py py-func docutils literal notranslate"><span class="pre">regexp_replace()</span></code></a> and <a class="reference internal" href="../../autoapi/datafusion/functions/index.html#datafusion.functions.regexp_match" title="datafusion.functions.regexp_match"><code class="xref py py-func docutils literal notranslate"><span class="pre">regexp_match()</span></code></a></p> | |
| <div class="cell docutils container"> | |
| <div class="cell_input docutils container"> | |
| <div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">df</span><span class="o">.</span><span class="n">select</span><span class="p">(</span> | |
| <span class="n">f</span><span class="o">.</span><span class="n">regexp_match</span><span class="p">(</span><span class="n">col</span><span class="p">(</span><span class="s1">'"Name"'</span><span class="p">),</span> <span class="n">literal</span><span class="p">(</span><span class="s2">"Char"</span><span class="p">))</span><span class="o">.</span><span class="n">alias</span><span class="p">(</span><span class="s2">"dragons"</span><span class="p">),</span> | |
| <span class="n">f</span><span class="o">.</span><span class="n">regexp_replace</span><span class="p">(</span><span class="n">col</span><span class="p">(</span><span class="s1">'"Name"'</span><span class="p">),</span> <span class="n">literal</span><span class="p">(</span><span class="s2">"saur"</span><span class="p">),</span> <span class="n">literal</span><span class="p">(</span><span class="s2">"fleur"</span><span class="p">))</span><span class="o">.</span><span class="n">alias</span><span class="p">(</span><span class="s2">"flowers"</span><span class="p">)</span> | |
| <span class="p">)</span> | |
| </pre></div> | |
| </div> | |
| </div> | |
| <div class="cell_output docutils container"> | |
| <div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>DataFrame() | |
| +---------+---------------------------+ | |
| | dragons | flowers | | |
| +---------+---------------------------+ | |
| | | Bulbafleur | | |
| | | Ivyfleur | | |
| | | Venufleur | | |
| | | VenufleurMega Venusaur | | |
| | [Char] | Charmander | | |
| | [Char] | Charmeleon | | |
| | [Char] | Charizard | | |
| | [Char] | CharizardMega Charizard X | | |
| | [Char] | CharizardMega Charizard Y | | |
| | | Squirtle | | |
| +---------+---------------------------+ | |
| Data truncated. | |
| </pre></div> | |
| </div> | |
| </div> | |
| </div> | |
| </section> | |
| <section id="casting"> | |
| <h2>Casting<a class="headerlink" href="#casting" title="Link to this heading">#</a></h2> | |
| <p>Casting expressions to different data types using <a class="reference internal" href="../../autoapi/datafusion/functions/index.html#datafusion.functions.arrow_cast" title="datafusion.functions.arrow_cast"><code class="xref py py-func docutils literal notranslate"><span class="pre">arrow_cast()</span></code></a></p> | |
| <div class="cell docutils container"> | |
| <div class="cell_input docutils container"> | |
| <div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">df</span><span class="o">.</span><span class="n">select</span><span class="p">(</span> | |
| <span class="n">f</span><span class="o">.</span><span class="n">arrow_cast</span><span class="p">(</span><span class="n">col</span><span class="p">(</span><span class="s1">'"Total"'</span><span class="p">),</span> <span class="s2">"Float64"</span><span class="p">)</span><span class="o">.</span><span class="n">alias</span><span class="p">(</span><span class="s2">"total_as_float"</span><span class="p">),</span> | |
| <span class="n">f</span><span class="o">.</span><span class="n">arrow_cast</span><span class="p">(</span><span class="n">col</span><span class="p">(</span><span class="s1">'"Total"'</span><span class="p">),</span> <span class="s2">"Int32"</span><span class="p">)</span><span class="o">.</span><span class="n">alias</span><span class="p">(</span><span class="s2">"total_as_int"</span><span class="p">)</span> | |
| <span class="p">)</span> | |
| </pre></div> | |
| </div> | |
| </div> | |
| <div class="cell_output docutils container"> | |
| <div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>DataFrame() | |
| +----------------+--------------+ | |
| | total_as_float | total_as_int | | |
| +----------------+--------------+ | |
| | 318.0 | 318 | | |
| | 405.0 | 405 | | |
| | 525.0 | 525 | | |
| | 625.0 | 625 | | |
| | 309.0 | 309 | | |
| | 405.0 | 405 | | |
| | 534.0 | 534 | | |
| | 634.0 | 634 | | |
| | 634.0 | 634 | | |
| | 314.0 | 314 | | |
| +----------------+--------------+ | |
| Data truncated. | |
| </pre></div> | |
| </div> | |
| </div> | |
| </div> | |
| </section> | |
| <section id="other"> | |
| <h2>Other<a class="headerlink" href="#other" title="Link to this heading">#</a></h2> | |
| <p>The function <a class="reference internal" href="../../autoapi/datafusion/functions/index.html#datafusion.functions.in_list" title="datafusion.functions.in_list"><code class="xref py py-func docutils literal notranslate"><span class="pre">in_list()</span></code></a> allows to check a column for the presence of multiple values:</p> | |
| <div class="cell docutils container"> | |
| <div class="cell_input docutils container"> | |
| <div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">types</span> <span class="o">=</span> <span class="p">[</span><span class="n">literal</span><span class="p">(</span><span class="s2">"Grass"</span><span class="p">),</span> <span class="n">literal</span><span class="p">(</span><span class="s2">"Fire"</span><span class="p">),</span> <span class="n">literal</span><span class="p">(</span><span class="s2">"Water"</span><span class="p">)]</span> | |
| <span class="p">(</span> | |
| <span class="n">df</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">in_list</span><span class="p">(</span><span class="n">col</span><span class="p">(</span><span class="s1">'"Type 1"'</span><span class="p">),</span> <span class="n">types</span><span class="p">,</span> <span class="n">negated</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span><span class="o">.</span><span class="n">alias</span><span class="p">(</span><span class="s2">"basic_types"</span><span class="p">))</span> | |
| <span class="o">.</span><span class="n">limit</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span> | |
| <span class="o">.</span><span class="n">to_pandas</span><span class="p">()</span> | |
| <span class="p">)</span> | |
| </pre></div> | |
| </div> | |
| </div> | |
| <div class="cell_output docutils container"> | |
| <div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span> basic_types | |
| 0 True | |
| 1 True | |
| 2 True | |
| 3 True | |
| 4 True | |
| 5 True | |
| 6 True | |
| 7 True | |
| 8 True | |
| 9 True | |
| 10 True | |
| 11 True | |
| 12 True | |
| 13 False | |
| 14 False | |
| 15 False | |
| 16 False | |
| 17 False | |
| 18 False | |
| 19 False | |
| </pre></div> | |
| </div> | |
| </div> | |
| </div> | |
| </section> | |
| </section> | |
| <section id="handling-missing-values"> | |
| <h1>Handling Missing Values<a class="headerlink" href="#handling-missing-values" title="Link to this heading">#</a></h1> | |
| <p>DataFusion provides methods to handle missing values in DataFrames:</p> | |
| <section id="fill-null"> | |
| <h2>fill_null<a class="headerlink" href="#fill-null" title="Link to this heading">#</a></h2> | |
| <p>The <code class="docutils literal notranslate"><span class="pre">fill_null()</span></code> method replaces NULL values in specified columns with a provided value:</p> | |
| <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Fill all NULL values with 0 where possible</span> | |
| <span class="n">df</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">fill_null</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> | |
| <span class="c1"># Fill NULL values only in specific string columns</span> | |
| <span class="n">df</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">fill_null</span><span class="p">(</span><span class="s2">"missing"</span><span class="p">,</span> <span class="n">subset</span><span class="o">=</span><span class="p">[</span><span class="s2">"name"</span><span class="p">,</span> <span class="s2">"category"</span><span class="p">])</span> | |
| </pre></div> | |
| </div> | |
| <p>The fill value will be cast to match each column’s type. If casting fails for a column, that column remains unchanged.</p> | |
| </section> | |
| <section id="fill-nan"> | |
| <h2>fill_nan<a class="headerlink" href="#fill-nan" title="Link to this heading">#</a></h2> | |
| <p>The <code class="docutils literal notranslate"><span class="pre">fill_nan()</span></code> method replaces NaN values in floating-point columns. NaN is | |
| distinct from NULL, which <code class="docutils literal notranslate"><span class="pre">fill_null()</span></code> handles:</p> | |
| <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Replace NaN with 0.0 in every floating-point column</span> | |
| <span class="n">df</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">fill_nan</span><span class="p">(</span><span class="mf">0.0</span><span class="p">)</span> | |
| <span class="c1"># Replace NaN only in specific columns</span> | |
| <span class="n">df</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">fill_nan</span><span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="n">subset</span><span class="o">=</span><span class="p">[</span><span class="s2">"price"</span><span class="p">])</span> | |
| </pre></div> | |
| </div> | |
| </section> | |
| <section id="column-names-with-uppercase-letters-or-dots"> | |
| <span id="fill-column-names"></span><h2>Column names with uppercase letters or dots<a class="headerlink" href="#column-names-with-uppercase-letters-or-dots" title="Link to this heading">#</a></h2> | |
| <p><code class="docutils literal notranslate"><span class="pre">fill_null()</span></code> and <code class="docutils literal notranslate"><span class="pre">fill_nan()</span></code> fail on a DataFrame that has any column whose | |
| name contains an uppercase letter or a <code class="docutils literal notranslate"><span class="pre">.</span></code>, even when that column is not in | |
| <code class="docutils literal notranslate"><span class="pre">subset</span></code> | |
| (<a class="reference external" href="https://github.com/apache/datafusion/issues/25829">apache/datafusion#25829</a>):</p> | |
| <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">df</span> <span class="o">=</span> <span class="n">ctx</span><span class="o">.</span><span class="n">from_pydict</span><span class="p">({</span><span class="s2">"Price"</span><span class="p">:</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="s2">"nan"</span><span class="p">),</span> <span class="mf">2.0</span><span class="p">],</span> <span class="s2">"qty"</span><span class="p">:</span> <span class="p">[</span><span class="nb">float</span><span class="p">(</span><span class="s2">"nan"</span><span class="p">),</span> <span class="mf">1.0</span><span class="p">]})</span> | |
| <span class="n">df</span><span class="o">.</span><span class="n">fill_nan</span><span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="n">subset</span><span class="o">=</span><span class="p">[</span><span class="s2">"qty"</span><span class="p">])</span> | |
| <span class="c1"># Schema error: No field named price. Did you mean '..."Price"'?</span> | |
| </pre></div> | |
| </div> | |
| <p>Until that is fixed, rename such columns first. Quote the old name so it is | |
| not normalized:</p> | |
| <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">df</span><span class="o">.</span><span class="n">with_column_renamed</span><span class="p">(</span><span class="s1">'"Price"'</span><span class="p">,</span> <span class="s2">"price"</span><span class="p">)</span><span class="o">.</span><span class="n">fill_nan</span><span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="n">subset</span><span class="o">=</span><span class="p">[</span><span class="s2">"qty"</span><span class="p">])</span> | |
| </pre></div> | |
| </div> | |
| </section> | |
| </section> | |
| </article> | |
| <footer class="prev-next-footer d-print-none"> | |
| <div class="prev-next-area"> | |
| <a class="left-prev" | |
| href="joins.html" | |
| title="previous page"> | |
| <i class="fa-solid fa-angle-left"></i> | |
| <div class="prev-next-info"> | |
| <p class="prev-next-subtitle">previous</p> | |
| <p class="prev-next-title">Joins</p> | |
| </div> | |
| </a> | |
| <a class="right-next" | |
| href="spark-functions.html" | |
| title="next page"> | |
| <div class="prev-next-info"> | |
| <p class="prev-next-subtitle">next</p> | |
| <p class="prev-next-title">Spark-Compatible Functions</p> | |
| </div> | |
| <i class="fa-solid fa-angle-right"></i> | |
| </a> | |
| </div> | |
| </footer> | |
| </div> | |
| <dialog id="pst-secondary-sidebar-modal"></dialog> | |
| <div id="pst-secondary-sidebar" class="bd-sidebar-secondary bd-toc"><div class="sidebar-secondary-items sidebar-secondary__inner"> | |
| <div class="sidebar-secondary-item"> | |
| <div | |
| id="pst-page-navigation-heading-2" | |
| class="page-toc tocsection onthispage"> | |
| <i class="fa-solid fa-list"></i> On this page | |
| </div> | |
| <nav class="bd-toc-nav page-toc" aria-labelledby="pst-page-navigation-heading-2"> | |
| <ul class="visible nav section-nav flex-column"> | |
| <li class="toc-h1 nav-item toc-entry"><a class="reference internal nav-link" href="#">Functions</a><ul class="visible nav section-nav flex-column"> | |
| <li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#mathematical">Mathematical</a></li> | |
| <li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#conditional">Conditional</a></li> | |
| <li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#temporal">Temporal</a></li> | |
| <li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#string">String</a></li> | |
| <li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#casting">Casting</a></li> | |
| <li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#other">Other</a></li> | |
| </ul> | |
| </li> | |
| <li class="toc-h1 nav-item toc-entry"><a class="reference internal nav-link" href="#handling-missing-values">Handling Missing Values</a><ul class="visible nav section-nav flex-column"> | |
| <li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#fill-null">fill_null</a></li> | |
| <li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#fill-nan">fill_nan</a></li> | |
| <li class="toc-h2 nav-item toc-entry"><a class="reference internal nav-link" href="#column-names-with-uppercase-letters-or-dots">Column names with uppercase letters or dots</a></li> | |
| </ul> | |
| </li> | |
| </ul> | |
| </nav></div> | |
| </div></div> | |
| </div> | |
| <footer class="bd-footer-content"> | |
| </footer> | |
| </main> | |
| </div> | |
| </div> | |
| <!-- Scripts loaded after <body> so the DOM is not blocked --> | |
| <script defer src="../../_static/scripts/bootstrap.js?digest=8878045cc6db502f8baf"></script> | |
| <script defer src="../../_static/scripts/pydata-sphinx-theme.js?digest=8878045cc6db502f8baf"></script> | |
| <!-- Based on pydata_sphinx_theme/footer.html --> | |
| <footer class="footer mt-5 mt-md-0"> | |
| <div class="container"> | |
| <div class="footer-item"> | |
| <p>Apache Arrow DataFusion, Arrow DataFusion, Apache, the Apache feather logo, and the Apache Arrow DataFusion project logo</p> | |
| <p>are either registered trademarks or trademarks of The Apache Software Foundation in the United States and other countries.</p> | |
| </div> | |
| </div> | |
| </footer> | |
| </body> | |
| </html> |