Skip to content

PERF: Series.fillna for pyarrow-backed dtypes #49722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 22, 2022

Conversation

lukemanley
Copy link
Member

Perf improvement in Series.fillna for pyarrow-backed dtypes.

      before           after         ratio
    [86f18282]       [34319599]
    <main>           <arrow-ea-fillna>
-        98.6±1ms       18.7±0.4ms     0.19  series_methods.Fillna.time_fillna('string[pyarrow]', 'backfill')
-        99.2±1ms       17.6±0.2ms     0.18  series_methods.Fillna.time_fillna('string[pyarrow]', 'pad')
-       1.86±0.1s      3.86±0.03ms     0.00  series_methods.Fillna.time_fillna('int64[pyarrow]', 'backfill')
-      1.89±0.07s      3.01±0.03ms     0.00  series_methods.Fillna.time_fillna('int64[pyarrow]', 'pad')

SOME BENCHMARKS HAVE CHANGED SIGNIFICANTLY.
PERFORMANCE INCREASED.

@lukemanley lukemanley added Performance Memory or execution speed performance ExtensionArray Extending pandas with custom dtypes or arrays. Arrow pyarrow functionality labels Nov 16, 2022

if method is not None and pa_version_under7p0:
# fill_null_{forward|backward} added in pyarrow 7.0
fallback_performancewarning(version="7")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make sure these are being checked with tm.maybe_produces_warning in the test suite (should show up in the pyarrow=6 build)? Ideally we want to catch all occurrences of these in this test suite

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added. thanks for the reviews

@mroeschke
Copy link
Member

Looking good. Just a single comment

@mroeschke
Copy link
Member

A few other places where this is showing up otherwise LGTM

pandas/tests/extension/test_string.py::TestMissing::test_fillna_no_op_returns_copy[True-pyarrow]
pandas/tests/extension/test_string.py::TestMissing::test_fillna_no_op_returns_copy[False-pyarrow]
  /home/runner/work/pandas/pandas/pandas/tests/extension/base/missing.py:93: PerformanceWarning: Falling back on a non-pyarrow code path which may decrease performance. Upgrade to pyarrow >=7 to possibly suppress this warning.
    result = data.fillna(method="backfill")

pandas/tests/extension/test_string.py::TestMissing::test_fillna_series_method[ffill-True-pyarrow]
pandas/tests/extension/test_string.py::TestMissing::test_fillna_series_method[ffill-False-pyarrow]
pandas/tests/extension/test_string.py::TestMissing::test_fillna_series_method[bfill-True-pyarrow]
pandas/tests/extension/test_string.py::TestMissing::test_fillna_series_method[bfill-False-pyarrow]
  /home/runner/work/pandas/pandas/pandas/tests/extension/base/missing.py:123: PerformanceWarning: Falling back on a non-pyarrow code path which may decrease performance. Upgrade to pyarrow >=7 to possibly suppress this warning.
    result = pd.Series(data_missing).fillna(method=fillna_method)

@lukemanley
Copy link
Member Author

A few other places where this is showing up otherwise LGTM

fixed, thx

@mroeschke mroeschke added this to the 2.0 milestone Nov 21, 2022
@mroeschke mroeschke merged commit 5e1b4b7 into pandas-dev:main Nov 22, 2022
@mroeschke
Copy link
Member

Thanks @lukemanley

mliu08 pushed a commit to mliu08/pandas that referenced this pull request Nov 27, 2022
* ArrowExtensionArray.fillna perf

* whatsnew

* fixes

* tighter try/excepts

* cleanup

* test for performance warning

* test for performance warning
@lukemanley
Copy link
Member Author

cc @mroeschke, @jbrockmendel

It turns out Block.fillna does not dispatch to the underlying arrays in all cases including the simple case where method and limit are None. This highlights the impact of not dispatching for Arrow-backed arrays:

import pandas as pd
import pandas._testing as tm

N = 10**6
arr = pd.array(tm.rands_array(10, N), dtype="string[pyarrow]")
arr[::2] = None
ser = pd.Series(arr)

%timeit ser.fillna("foo")
60.5 ms ± 119 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)     # <- no dispatch

%timeit arr.fillna("foo")
4.89 ms ± 22.5 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)   # <- potential, if dispatched

It looks like the pattern might be to define ArrowBackedExtensionBlock(ExtensionBlock) with a fillna method that dispatches to ArrowExtensionArray.fillna. However, the Block.fillna method is marked final. Any suggestions? Would it be problematic to remove the final attribute?

@mroeschke
Copy link
Member

I imagine ExtensionBlock should generally define and just dispatch to to the array's fillna since (at least for the foreseeable future) they will be 1D?

@jbrockmendel
Copy link
Member

I imagine ExtensionBlock should generally define and just dispatch to to the array's fillna

That makes sense to me. Would want to make sure we're not hurting perf in non-arrow cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Arrow pyarrow functionality ExtensionArray Extending pandas with custom dtypes or arrays. Performance Memory or execution speed performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants