diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index cb8798c4a5565..a516174e6864b 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -130,10 +130,18 @@ and will be removed in pandas 4.0. .. _whatsnew_300.enhancements.col: -``pd.col`` syntax can now be used in :meth:`DataFrame.assign` and :meth:`DataFrame.loc` -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +Initial support for ``pd.col()`` syntax to create expressions +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -You can now use ``pd.col`` to create callables for use in dataframe methods which accept them. For example, if you have a dataframe +This release introduces :func:`col` to refer to a DataFrame column by name +and build up expressions. + +This can be used as a simplified syntax to create callables for use in +methods such as :meth:`DataFrame.assign`. In practice, where you would +have to use a ``lambda`` function before, you can now use ``pd.col()`` +instead. + +For example, if you have a dataframe .. ipython:: python @@ -151,6 +159,18 @@ you can now write: df.assign(c = pd.col('a') + pd.col('b')) +The expression object returned by :func:`col` supports all standard operators +(like ``+``, ``-``, ``*``, ``/``, etc.) and all Series methods and namespaces +(like ``pd.col("name").sum()``, ``pd.col("name").str.upper()``, etc.). + +Currently, the ``pd.col()`` syntax can be used in any place which accepts a +callable that takes the calling DataFrame as first argument and returns a +Series, like ``lambda df: df[col_name]``. +This includes :meth:`DataFrame.assign`, :meth:`DataFrame.loc`, and getitem/setitem. + +It is expected that the support for ``pd.col()`` will be expanded to more methods +in future releases. + New Deprecation Policy ^^^^^^^^^^^^^^^^^^^^^^ pandas 3.0.0 introduces a new 3-stage deprecation policy: using ``DeprecationWarning`` initially, then switching to ``FutureWarning`` for broader visibility in the last minor version before the next major release, and then removal of the deprecated functionality in the major release. This was done to give downstream packages more time to adjust to pandas deprecations, which should reduce the amount of warnings that a user gets from code that isn't theirs. See `PDEP 17 `_ for more details. diff --git a/pandas/core/col.py b/pandas/core/col.py index b8e965845b4b6..a91a53dd19fde 100644 --- a/pandas/core/col.py +++ b/pandas/core/col.py @@ -260,6 +260,8 @@ def col(col_name: Hashable) -> Expression: :meth:`DataFrame.assign` or :meth:`DataFrame.loc`, can also accept ``pd.col(col_name)``. + .. versionadded:: 3.0.0 + Parameters ---------- col_name : Hashable