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
39 changes: 39 additions & 0 deletions asv_bench/benchmarks/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,42 @@ def time_year(self, calendar):

def time_floor(self, calendar):
_ = self.da.time.dt.floor("D")


NSTRINGS = 100_000


@parameterize({"dtype": ("U", "S", "O")})
class StringAccessor:
def setup(self, dtype):
rng = np.random.default_rng(0)
chars = np.array(list("abcdefgh XYZ"))
strings = rng.choice(chars, size=(NSTRINGS, 12)).view("U12").ravel()
self.da = xr.DataArray(strings.astype(dtype), dims="x")

def time_len(self, dtype):
self.da.str.len()

def time_isalpha(self, dtype):
self.da.str.isalpha()

def time_startswith(self, dtype):
self.da.str.startswith("ab")

def time_find(self, dtype):
self.da.str.find("ab")

def time_contains(self, dtype):
self.da.str.contains("ab", regex=False)

def time_strip(self, dtype):
self.da.str.strip()

def time_center(self, dtype):
self.da.str.center(16)

def time_replace(self, dtype):
self.da.str.replace("ab", "-", regex=False)

def time_repeat(self, dtype):
self.da.str.repeat(2)
13 changes: 13 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ Deprecations

Bug Fixes
~~~~~~~~~
- Results of the string accessor methods ``strip``, ``pad``, ``center``,
``ljust``, ``rjust``, ``zfill`` and ``replace`` on dask arrays are no longer
truncated to a single character, and ``repeat`` no longer fails on dask arrays
(:pull:`11746`).
By `Michael Niklas <https://github.com/headtr1ck>`_.
- Encoding a string variable whose ``char_dim_name`` has characters after its
last number (e.g. ``"n4_chars"``) no longer drops them. When the length does
not match, only the number is replaced (``"n3_chars"``) (:pull:`11757`).
Expand Down Expand Up @@ -386,6 +391,14 @@ Documentation

Performance
~~~~~~~~~~~
- The string accessor uses the vectorized functions of ``np.strings`` for
``len``, ``startswith``, ``endswith``, ``find``, ``rfind``, ``index``,
``rindex``, ``strip``, ``lstrip``, ``rstrip``, ``pad``, ``center``,
``ljust``, ``rjust``, ``zfill``, ``repeat``, the ``is*`` methods and
``contains`` and ``replace`` with ``regex=False`` and case-sensitive matching,
which makes them 5-80 times faster for arrays with a string dtype
(:pull:`11746`).
By `Michael Niklas <https://github.com/headtr1ck>`_.
- Speed up rolling reductions of dask arrays without bottleneck, e.g. ``sum``,
``mean`` and ``count``, by about 7x with about 12x fewer tasks for a window of 20.
Counting the values in each window no longer splits the chunks of the other
Expand Down
Loading
Loading