Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 <https://pandas.pydata.org/pdeps/0017-backwards-compatibility-and-deprecation-policy.html>`_ for more details.
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/col.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading