🍾 Xarray is now 10 years old! 🎉

xarray.core.accessor_str.StringAccessor

xarray.core.accessor_str.StringAccessor#

class xarray.core.accessor_str.StringAccessor(obj)[source]#

Vectorized string functions for string-like arrays.

Similar to pandas, fields can be accessed through the .str attribute for applicable DataArrays.

>>> da = xr.DataArray(["some", "text", "in", "an", "array"])
>>> da.str.len()
<xarray.DataArray (dim_0: 5)> Size: 40B
array([4, 4, 2, 2, 5])
Dimensions without coordinates: dim_0

It also implements +, *, and %, which operate as elementwise versions of the corresponding str methods. These will automatically broadcast for array-like inputs.

>>> da1 = xr.DataArray(["first", "second", "third"], dims=["X"])
>>> da2 = xr.DataArray([1, 2, 3], dims=["Y"])
>>> da1.str + da2
<xarray.DataArray (X: 3, Y: 3)> Size: 252B
array([['first1', 'first2', 'first3'],
       ['second1', 'second2', 'second3'],
       ['third1', 'third2', 'third3']], dtype='<U7')
Dimensions without coordinates: X, Y
>>> da1 = xr.DataArray(["a", "b", "c", "d"], dims=["X"])
>>> reps = xr.DataArray([3, 4], dims=["Y"])
>>> da1.str * reps
<xarray.DataArray (X: 4, Y: 2)> Size: 128B
array([['aaa', 'aaaa'],
       ['bbb', 'bbbb'],
       ['ccc', 'cccc'],
       ['ddd', 'dddd']], dtype='<U4')
Dimensions without coordinates: X, Y
>>> da1 = xr.DataArray(["%s_%s", "%s-%s", "%s|%s"], dims=["X"])
>>> da2 = xr.DataArray([1, 2], dims=["Y"])
>>> da3 = xr.DataArray([0.1, 0.2], dims=["Z"])
>>> da1.str % (da2, da3)
<xarray.DataArray (X: 3, Y: 2, Z: 2)> Size: 240B
array([[['1_0.1', '1_0.2'],
        ['2_0.1', '2_0.2']],

       [['1-0.1', '1-0.2'],
        ['2-0.1', '2-0.2']],

       [['1|0.1', '1|0.2'],
        ['2|0.1', '2|0.2']]], dtype='<U5')
Dimensions without coordinates: X, Y, Z

Note

When using % formatting with a dict, the values are always used as a single value, they are not applied elementwise.

>>> da1 = xr.DataArray(["%(a)s"], dims=["X"])
>>> da2 = xr.DataArray([1, 2, 3], dims=["Y"])
>>> da1 % {"a": da2}
<xarray.DataArray (X: 1)> Size: 8B
array(['<xarray.DataArray (Y: 3)> Size: 24B\narray([1, 2, 3])\nDimensions without coordinates: Y'],
      dtype=object)
Dimensions without coordinates: X
__init__(obj)[source]#

Methods

__init__(obj)

capitalize()

Convert strings in the array to be capitalized.

casefold()

Convert strings in the array to be casefolded.

cat(*others[, sep])

Concatenate strings elementwise in the DataArray with other strings.

center(width[, fillchar])

Pad left and right side of each string in the array.

contains(pat[, case, flags, regex])

Test if pattern or regex is contained within each string of the array.

count(pat[, flags, case])

Count occurrences of pattern in each string of the array.

decode(encoding[, errors])

Decode character string in the array using indicated encoding.

encode(encoding[, errors])

Encode character string in the array using indicated encoding.

endswith(pat)

Test if the end of each string in the array matches a pattern.

extract(pat, dim[, case, flags])

Extract the first match of capture groups in the regex pat as a new dimension in a DataArray.

extractall(pat, group_dim, match_dim[, ...])

Extract all matches of capture groups in the regex pat as new dimensions in a DataArray.

find(sub[, start, end, side])

Return lowest or highest indexes in each strings in the array where the substring is fully contained between [start:end].

findall(pat[, case, flags])

Find all occurrences of pattern or regular expression in the DataArray.

format(*args, **kwargs)

Perform python string formatting on each element of the DataArray.

get(i[, default])

Extract character number i from each string in the array.

get_dummies(dim[, sep])

Return DataArray of dummy/indicator variables.

index(sub[, start, end, side])

Return lowest or highest indexes in each strings where the substring is fully contained between [start:end].

isalnum()

Check whether all characters in each string are alphanumeric.

isalpha()

Check whether all characters in each string are alphabetic.

isdecimal()

Check whether all characters in each string are decimal.

isdigit()

Check whether all characters in each string are digits.

islower()

Check whether all characters in each string are lowercase.

isnumeric()

Check whether all characters in each string are numeric.

isspace()

Check whether all characters in each string are spaces.

istitle()

Check whether all characters in each string are titlecase.

isupper()

Check whether all characters in each string are uppercase.

join([dim, sep])

Concatenate strings in a DataArray along a particular dimension.

len()

Compute the length of each string in the array.

ljust(width[, fillchar])

Pad right side of each string in the array.

lower()

Convert strings in the array to lowercase.

lstrip([to_strip])

Remove leading characters.

match(pat[, case, flags])

Determine if each string in the array matches a regular expression.

normalize(form)

Return the Unicode normal form for the strings in the datarray.

pad(width[, side, fillchar])

Pad strings in the array up to width.

partition(dim[, sep])

Split the strings in the DataArray at the first occurrence of separator sep.

repeat(repeats)

Repeat each string in the array.

replace(pat, repl[, n, case, flags, regex])

Replace occurrences of pattern/regex in the array with some string.

rfind(sub[, start, end])

Return highest indexes in each strings in the array where the substring is fully contained between [start:end].

rindex(sub[, start, end])

Return highest indexes in each strings where the substring is fully contained between [start:end].

rjust(width[, fillchar])

Pad left side of each string in the array.

rpartition(dim[, sep])

Split the strings in the DataArray at the last occurrence of separator sep.

rsplit(dim[, sep, maxsplit])

Split strings in a DataArray around the given separator/delimiter sep.

rstrip([to_strip])

Remove trailing characters.

slice([start, stop, step])

Slice substrings from each string in the array.

slice_replace([start, stop, repl])

Replace a positional slice of a string with another value.

split(dim[, sep, maxsplit])

Split strings in a DataArray around the given separator/delimiter sep.

startswith(pat)

Test if the start of each string in the array matches a pattern.

strip([to_strip, side])

Remove leading and trailing characters.

swapcase()

Convert strings in the array to be swapcased.

title()

Convert strings in the array to titlecase.

translate(table)

Map characters of each string through the given mapping table.

upper()

Convert strings in the array to uppercase.

wrap(width, **kwargs)

Wrap long strings in the array in paragraphs with length less than width.

zfill(width)

Pad each string in the array by prepending '0' characters.