1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899
|
# The StringAccessor class defined below is an adaptation of the
# pandas string methods source code (see pd.core.strings)
# For reference, here is a copy of the pandas copyright notice:
# (c) 2011-2012, Lambda Foundry, Inc. and PyData Development Team
# All rights reserved.
# Copyright (c) 2008-2011 AQR Capital Management, LLC
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of the copyright holder nor the names of any
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from __future__ import annotations
import codecs
import re
import textwrap
from collections.abc import Callable, Hashable, Mapping
from functools import reduce
from operator import or_ as set_union
from re import Pattern
from typing import TYPE_CHECKING, Any, Generic
from unicodedata import normalize
import numpy as np
from xarray.core import duck_array_ops
from xarray.core.types import T_DataArray
if TYPE_CHECKING:
from numpy.typing import DTypeLike
from xarray.core.dataarray import DataArray
_cpython_optimized_encoders = (
"utf-8",
"utf8",
"latin-1",
"latin1",
"iso-8859-1",
"mbcs",
"ascii",
)
_cpython_optimized_decoders = _cpython_optimized_encoders + ("utf-16", "utf-32")
def _contains_obj_type(*, pat: Any, checker: Any) -> bool:
"""Determine if the object fits some rule or is array of objects that do so."""
if isinstance(checker, type):
targtype = checker
checker = lambda x: isinstance(x, targtype)
if checker(pat):
return True
# If it is not an object array it can't contain compiled re
if getattr(pat, "dtype", "no") != np.object_:
return False
return _apply_str_ufunc(func=checker, obj=pat).all()
def _contains_str_like(pat: Any) -> bool:
"""Determine if the object is a str-like or array of str-like."""
if isinstance(pat, str | bytes):
return True
if not hasattr(pat, "dtype"):
return False
return pat.dtype.kind in ["U", "S"]
def _contains_compiled_re(pat: Any) -> bool:
"""Determine if the object is a compiled re or array of compiled re."""
return _contains_obj_type(pat=pat, checker=re.Pattern)
def _contains_callable(pat: Any) -> bool:
"""Determine if the object is a callable or array of callables."""
return _contains_obj_type(pat=pat, checker=callable)
def _apply_str_ufunc(
*,
func: Callable,
obj: Any,
dtype: DTypeLike = None,
output_core_dims: list | tuple = ((),),
output_sizes: Mapping[Any, int] | None = None,
func_args: tuple = (),
func_kwargs: Mapping = {},
) -> Any:
# TODO handling of na values ?
if dtype is None:
dtype = obj.dtype
dask_gufunc_kwargs = dict()
if output_sizes is not None:
dask_gufunc_kwargs["output_sizes"] = output_sizes
from xarray.computation.apply_ufunc import apply_ufunc
return apply_ufunc(
func,
obj,
*func_args,
vectorize=True,
dask="parallelized",
output_dtypes=[dtype],
output_core_dims=output_core_dims,
dask_gufunc_kwargs=dask_gufunc_kwargs,
**func_kwargs,
)
class StringAccessor(Generic[T_DataArray]):
r"""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']],
<BLANKLINE>
[['1-0.1', '1-0.2'],
['2-0.1', '2-0.2']],
<BLANKLINE>
[['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
"""
__slots__ = ("_obj",)
def __init__(self, obj: T_DataArray) -> None:
self._obj = obj
def _stringify(self, invar: Any) -> str | bytes | Any:
"""
Convert a string-like to the correct string/bytes type.
This is mostly here to tell mypy a pattern is a str/bytes not a re.Pattern.
"""
if hasattr(invar, "astype"):
return invar.astype(self._obj.dtype.kind)
else:
return self._obj.dtype.type(invar)
def _apply(
self,
*,
func: Callable,
dtype: DTypeLike = None,
output_core_dims: list | tuple = ((),),
output_sizes: Mapping[Any, int] | None = None,
func_args: tuple = (),
func_kwargs: Mapping = {},
) -> T_DataArray:
return _apply_str_ufunc(
obj=self._obj,
func=func,
dtype=dtype,
output_core_dims=output_core_dims,
output_sizes=output_sizes,
func_args=func_args,
func_kwargs=func_kwargs,
)
def _re_compile(
self,
*,
pat: str | bytes | Pattern | Any,
flags: int = 0,
case: bool | None = None,
) -> Pattern | Any:
is_compiled_re = isinstance(pat, re.Pattern)
if is_compiled_re and flags != 0:
raise ValueError("Flags cannot be set when pat is a compiled regex.")
if is_compiled_re and case is not None:
raise ValueError("Case cannot be set when pat is a compiled regex.")
if is_compiled_re:
# no-op, needed to tell mypy this isn't a string
return re.compile(pat)
if case is None:
case = True
# The case is handled by the re flags internally.
# Add it to the flags if necessary.
if not case:
flags |= re.IGNORECASE
if getattr(pat, "dtype", None) != np.object_:
pat = self._stringify(pat)
def func(x):
return re.compile(x, flags=flags)
if isinstance(pat, np.ndarray):
# apply_ufunc doesn't work for numpy arrays with output object dtypes
func_ = np.vectorize(func)
return func_(pat)
else:
return _apply_str_ufunc(func=func, obj=pat, dtype=np.object_)
def len(self) -> T_DataArray:
"""
Compute the length of each string in the array.
Returns
-------
lengths array : array of int
"""
return self._apply(func=len, dtype=int)
def __getitem__(
self,
key: int | slice,
) -> T_DataArray:
if isinstance(key, slice):
return self.slice(start=key.start, stop=key.stop, step=key.step)
else:
return self.get(key)
def __add__(self, other: Any) -> T_DataArray:
return self.cat(other, sep="")
def __mul__(
self,
num: int | Any,
) -> T_DataArray:
return self.repeat(num)
def __mod__(
self,
other: Any,
) -> T_DataArray:
if isinstance(other, dict):
other = {key: self._stringify(val) for key, val in other.items()}
return self._apply(func=lambda x: x % other)
elif isinstance(other, tuple):
other = tuple(self._stringify(x) for x in other)
return self._apply(func=lambda x, *y: x % y, func_args=other)
else:
return self._apply(func=lambda x, y: x % y, func_args=(other,))
def get(
self,
i: int | Any,
default: str | bytes = "",
) -> T_DataArray:
"""
Extract character number `i` from each string in the array.
If `i` is array-like, they are broadcast against the array and
applied elementwise.
Parameters
----------
i : int or array-like of int
Position of element to extract.
If array-like, it is broadcast.
default : str or bytes, default: ""
Value for out-of-range index.
Returns
-------
items : array of object
"""
def f(x, iind):
islice = slice(-1, None) if iind == -1 else slice(iind, iind + 1)
item = x[islice]
return item or default
return self._apply(func=f, func_args=(i,))
def slice(
self,
start: int | Any | None = None,
stop: int | Any | None = None,
step: int | Any | None = None,
) -> T_DataArray:
"""
Slice substrings from each string in the array.
If `start`, `stop`, or 'step` is array-like, they are broadcast
against the array and applied elementwise.
Parameters
----------
start : int or array-like of int, optional
Start position for slice operation.
If array-like, it is broadcast.
stop : int or array-like of int, optional
Stop position for slice operation.
If array-like, it is broadcast.
step : int or array-like of int, optional
Step size for slice operation.
If array-like, it is broadcast.
Returns
-------
sliced strings : same type as values
"""
f = lambda x, istart, istop, istep: x[slice(istart, istop, istep)]
return self._apply(func=f, func_args=(start, stop, step))
def slice_replace(
self,
start: int | Any | None = None,
stop: int | Any | None = None,
repl: str | bytes | Any = "",
) -> T_DataArray:
"""
Replace a positional slice of a string with another value.
If `start`, `stop`, or 'repl` is array-like, they are broadcast
against the array and applied elementwise.
Parameters
----------
start : int or array-like of int, optional
Left index position to use for the slice. If not specified (None),
the slice is unbounded on the left, i.e. slice from the start
of the string. If array-like, it is broadcast.
stop : int or array-like of int, optional
Right index position to use for the slice. If not specified (None),
the slice is unbounded on the right, i.e. slice until the
end of the string. If array-like, it is broadcast.
repl : str or array-like of str, default: ""
String for replacement. If not specified, the sliced region
is replaced with an empty string. If array-like, it is broadcast.
Returns
-------
replaced : same type as values
"""
repl = self._stringify(repl)
def func(x, istart, istop, irepl):
if len(x[istart:istop]) == 0:
local_stop = istart
else:
local_stop = istop
y = self._stringify("")
if istart is not None:
y += x[:istart]
y += irepl
if istop is not None:
y += x[local_stop:]
return y
return self._apply(func=func, func_args=(start, stop, repl))
def cat(self, *others, sep: str | bytes | Any = "") -> T_DataArray:
"""
Concatenate strings elementwise in the DataArray with other strings.
The other strings can either be string scalars or other array-like.
Dimensions are automatically broadcast together.
An optional separator `sep` can also be specified. If `sep` is
array-like, it is broadcast against the array and applied elementwise.
Parameters
----------
*others : str or array-like of str
Strings or array-like of strings to concatenate elementwise with
the current DataArray.
sep : str or array-like of str, default: "".
Separator to use between strings.
It is broadcast in the same way as the other input strings.
If array-like, its dimensions will be placed at the end of the output array dimensions.
Returns
-------
concatenated : same type as values
Examples
--------
Create a string array
>>> myarray = xr.DataArray(
... ["11111", "4"],
... dims=["X"],
... )
Create some arrays to concatenate with it
>>> values_1 = xr.DataArray(
... ["a", "bb", "cccc"],
... dims=["Y"],
... )
>>> values_2 = np.array(3.4)
>>> values_3 = ""
>>> values_4 = np.array("test", dtype=np.str_)
Determine the separator to use
>>> seps = xr.DataArray(
... [" ", ", "],
... dims=["ZZ"],
... )
Concatenate the arrays using the separator
>>> myarray.str.cat(values_1, values_2, values_3, values_4, sep=seps)
<xarray.DataArray (X: 2, Y: 3, ZZ: 2)> Size: 1kB
array([[['11111 a 3.4 test', '11111, a, 3.4, , test'],
['11111 bb 3.4 test', '11111, bb, 3.4, , test'],
['11111 cccc 3.4 test', '11111, cccc, 3.4, , test']],
<BLANKLINE>
[['4 a 3.4 test', '4, a, 3.4, , test'],
['4 bb 3.4 test', '4, bb, 3.4, , test'],
['4 cccc 3.4 test', '4, cccc, 3.4, , test']]], dtype='<U24')
Dimensions without coordinates: X, Y, ZZ
See Also
--------
pandas.Series.str.cat
str.join
"""
sep = self._stringify(sep)
others = tuple(self._stringify(x) for x in others)
others = others + (sep,)
# sep will go at the end of the input arguments.
func = lambda *x: x[-1].join(x[:-1])
return self._apply(
func=func,
func_args=others,
dtype=self._obj.dtype.kind,
)
def join(
self,
dim: Hashable = None,
sep: str | bytes | Any = "",
) -> T_DataArray:
"""
Concatenate strings in a DataArray along a particular dimension.
An optional separator `sep` can also be specified. If `sep` is
array-like, it is broadcast against the array and applied elementwise.
Parameters
----------
dim : hashable, optional
Dimension along which the strings should be concatenated.
Only one dimension is allowed at a time.
Optional for 0D or 1D DataArrays, required for multidimensional DataArrays.
sep : str or array-like, default: "".
Separator to use between strings.
It is broadcast in the same way as the other input strings.
If array-like, its dimensions will be placed at the end of the output array dimensions.
Returns
-------
joined : same type as values
Examples
--------
Create an array
>>> values = xr.DataArray(
... [["a", "bab", "abc"], ["abcd", "", "abcdef"]],
... dims=["X", "Y"],
... )
Determine the separator
>>> seps = xr.DataArray(
... ["-", "_"],
... dims=["ZZ"],
... )
Join the strings along a given dimension
>>> values.str.join(dim="Y", sep=seps)
<xarray.DataArray (X: 2, ZZ: 2)> Size: 192B
array([['a-bab-abc', 'a_bab_abc'],
['abcd--abcdef', 'abcd__abcdef']], dtype='<U12')
Dimensions without coordinates: X, ZZ
See Also
--------
pandas.Series.str.join
str.join
"""
if self._obj.ndim > 1 and dim is None:
raise ValueError("Dimension must be specified for multidimensional arrays.")
if self._obj.ndim > 1:
# Move the target dimension to the start and split along it
dimshifted = list(self._obj.transpose(dim, ...))
elif self._obj.ndim == 1:
dimshifted = list(self._obj)
else:
dimshifted = [self._obj]
start, *others = dimshifted
# concatenate the resulting arrays
return start.str.cat(*others, sep=sep)
def format(
self,
*args: Any,
**kwargs: Any,
) -> T_DataArray:
"""
Perform python string formatting on each element of the DataArray.
This is equivalent to calling `str.format` on every element of the
DataArray. The replacement values can either be a string-like
scalar or array-like of string-like values. If array-like,
the values will be broadcast and applied elementwiseto the input
DataArray.
.. note::
Array-like values provided as `*args` will have their
dimensions added even if those arguments are not used in any
string formatting.
.. warning::
Array-like arguments are only applied elementwise for `*args`.
For `**kwargs`, values are used as-is.
Parameters
----------
*args : str or bytes or array-like of str or bytes
Values for positional formatting.
If array-like, the values are broadcast and applied elementwise.
The dimensions will be placed at the end of the output array dimensions
in the order they are provided.
**kwargs : str or bytes or array-like of str or bytes
Values for keyword-based formatting.
These are **not** broadcast or applied elementwise.
Returns
-------
formatted : same type as values
Examples
--------
Create an array to format.
>>> values = xr.DataArray(
... ["{} is {adj0}", "{} and {} are {adj1}"],
... dims=["X"],
... )
Set the values to fill.
>>> noun0 = xr.DataArray(
... ["spam", "egg"],
... dims=["Y"],
... )
>>> noun1 = xr.DataArray(
... ["lancelot", "arthur"],
... dims=["ZZ"],
... )
>>> adj0 = "unexpected"
>>> adj1 = "like a duck"
Insert the values into the array
>>> values.str.format(noun0, noun1, adj0=adj0, adj1=adj1)
<xarray.DataArray (X: 2, Y: 2, ZZ: 2)> Size: 1kB
array([[['spam is unexpected', 'spam is unexpected'],
['egg is unexpected', 'egg is unexpected']],
<BLANKLINE>
[['spam and lancelot are like a duck',
'spam and arthur are like a duck'],
['egg and lancelot are like a duck',
'egg and arthur are like a duck']]], dtype='<U33')
Dimensions without coordinates: X, Y, ZZ
See Also
--------
str.format
"""
args = tuple(self._stringify(x) for x in args)
kwargs = {key: self._stringify(val) for key, val in kwargs.items()}
return self._apply(
func=self._obj.dtype.type.format,
func_args=args,
func_kwargs={"kwargs": kwargs},
)
def capitalize(self) -> T_DataArray:
"""
Convert strings in the array to be capitalized.
Returns
-------
capitalized : same type as values
Examples
--------
>>> da = xr.DataArray(
... ["temperature", "PRESSURE", "PreCipiTation", "daily rainfall"], dims="x"
... )
>>> da
<xarray.DataArray (x: 4)> Size: 224B
array(['temperature', 'PRESSURE', 'PreCipiTation', 'daily rainfall'],
dtype='<U14')
Dimensions without coordinates: x
>>> capitalized = da.str.capitalize()
>>> capitalized
<xarray.DataArray (x: 4)> Size: 224B
array(['Temperature', 'Pressure', 'Precipitation', 'Daily rainfall'],
dtype='<U14')
Dimensions without coordinates: x
"""
return self._apply(func=lambda x: x.capitalize())
def lower(self) -> T_DataArray:
"""
Convert strings in the array to lowercase.
Returns
-------
lowered : same type as values
Examples
--------
>>> da = xr.DataArray(["Temperature", "PRESSURE"], dims="x")
>>> da
<xarray.DataArray (x: 2)> Size: 88B
array(['Temperature', 'PRESSURE'], dtype='<U11')
Dimensions without coordinates: x
>>> lowered = da.str.lower()
>>> lowered
<xarray.DataArray (x: 2)> Size: 88B
array(['temperature', 'pressure'], dtype='<U11')
Dimensions without coordinates: x
"""
return self._apply(func=lambda x: x.lower())
def swapcase(self) -> T_DataArray:
"""
Convert strings in the array to be swapcased.
Returns
-------
swapcased : same type as values
Examples
--------
>>> import xarray as xr
>>> da = xr.DataArray(["temperature", "PRESSURE", "HuMiDiTy"], dims="x")
>>> da
<xarray.DataArray (x: 3)> Size: 132B
array(['temperature', 'PRESSURE', 'HuMiDiTy'], dtype='<U11')
Dimensions without coordinates: x
>>> swapcased = da.str.swapcase()
>>> swapcased
<xarray.DataArray (x: 3)> Size: 132B
array(['TEMPERATURE', 'pressure', 'hUmIdItY'], dtype='<U11')
Dimensions without coordinates: x
"""
return self._apply(func=lambda x: x.swapcase())
def title(self) -> T_DataArray:
"""
Convert strings in the array to titlecase.
Returns
-------
titled : same type as values
Examples
--------
>>> da = xr.DataArray(["temperature", "PRESSURE", "HuMiDiTy"], dims="x")
>>> da
<xarray.DataArray (x: 3)> Size: 132B
array(['temperature', 'PRESSURE', 'HuMiDiTy'], dtype='<U11')
Dimensions without coordinates: x
>>> titled = da.str.title()
>>> titled
<xarray.DataArray (x: 3)> Size: 132B
array(['Temperature', 'Pressure', 'Humidity'], dtype='<U11')
Dimensions without coordinates: x
"""
return self._apply(func=lambda x: x.title())
def upper(self) -> T_DataArray:
"""
Convert strings in the array to uppercase.
Returns
-------
uppered : same type as values
Examples
--------
>>> da = xr.DataArray(["temperature", "HuMiDiTy"], dims="x")
>>> da
<xarray.DataArray (x: 2)> Size: 88B
array(['temperature', 'HuMiDiTy'], dtype='<U11')
Dimensions without coordinates: x
>>> uppered = da.str.upper()
>>> uppered
<xarray.DataArray (x: 2)> Size: 88B
array(['TEMPERATURE', 'HUMIDITY'], dtype='<U11')
Dimensions without coordinates: x
"""
return self._apply(func=lambda x: x.upper())
def casefold(self) -> T_DataArray:
"""
Convert strings in the array to be casefolded.
Casefolding is similar to converting to lowercase,
but removes all case distinctions.
This is important in some languages that have more complicated
cases and case conversions. For example,
the 'ß' character in German is case-folded to 'ss', whereas it is lowercased
to 'ß'.
Returns
-------
casefolded : same type as values
Examples
--------
>>> da = xr.DataArray(["TEMPERATURE", "HuMiDiTy"], dims="x")
>>> da
<xarray.DataArray (x: 2)> Size: 88B
array(['TEMPERATURE', 'HuMiDiTy'], dtype='<U11')
Dimensions without coordinates: x
>>> casefolded = da.str.casefold()
>>> casefolded
<xarray.DataArray (x: 2)> Size: 88B
array(['temperature', 'humidity'], dtype='<U11')
Dimensions without coordinates: x
>>> da = xr.DataArray(["ß", "İ"], dims="x")
>>> da
<xarray.DataArray (x: 2)> Size: 8B
array(['ß', 'İ'], dtype='<U1')
Dimensions without coordinates: x
>>> casefolded = da.str.casefold()
>>> casefolded
<xarray.DataArray (x: 2)> Size: 16B
array(['ss', 'i̇'], dtype='<U2')
Dimensions without coordinates: x
"""
return self._apply(func=lambda x: x.casefold())
def normalize(
self,
form: str,
) -> T_DataArray:
"""
Return the Unicode normal form for the strings in the datarray.
For more information on the forms, see the documentation for
:func:`unicodedata.normalize`.
Parameters
----------
form : {"NFC", "NFKC", "NFD", "NFKD"}
Unicode form.
Returns
-------
normalized : same type as values
"""
return self._apply(func=lambda x: normalize(form, x)) # type: ignore[arg-type]
def isalnum(self) -> T_DataArray:
"""
Check whether all characters in each string are alphanumeric.
Returns
-------
isalnum : array of bool
Array of boolean values with the same shape as the original array.
Examples
--------
>>> da = xr.DataArray(["H2O", "NaCl-"], dims="x")
>>> da
<xarray.DataArray (x: 2)> Size: 40B
array(['H2O', 'NaCl-'], dtype='<U5')
Dimensions without coordinates: x
>>> isalnum = da.str.isalnum()
>>> isalnum
<xarray.DataArray (x: 2)> Size: 2B
array([ True, False])
Dimensions without coordinates: x
"""
return self._apply(func=lambda x: x.isalnum(), dtype=bool)
def isalpha(self) -> T_DataArray:
"""
Check whether all characters in each string are alphabetic.
Returns
-------
isalpha : array of bool
Array of boolean values with the same shape as the original array.
Examples
--------
>>> da = xr.DataArray(["Mn", "H2O", "NaCl-"], dims="x")
>>> da
<xarray.DataArray (x: 3)> Size: 60B
array(['Mn', 'H2O', 'NaCl-'], dtype='<U5')
Dimensions without coordinates: x
>>> isalpha = da.str.isalpha()
>>> isalpha
<xarray.DataArray (x: 3)> Size: 3B
array([ True, False, False])
Dimensions without coordinates: x
"""
return self._apply(func=lambda x: x.isalpha(), dtype=bool)
def isdecimal(self) -> T_DataArray:
"""
Check whether all characters in each string are decimal.
Returns
-------
isdecimal : array of bool
Array of boolean values with the same shape as the original array.
Examples
--------
>>> da = xr.DataArray(["2.3", "123", "0"], dims="x")
>>> da
<xarray.DataArray (x: 3)> Size: 36B
array(['2.3', '123', '0'], dtype='<U3')
Dimensions without coordinates: x
>>> isdecimal = da.str.isdecimal()
>>> isdecimal
<xarray.DataArray (x: 3)> Size: 3B
array([False, True, True])
Dimensions without coordinates: x
"""
return self._apply(func=lambda x: x.isdecimal(), dtype=bool)
def isdigit(self) -> T_DataArray:
"""
Check whether all characters in each string are digits.
Returns
-------
isdigit : array of bool
Array of boolean values with the same shape as the original array.
Examples
--------
>>> da = xr.DataArray(["123", "1.2", "0", "CO2", "NaCl"], dims="x")
>>> da
<xarray.DataArray (x: 5)> Size: 80B
array(['123', '1.2', '0', 'CO2', 'NaCl'], dtype='<U4')
Dimensions without coordinates: x
>>> isdigit = da.str.isdigit()
>>> isdigit
<xarray.DataArray (x: 5)> Size: 5B
array([ True, False, True, False, False])
Dimensions without coordinates: x
"""
return self._apply(func=lambda x: x.isdigit(), dtype=bool)
def islower(self) -> T_DataArray:
"""
Check whether all characters in each string are lowercase.
Returns
-------
islower : array of bool
Array of boolean values with the same shape as the original array indicating whether all characters of each
element of the string array are lowercase (True) or not (False).
Examples
--------
>>> da = xr.DataArray(["temperature", "HUMIDITY", "pREciPiTaTioN"], dims="x")
>>> da
<xarray.DataArray (x: 3)> Size: 156B
array(['temperature', 'HUMIDITY', 'pREciPiTaTioN'], dtype='<U13')
Dimensions without coordinates: x
>>> islower = da.str.islower()
>>> islower
<xarray.DataArray (x: 3)> Size: 3B
array([ True, False, False])
Dimensions without coordinates: x
"""
return self._apply(func=lambda x: x.islower(), dtype=bool)
def isnumeric(self) -> T_DataArray:
"""
Check whether all characters in each string are numeric.
Returns
-------
isnumeric : array of bool
Array of boolean values with the same shape as the original array.
Examples
--------
>>> da = xr.DataArray(["123", "2.3", "H2O", "NaCl-", "Mn"], dims="x")
>>> da
<xarray.DataArray (x: 5)> Size: 100B
array(['123', '2.3', 'H2O', 'NaCl-', 'Mn'], dtype='<U5')
Dimensions without coordinates: x
>>> isnumeric = da.str.isnumeric()
>>> isnumeric
<xarray.DataArray (x: 5)> Size: 5B
array([ True, False, False, False, False])
Dimensions without coordinates: x
"""
return self._apply(func=lambda x: x.isnumeric(), dtype=bool)
def isspace(self) -> T_DataArray:
"""
Check whether all characters in each string are spaces.
Returns
-------
isspace : array of bool
Array of boolean values with the same shape as the original array.
Examples
--------
>>> da = xr.DataArray(["", " ", "\\t", "\\n"], dims="x")
>>> da
<xarray.DataArray (x: 4)> Size: 16B
array(['', ' ', '\\t', '\\n'], dtype='<U1')
Dimensions without coordinates: x
>>> isspace = da.str.isspace()
>>> isspace
<xarray.DataArray (x: 4)> Size: 4B
array([False, True, True, True])
Dimensions without coordinates: x
"""
return self._apply(func=lambda x: x.isspace(), dtype=bool)
def istitle(self) -> T_DataArray:
"""
Check whether all characters in each string are titlecase.
Returns
-------
istitle : array of bool
Array of boolean values with the same shape as the original array.
Examples
--------
>>> da = xr.DataArray(
... [
... "The Evolution Of Species",
... "The Theory of relativity",
... "the quantum mechanics of atoms",
... ],
... dims="title",
... )
>>> da
<xarray.DataArray (title: 3)> Size: 360B
array(['The Evolution Of Species', 'The Theory of relativity',
'the quantum mechanics of atoms'], dtype='<U30')
Dimensions without coordinates: title
>>> istitle = da.str.istitle()
>>> istitle
<xarray.DataArray (title: 3)> Size: 3B
array([ True, False, False])
Dimensions without coordinates: title
"""
return self._apply(func=lambda x: x.istitle(), dtype=bool)
def isupper(self) -> T_DataArray:
"""
Check whether all characters in each string are uppercase.
Returns
-------
isupper : array of bool
Array of boolean values with the same shape as the original array.
Examples
--------
>>> da = xr.DataArray(["TEMPERATURE", "humidity", "PreCIpiTAtioN"], dims="x")
>>> da
<xarray.DataArray (x: 3)> Size: 156B
array(['TEMPERATURE', 'humidity', 'PreCIpiTAtioN'], dtype='<U13')
Dimensions without coordinates: x
>>> isupper = da.str.isupper()
>>> isupper
<xarray.DataArray (x: 3)> Size: 3B
array([ True, False, False])
Dimensions without coordinates: x
"""
return self._apply(func=lambda x: x.isupper(), dtype=bool)
def count(
self, pat: str | bytes | Pattern | Any, flags: int = 0, case: bool | None = None
) -> T_DataArray:
"""
Count occurrences of pattern in each string of the array.
This function is used to count the number of times a particular regex
pattern is repeated in each of the string elements of the
:class:`~xarray.DataArray`.
The pattern `pat` can either be a single ``str`` or `re.Pattern` or
array-like of ``str`` or `re.Pattern`. If array-like, it is broadcast
against the array and applied elementwise.
Parameters
----------
pat : str or re.Pattern or array-like of str or re.Pattern
A string containing a regular expression or a compiled regular
expression object. If array-like, it is broadcast.
flags : int, default: 0
Flags to pass through to the re module, e.g. `re.IGNORECASE`.
see `compilation-flags <https://docs.python.org/3/howto/regex.html#compilation-flags>`_.
``0`` means no flags. Flags can be combined with the bitwise or operator ``|``.
Cannot be set if `pat` is a compiled regex.
case : bool, default: True
If True, case sensitive.
Cannot be set if `pat` is a compiled regex.
Equivalent to setting the `re.IGNORECASE` flag.
Returns
-------
counts : array of int
Examples
--------
>>> da = xr.DataArray(["jjklmn", "opjjqrs", "t-JJ99vwx"], dims="x")
>>> da
<xarray.DataArray (x: 3)> Size: 108B
array(['jjklmn', 'opjjqrs', 't-JJ99vwx'], dtype='<U9')
Dimensions without coordinates: x
Using a string:
>>> da.str.count("jj")
<xarray.DataArray (x: 3)> Size: 24B
array([1, 1, 0])
Dimensions without coordinates: x
Enable case-insensitive matching by setting case to false:
>>> counts = da.str.count("jj", case=False)
>>> counts
<xarray.DataArray (x: 3)> Size: 24B
array([1, 1, 1])
Dimensions without coordinates: x
Using regex:
>>> pat = "JJ[0-9]{2}[a-z]{3}"
>>> counts = da.str.count(pat)
>>> counts
<xarray.DataArray (x: 3)> Size: 24B
array([0, 0, 1])
Dimensions without coordinates: x
Using an array of strings (the pattern will be broadcast against the array):
>>> pat = xr.DataArray(["jj", "JJ"], dims="y")
>>> counts = da.str.count(pat)
>>> counts
<xarray.DataArray (x: 3, y: 2)> Size: 48B
array([[1, 0],
[1, 0],
[0, 1]])
Dimensions without coordinates: x, y
"""
pat = self._re_compile(pat=pat, flags=flags, case=case)
func = lambda x, ipat: len(ipat.findall(x))
return self._apply(func=func, func_args=(pat,), dtype=int)
def startswith(self, pat: str | bytes | Any) -> T_DataArray:
"""
Test if the start of each string in the array matches a pattern.
The pattern `pat` can either be a ``str`` or array-like of ``str``.
If array-like, it will be broadcast and applied elementwise.
Parameters
----------
pat : str
Character sequence. Regular expressions are not accepted.
If array-like, it is broadcast.
Returns
-------
startswith : array of bool
An array of booleans indicating whether the given pattern matches
the start of each string element.
Examples
--------
>>> da = xr.DataArray(["$100", "£23", "100"], dims="x")
>>> da
<xarray.DataArray (x: 3)> Size: 48B
array(['$100', '£23', '100'], dtype='<U4')
Dimensions without coordinates: x
>>> startswith = da.str.startswith("$")
>>> startswith
<xarray.DataArray (x: 3)> Size: 3B
array([ True, False, False])
Dimensions without coordinates: x
"""
pat = self._stringify(pat)
func = lambda x, y: x.startswith(y)
return self._apply(func=func, func_args=(pat,), dtype=bool)
def endswith(self, pat: str | bytes | Any) -> T_DataArray:
"""
Test if the end of each string in the array matches a pattern.
The pattern `pat` can either be a ``str`` or array-like of ``str``.
If array-like, it will be broadcast and applied elementwise.
Parameters
----------
pat : str
Character sequence. Regular expressions are not accepted.
If array-like, it is broadcast.
Returns
-------
endswith : array of bool
A Series of booleans indicating whether the given pattern matches
the end of each string element.
Examples
--------
>>> da = xr.DataArray(["10C", "10c", "100F"], dims="x")
>>> da
<xarray.DataArray (x: 3)> Size: 48B
array(['10C', '10c', '100F'], dtype='<U4')
Dimensions without coordinates: x
>>> endswith = da.str.endswith("C")
>>> endswith
<xarray.DataArray (x: 3)> Size: 3B
array([ True, False, False])
Dimensions without coordinates: x
"""
pat = self._stringify(pat)
func = lambda x, y: x.endswith(y)
return self._apply(func=func, func_args=(pat,), dtype=bool)
def pad(
self,
width: int | Any,
side: str = "left",
fillchar: str | bytes | Any = " ",
) -> T_DataArray:
"""
Pad strings in the array up to width.
If `width` or 'fillchar` is array-like, they are broadcast
against the array and applied elementwise.
Parameters
----------
width : int or array-like of int
Minimum width of resulting string; additional characters will be
filled with character defined in ``fillchar``.
If array-like, it is broadcast.
side : {"left", "right", "both"}, default: "left"
Side from which to fill resulting string.
fillchar : str or array-like of str, default: " "
Additional character for filling, default is a space.
If array-like, it is broadcast.
Returns
-------
filled : same type as values
Array with a minimum number of char in each element.
Examples
--------
Pad strings in the array with a single string on the left side.
Define the string in the array.
>>> da = xr.DataArray(["PAR184", "TKO65", "NBO9139", "NZ39"], dims="x")
>>> da
<xarray.DataArray (x: 4)> Size: 112B
array(['PAR184', 'TKO65', 'NBO9139', 'NZ39'], dtype='<U7')
Dimensions without coordinates: x
Pad the strings
>>> filled = da.str.pad(8, side="left", fillchar="0")
>>> filled
<xarray.DataArray (x: 4)> Size: 128B
array(['00PAR184', '000TKO65', '0NBO9139', '0000NZ39'], dtype='<U8')
Dimensions without coordinates: x
Pad strings on the right side
>>> filled = da.str.pad(8, side="right", fillchar="0")
>>> filled
<xarray.DataArray (x: 4)> Size: 128B
array(['PAR18400', 'TKO65000', 'NBO91390', 'NZ390000'], dtype='<U8')
Dimensions without coordinates: x
Pad strings on both sides
>>> filled = da.str.pad(8, side="both", fillchar="0")
>>> filled
<xarray.DataArray (x: 4)> Size: 128B
array(['0PAR1840', '0TKO6500', 'NBO91390', '00NZ3900'], dtype='<U8')
Dimensions without coordinates: x
Using an array-like width
>>> width = xr.DataArray([8, 10], dims="y")
>>> filled = da.str.pad(width, side="left", fillchar="0")
>>> filled
<xarray.DataArray (x: 4, y: 2)> Size: 320B
array([['00PAR184', '0000PAR184'],
['000TKO65', '00000TKO65'],
['0NBO9139', '000NBO9139'],
['0000NZ39', '000000NZ39']], dtype='<U10')
Dimensions without coordinates: x, y
Using an array-like value for fillchar
>>> fillchar = xr.DataArray(["0", "-"], dims="y")
>>> filled = da.str.pad(8, side="left", fillchar=fillchar)
>>> filled
<xarray.DataArray (x: 4, y: 2)> Size: 256B
array([['00PAR184', '--PAR184'],
['000TKO65', '---TKO65'],
['0NBO9139', '-NBO9139'],
['0000NZ39', '----NZ39']], dtype='<U8')
Dimensions without coordinates: x, y
"""
if side == "left":
func = self.rjust
elif side == "right":
func = self.ljust
elif side == "both":
func = self.center
else: # pragma: no cover
raise ValueError("Invalid side")
return func(width=width, fillchar=fillchar)
def _padder(
self,
*,
func: Callable,
width: int | Any,
fillchar: str | bytes | Any = " ",
) -> T_DataArray:
"""
Wrapper function to handle padding operations
"""
fillchar = self._stringify(fillchar)
def overfunc(x, iwidth, ifillchar):
if len(ifillchar) != 1:
raise TypeError("fillchar must be a character, not str")
return func(x, int(iwidth), ifillchar)
return self._apply(func=overfunc, func_args=(width, fillchar))
def center(
self, width: int | Any, fillchar: str | bytes | Any = " "
) -> T_DataArray:
"""
Pad left and right side of each string in the array.
If `width` or 'fillchar` is array-like, they are broadcast
against the array and applied elementwise.
Parameters
----------
width : int or array-like of int
Minimum width of resulting string; additional characters will be
filled with ``fillchar``. If array-like, it is broadcast.
fillchar : str or array-like of str, default: " "
Additional character for filling, default is a space.
If array-like, it is broadcast.
Returns
-------
filled : same type as values
"""
func = self._obj.dtype.type.center
return self._padder(func=func, width=width, fillchar=fillchar)
def ljust(
self,
width: int | Any,
fillchar: str | bytes | Any = " ",
) -> T_DataArray:
"""
Pad right side of each string in the array.
If `width` or 'fillchar` is array-like, they are broadcast
against the array and applied elementwise.
Parameters
----------
width : int or array-like of int
Minimum width of resulting string; additional characters will be
filled with ``fillchar``. If array-like, it is broadcast.
fillchar : str or array-like of str, default: " "
Additional character for filling, default is a space.
If array-like, it is broadcast.
Returns
-------
filled : same type as values
"""
func = self._obj.dtype.type.ljust
return self._padder(func=func, width=width, fillchar=fillchar)
def rjust(
self,
width: int | Any,
fillchar: str | bytes | Any = " ",
) -> T_DataArray:
"""
Pad left side of each string in the array.
If `width` or 'fillchar` is array-like, they are broadcast
against the array and applied elementwise.
Parameters
----------
width : int or array-like of int
Minimum width of resulting string; additional characters will be
filled with ``fillchar``. If array-like, it is broadcast.
fillchar : str or array-like of str, default: " "
Additional character for filling, default is a space.
If array-like, it is broadcast.
Returns
-------
filled : same type as values
"""
func = self._obj.dtype.type.rjust
return self._padder(func=func, width=width, fillchar=fillchar)
def zfill(self, width: int | Any) -> T_DataArray:
"""
Pad each string in the array by prepending '0' characters.
Strings in the array are padded with '0' characters on the
left of the string to reach a total string length `width`. Strings
in the array with length greater or equal to `width` are unchanged.
If `width` is array-like, it is broadcast against the array and applied
elementwise.
Parameters
----------
width : int or array-like of int
Minimum length of resulting string; strings with length less
than `width` be prepended with '0' characters. If array-like, it is broadcast.
Returns
-------
filled : same type as values
"""
return self.rjust(width, fillchar="0")
def contains(
self,
pat: str | bytes | Pattern | Any,
case: bool | None = None,
flags: int = 0,
regex: bool = True,
) -> T_DataArray:
"""
Test if pattern or regex is contained within each string of the array.
Return boolean array based on whether a given pattern or regex is
contained within a string of the array.
The pattern `pat` can either be a single ``str`` or `re.Pattern` or
array-like of ``str`` or `re.Pattern`. If array-like, it is broadcast
against the array and applied elementwise.
Parameters
----------
pat : str or re.Pattern or array-like of str or re.Pattern
Character sequence, a string containing a regular expression,
or a compiled regular expression object. If array-like, it is broadcast.
case : bool, default: True
If True, case sensitive.
Cannot be set if `pat` is a compiled regex.
Equivalent to setting the `re.IGNORECASE` flag.
flags : int, default: 0
Flags to pass through to the re module, e.g. `re.IGNORECASE`.
see `compilation-flags <https://docs.python.org/3/howto/regex.html#compilation-flags>`_.
``0`` means no flags. Flags can be combined with the bitwise or operator ``|``.
Cannot be set if `pat` is a compiled regex.
regex : bool, default: True
If True, assumes the pat is a regular expression.
If False, treats the pat as a literal string.
Cannot be set to `False` if `pat` is a compiled regex.
Returns
-------
contains : array of bool
An array of boolean values indicating whether the
given pattern is contained within the string of each element
of the array.
"""
is_compiled_re = _contains_compiled_re(pat)
if is_compiled_re and not regex:
raise ValueError(
"Must use regular expression matching for regular expression object."
)
if regex:
if not is_compiled_re:
pat = self._re_compile(pat=pat, flags=flags, case=case)
def func(x, ipat):
if ipat.groups > 0: # pragma: no cover
raise ValueError("This pattern has match groups.")
return bool(ipat.search(x))
else:
pat = self._stringify(pat)
if case or case is None:
func = lambda x, ipat: ipat in x
elif self._obj.dtype.char == "U":
uppered = self.casefold()
uppat = StringAccessor(pat).casefold() # type: ignore[type-var] # hack?
return uppered.str.contains(uppat, regex=False) # type: ignore[return-value]
else:
uppered = self.upper()
uppat = StringAccessor(pat).upper() # type: ignore[type-var] # hack?
return uppered.str.contains(uppat, regex=False) # type: ignore[return-value]
return self._apply(func=func, func_args=(pat,), dtype=bool)
def match(
self,
pat: str | bytes | Pattern | Any,
case: bool | None = None,
flags: int = 0,
) -> T_DataArray:
"""
Determine if each string in the array matches a regular expression.
The pattern `pat` can either be a single ``str`` or `re.Pattern` or
array-like of ``str`` or `re.Pattern`. If array-like, it is broadcast
against the array and applied elementwise.
Parameters
----------
pat : str or re.Pattern or array-like of str or re.Pattern
A string containing a regular expression or
a compiled regular expression object. If array-like, it is broadcast.
case : bool, default: True
If True, case sensitive.
Cannot be set if `pat` is a compiled regex.
Equivalent to setting the `re.IGNORECASE` flag.
flags : int, default: 0
Flags to pass through to the re module, e.g. `re.IGNORECASE`.
see `compilation-flags <https://docs.python.org/3/howto/regex.html#compilation-flags>`_.
``0`` means no flags. Flags can be combined with the bitwise or operator ``|``.
Cannot be set if `pat` is a compiled regex.
Returns
-------
matched : array of bool
"""
pat = self._re_compile(pat=pat, flags=flags, case=case)
func = lambda x, ipat: bool(ipat.match(x))
return self._apply(func=func, func_args=(pat,), dtype=bool)
def strip(
self, to_strip: str | bytes | Any = None, side: str = "both"
) -> T_DataArray:
"""
Remove leading and trailing characters.
Strip whitespaces (including newlines) or a set of specified characters
from each string in the array from left and/or right sides.
`to_strip` can either be a ``str`` or array-like of ``str``.
If array-like, it will be broadcast and applied elementwise.
Parameters
----------
to_strip : str or array-like of str or None, default: None
Specifying the set of characters to be removed.
All combinations of this set of characters will be stripped.
If None then whitespaces are removed. If array-like, it is broadcast.
side : {"left", "right", "both"}, default: "both"
Side from which to strip.
Returns
-------
stripped : same type as values
"""
if to_strip is not None:
to_strip = self._stringify(to_strip)
if side == "both":
func = lambda x, y: x.strip(y)
elif side == "left":
func = lambda x, y: x.lstrip(y)
elif side == "right":
func = lambda x, y: x.rstrip(y)
else: # pragma: no cover
raise ValueError("Invalid side")
return self._apply(func=func, func_args=(to_strip,))
def lstrip(self, to_strip: str | bytes | Any = None) -> T_DataArray:
"""
Remove leading characters.
Strip whitespaces (including newlines) or a set of specified characters
from each string in the array from the left side.
`to_strip` can either be a ``str`` or array-like of ``str``.
If array-like, it will be broadcast and applied elementwise.
Parameters
----------
to_strip : str or array-like of str or None, default: None
Specifying the set of characters to be removed.
All combinations of this set of characters will be stripped.
If None then whitespaces are removed. If array-like, it is broadcast.
Returns
-------
stripped : same type as values
"""
return self.strip(to_strip, side="left")
def rstrip(self, to_strip: str | bytes | Any = None) -> T_DataArray:
"""
Remove trailing characters.
Strip whitespaces (including newlines) or a set of specified characters
from each string in the array from the right side.
`to_strip` can either be a ``str`` or array-like of ``str``.
If array-like, it will be broadcast and applied elementwise.
Parameters
----------
to_strip : str or array-like of str or None, default: None
Specifying the set of characters to be removed.
All combinations of this set of characters will be stripped.
If None then whitespaces are removed. If array-like, it is broadcast.
Returns
-------
stripped : same type as values
"""
return self.strip(to_strip, side="right")
def wrap(self, width: int | Any, **kwargs) -> T_DataArray:
"""
Wrap long strings in the array in paragraphs with length less than `width`.
This method has the same keyword parameters and defaults as
:class:`textwrap.TextWrapper`.
If `width` is array-like, it is broadcast against the array and applied
elementwise.
Parameters
----------
width : int or array-like of int
Maximum line-width.
If array-like, it is broadcast.
**kwargs
keyword arguments passed into :class:`textwrap.TextWrapper`.
Returns
-------
wrapped : same type as values
"""
ifunc = lambda x: textwrap.TextWrapper(width=x, **kwargs)
tw = StringAccessor(width)._apply(func=ifunc, dtype=np.object_) # type: ignore[type-var] # hack?
func = lambda x, itw: "\n".join(itw.wrap(x))
return self._apply(func=func, func_args=(tw,))
# Mapping is only covariant in its values, maybe use a custom CovariantMapping?
def translate(self, table: Mapping[Any, str | bytes | int | None]) -> T_DataArray:
"""
Map characters of each string through the given mapping table.
Parameters
----------
table : dict-like from and to str or bytes or int
A a mapping of Unicode ordinals to Unicode ordinals, strings, int
or None. Unmapped characters are left untouched. Characters mapped
to None are deleted. :meth:`str.maketrans` is a helper function for
making translation tables.
Returns
-------
translated : same type as values
"""
func = lambda x: x.translate(table)
return self._apply(func=func)
def repeat(
self,
repeats: int | Any,
) -> T_DataArray:
"""
Repeat each string in the array.
If `repeats` is array-like, it is broadcast against the array and applied
elementwise.
Parameters
----------
repeats : int or array-like of int
Number of repetitions.
If array-like, it is broadcast.
Returns
-------
repeated : same type as values
Array of repeated string objects.
"""
func = lambda x, y: x * y
return self._apply(func=func, func_args=(repeats,))
def find(
self,
sub: str | bytes | Any,
start: int | Any = 0,
end: int | Any = None,
side: str = "left",
) -> T_DataArray:
"""
Return lowest or highest indexes in each strings in the array
where the substring is fully contained between [start:end].
Return -1 on failure.
If `start`, `end`, or 'sub` is array-like, they are broadcast
against the array and applied elementwise.
Parameters
----------
sub : str or array-like of str
Substring being searched.
If array-like, it is broadcast.
start : int or array-like of int
Left edge index.
If array-like, it is broadcast.
end : int or array-like of int
Right edge index.
If array-like, it is broadcast.
side : {"left", "right"}, default: "left"
Starting side for search.
Returns
-------
found : array of int
"""
sub = self._stringify(sub)
if side == "left":
method = "find"
elif side == "right":
method = "rfind"
else: # pragma: no cover
raise ValueError("Invalid side")
func = lambda x, isub, istart, iend: getattr(x, method)(isub, istart, iend)
return self._apply(func=func, func_args=(sub, start, end), dtype=int)
def rfind(
self,
sub: str | bytes | Any,
start: int | Any = 0,
end: int | Any = None,
) -> T_DataArray:
"""
Return highest indexes in each strings in the array
where the substring is fully contained between [start:end].
Return -1 on failure.
If `start`, `end`, or 'sub` is array-like, they are broadcast
against the array and applied elementwise.
Parameters
----------
sub : str or array-like of str
Substring being searched.
If array-like, it is broadcast.
start : int or array-like of int
Left edge index.
If array-like, it is broadcast.
end : int or array-like of int
Right edge index.
If array-like, it is broadcast.
Returns
-------
found : array of int
"""
return self.find(sub, start=start, end=end, side="right")
def index(
self,
sub: str | bytes | Any,
start: int | Any = 0,
end: int | Any = None,
side: str = "left",
) -> T_DataArray:
"""
Return lowest or highest indexes in each strings where the substring is
fully contained between [start:end]. This is the same as
``str.find`` except instead of returning -1, it raises a ValueError
when the substring is not found.
If `start`, `end`, or 'sub` is array-like, they are broadcast
against the array and applied elementwise.
Parameters
----------
sub : str or array-like of str
Substring being searched.
If array-like, it is broadcast.
start : int or array-like of int
Left edge index.
If array-like, it is broadcast.
end : int or array-like of int
Right edge index.
If array-like, it is broadcast.
side : {"left", "right"}, default: "left"
Starting side for search.
Returns
-------
found : array of int
Raises
------
ValueError
substring is not found
"""
sub = self._stringify(sub)
if side == "left":
method = "index"
elif side == "right":
method = "rindex"
else: # pragma: no cover
raise ValueError("Invalid side")
func = lambda x, isub, istart, iend: getattr(x, method)(isub, istart, iend)
return self._apply(func=func, func_args=(sub, start, end), dtype=int)
def rindex(
self,
sub: str | bytes | Any,
start: int | Any = 0,
end: int | Any = None,
) -> T_DataArray:
"""
Return highest indexes in each strings where the substring is
fully contained between [start:end]. This is the same as
``str.rfind`` except instead of returning -1, it raises a ValueError
when the substring is not found.
If `start`, `end`, or 'sub` is array-like, they are broadcast
against the array and applied elementwise.
Parameters
----------
sub : str or array-like of str
Substring being searched.
If array-like, it is broadcast.
start : int or array-like of int
Left edge index.
If array-like, it is broadcast.
end : int or array-like of int
Right edge index.
If array-like, it is broadcast.
Returns
-------
found : array of int
Raises
------
ValueError
substring is not found
"""
return self.index(sub, start=start, end=end, side="right")
def replace(
self,
pat: str | bytes | Pattern | Any,
repl: str | bytes | Callable | Any,
n: int | Any = -1,
case: bool | None = None,
flags: int = 0,
regex: bool = True,
) -> T_DataArray:
"""
Replace occurrences of pattern/regex in the array with some string.
If `pat`, `repl`, or 'n` is array-like, they are broadcast
against the array and applied elementwise.
Parameters
----------
pat : str or re.Pattern or array-like of str or re.Pattern
String can be a character sequence or regular expression.
If array-like, it is broadcast.
repl : str or callable or array-like of str or callable
Replacement string or a callable. The callable is passed the regex
match object and must return a replacement string to be used.
See :func:`re.sub`.
If array-like, it is broadcast.
n : int or array of int, default: -1
Number of replacements to make from start. Use ``-1`` to replace all.
If array-like, it is broadcast.
case : bool, default: True
If True, case sensitive.
Cannot be set if `pat` is a compiled regex.
Equivalent to setting the `re.IGNORECASE` flag.
flags : int, default: 0
Flags to pass through to the re module, e.g. `re.IGNORECASE`.
see `compilation-flags <https://docs.python.org/3/howto/regex.html#compilation-flags>`_.
``0`` means no flags. Flags can be combined with the bitwise or operator ``|``.
Cannot be set if `pat` is a compiled regex.
regex : bool, default: True
If True, assumes the passed-in pattern is a regular expression.
If False, treats the pattern as a literal string.
Cannot be set to False if `pat` is a compiled regex or `repl` is
a callable.
Returns
-------
replaced : same type as values
A copy of the object with all matching occurrences of `pat`
replaced by `repl`.
"""
if _contains_str_like(repl):
repl = self._stringify(repl)
elif not _contains_callable(repl): # pragma: no cover
raise TypeError("repl must be a string or callable")
is_compiled_re = _contains_compiled_re(pat)
if not regex and is_compiled_re:
raise ValueError(
"Cannot use a compiled regex as replacement pattern with regex=False"
)
if not regex and callable(repl):
raise ValueError("Cannot use a callable replacement when regex=False")
if regex:
pat = self._re_compile(pat=pat, flags=flags, case=case)
func = lambda x, ipat, irepl, i_n: ipat.sub(
repl=irepl, string=x, count=max(i_n, 0)
)
else:
pat = self._stringify(pat)
func = lambda x, ipat, irepl, i_n: x.replace(ipat, irepl, i_n)
return self._apply(func=func, func_args=(pat, repl, n))
def extract(
self,
pat: str | bytes | Pattern | Any,
dim: Hashable,
case: bool | None = None,
flags: int = 0,
) -> T_DataArray:
r"""
Extract the first match of capture groups in the regex pat as a new
dimension in a DataArray.
For each string in the DataArray, extract groups from the first match
of regular expression pat.
If `pat` is array-like, it is broadcast against the array and applied
elementwise.
Parameters
----------
pat : str or re.Pattern or array-like of str or re.Pattern
A string containing a regular expression or a compiled regular
expression object. If array-like, it is broadcast.
dim : hashable or None
Name of the new dimension to store the captured strings in.
If None, the pattern must have only one capture group and the
resulting DataArray will have the same size as the original.
case : bool, default: True
If True, case sensitive.
Cannot be set if `pat` is a compiled regex.
Equivalent to setting the `re.IGNORECASE` flag.
flags : int, default: 0
Flags to pass through to the re module, e.g. `re.IGNORECASE`.
see `compilation-flags <https://docs.python.org/3/howto/regex.html#compilation-flags>`_.
``0`` means no flags. Flags can be combined with the bitwise or operator ``|``.
Cannot be set if `pat` is a compiled regex.
Returns
-------
extracted : same type as values or object array
Raises
------
ValueError
`pat` has no capture groups.
ValueError
`dim` is None and there is more than one capture group.
ValueError
`case` is set when `pat` is a compiled regular expression.
KeyError
The given dimension is already present in the DataArray.
Examples
--------
Create a string array
>>> value = xr.DataArray(
... [
... [
... "a_Xy_0",
... "ab_xY_10-bab_Xy_110-baab_Xy_1100",
... "abc_Xy_01-cbc_Xy_2210",
... ],
... [
... "abcd_Xy_-dcd_Xy_33210-dccd_Xy_332210",
... "",
... "abcdef_Xy_101-fef_Xy_5543210",
... ],
... ],
... dims=["X", "Y"],
... )
Extract matches
>>> value.str.extract(r"(\w+)_Xy_(\d*)", dim="match")
<xarray.DataArray (X: 2, Y: 3, match: 2)> Size: 288B
array([[['a', '0'],
['bab', '110'],
['abc', '01']],
<BLANKLINE>
[['abcd', ''],
['', ''],
['abcdef', '101']]], dtype='<U6')
Dimensions without coordinates: X, Y, match
See Also
--------
DataArray.str.extractall
DataArray.str.findall
re.compile
re.search
pandas.Series.str.extract
"""
pat = self._re_compile(pat=pat, flags=flags, case=case)
if isinstance(pat, re.Pattern):
maxgroups = pat.groups
else:
maxgroups = (
_apply_str_ufunc(obj=pat, func=lambda x: x.groups, dtype=np.int_)
.max()
.data.tolist()
)
if maxgroups == 0:
raise ValueError("No capture groups found in pattern.")
if dim is None and maxgroups != 1:
raise ValueError(
"Dimension must be specified if more than one capture group is given."
)
if dim is not None and dim in self._obj.dims:
raise KeyError(f"Dimension '{dim}' already present in DataArray.")
def _get_res_single(val, pat):
match = pat.search(val)
if match is None:
return ""
res = match.group(1)
if res is None:
res = ""
return res
def _get_res_multi(val, pat):
match = pat.search(val)
if match is None:
return np.array([""], val.dtype)
match = match.groups()
match = [grp if grp is not None else "" for grp in match]
return np.array(match, val.dtype)
if dim is None:
return self._apply(func=_get_res_single, func_args=(pat,))
else:
# dtype MUST be object or strings can be truncated
# See: https://github.com/numpy/numpy/issues/8352
return duck_array_ops.astype(
self._apply(
func=_get_res_multi,
func_args=(pat,),
dtype=np.object_,
output_core_dims=[[dim]],
output_sizes={dim: maxgroups},
),
self._obj.dtype.kind,
)
def extractall(
self,
pat: str | bytes | Pattern | Any,
group_dim: Hashable,
match_dim: Hashable,
case: bool | None = None,
flags: int = 0,
) -> T_DataArray:
r"""
Extract all matches of capture groups in the regex pat as new
dimensions in a DataArray.
For each string in the DataArray, extract groups from all matches
of regular expression pat.
Equivalent to applying re.findall() to all the elements in the DataArray
and splitting the results across dimensions.
If `pat` is array-like, it is broadcast against the array and applied
elementwise.
Parameters
----------
pat : str or re.Pattern
A string containing a regular expression or a compiled regular
expression object. If array-like, it is broadcast.
group_dim : hashable
Name of the new dimensions corresponding to the capture groups.
This dimension is added to the new DataArray first.
match_dim : hashable
Name of the new dimensions corresponding to the matches for each group.
This dimension is added to the new DataArray second.
case : bool, default: True
If True, case sensitive.
Cannot be set if `pat` is a compiled regex.
Equivalent to setting the `re.IGNORECASE` flag.
flags : int, default: 0
Flags to pass through to the re module, e.g. `re.IGNORECASE`.
see `compilation-flags <https://docs.python.org/3/howto/regex.html#compilation-flags>`_.
``0`` means no flags. Flags can be combined with the bitwise or operator ``|``.
Cannot be set if `pat` is a compiled regex.
Returns
-------
extracted : same type as values or object array
Raises
------
ValueError
`pat` has no capture groups.
ValueError
`case` is set when `pat` is a compiled regular expression.
KeyError
Either of the given dimensions is already present in the DataArray.
KeyError
The given dimensions names are the same.
Examples
--------
Create a string array
>>> value = xr.DataArray(
... [
... [
... "a_Xy_0",
... "ab_xY_10-bab_Xy_110-baab_Xy_1100",
... "abc_Xy_01-cbc_Xy_2210",
... ],
... [
... "abcd_Xy_-dcd_Xy_33210-dccd_Xy_332210",
... "",
... "abcdef_Xy_101-fef_Xy_5543210",
... ],
... ],
... dims=["X", "Y"],
... )
Extract matches
>>> value.str.extractall(
... r"(\w+)_Xy_(\d*)", group_dim="group", match_dim="match"
... )
<xarray.DataArray (X: 2, Y: 3, group: 3, match: 2)> Size: 1kB
array([[[['a', '0'],
['', ''],
['', '']],
<BLANKLINE>
[['bab', '110'],
['baab', '1100'],
['', '']],
<BLANKLINE>
[['abc', '01'],
['cbc', '2210'],
['', '']]],
<BLANKLINE>
<BLANKLINE>
[[['abcd', ''],
['dcd', '33210'],
['dccd', '332210']],
<BLANKLINE>
[['', ''],
['', ''],
['', '']],
<BLANKLINE>
[['abcdef', '101'],
['fef', '5543210'],
['', '']]]], dtype='<U7')
Dimensions without coordinates: X, Y, group, match
See Also
--------
DataArray.str.extract
DataArray.str.findall
re.compile
re.findall
pandas.Series.str.extractall
"""
pat = self._re_compile(pat=pat, flags=flags, case=case)
if group_dim in self._obj.dims:
raise KeyError(
f"Group dimension '{group_dim}' already present in DataArray."
)
if match_dim in self._obj.dims:
raise KeyError(
f"Match dimension '{match_dim}' already present in DataArray."
)
if group_dim == match_dim:
raise KeyError(
f"Group dimension '{group_dim}' is the same as match dimension '{match_dim}'."
)
_get_count = lambda x, ipat: len(ipat.findall(x))
maxcount = (
self._apply(func=_get_count, func_args=(pat,), dtype=np.int_)
.max()
.data.tolist()
)
if isinstance(pat, re.Pattern):
maxgroups = pat.groups
else:
maxgroups = (
_apply_str_ufunc(obj=pat, func=lambda x: x.groups, dtype=np.int_)
.max()
.data.tolist()
)
def _get_res(val, ipat, imaxcount=maxcount, dtype=self._obj.dtype):
if ipat.groups == 0:
raise ValueError("No capture groups found in pattern.")
matches = ipat.findall(val)
res = np.zeros([maxcount, ipat.groups], dtype)
if ipat.groups == 1:
for imatch, match in enumerate(matches):
res[imatch, 0] = match
else:
for imatch, match in enumerate(matches):
for jmatch, submatch in enumerate(match):
res[imatch, jmatch] = submatch
return res
return duck_array_ops.astype(
self._apply(
# dtype MUST be object or strings can be truncated
# See: https://github.com/numpy/numpy/issues/8352
func=_get_res,
func_args=(pat,),
dtype=np.object_,
output_core_dims=[[group_dim, match_dim]],
output_sizes={group_dim: maxgroups, match_dim: maxcount},
),
self._obj.dtype.kind,
)
def findall(
self,
pat: str | bytes | Pattern | Any,
case: bool | None = None,
flags: int = 0,
) -> T_DataArray:
r"""
Find all occurrences of pattern or regular expression in the DataArray.
Equivalent to applying re.findall() to all the elements in the DataArray.
Results in an object array of lists.
If there is only one capture group, the lists will be a sequence of matches.
If there are multiple capture groups, the lists will be a sequence of lists,
each of which contains a sequence of matches.
If `pat` is array-like, it is broadcast against the array and applied
elementwise.
Parameters
----------
pat : str or re.Pattern
A string containing a regular expression or a compiled regular
expression object. If array-like, it is broadcast.
case : bool, default: True
If True, case sensitive.
Cannot be set if `pat` is a compiled regex.
Equivalent to setting the `re.IGNORECASE` flag.
flags : int, default: 0
Flags to pass through to the re module, e.g. `re.IGNORECASE`.
see `compilation-flags <https://docs.python.org/3/howto/regex.html#compilation-flags>`_.
``0`` means no flags. Flags can be combined with the bitwise or operator ``|``.
Cannot be set if `pat` is a compiled regex.
Returns
-------
extracted : object array
Raises
------
ValueError
`pat` has no capture groups.
ValueError
`case` is set when `pat` is a compiled regular expression.
Examples
--------
Create a string array
>>> value = xr.DataArray(
... [
... [
... "a_Xy_0",
... "ab_xY_10-bab_Xy_110-baab_Xy_1100",
... "abc_Xy_01-cbc_Xy_2210",
... ],
... [
... "abcd_Xy_-dcd_Xy_33210-dccd_Xy_332210",
... "",
... "abcdef_Xy_101-fef_Xy_5543210",
... ],
... ],
... dims=["X", "Y"],
... )
Extract matches
>>> value.str.findall(r"(\w+)_Xy_(\d*)")
<xarray.DataArray (X: 2, Y: 3)> Size: 48B
array([[list([('a', '0')]), list([('bab', '110'), ('baab', '1100')]),
list([('abc', '01'), ('cbc', '2210')])],
[list([('abcd', ''), ('dcd', '33210'), ('dccd', '332210')]),
list([]), list([('abcdef', '101'), ('fef', '5543210')])]],
dtype=object)
Dimensions without coordinates: X, Y
See Also
--------
DataArray.str.extract
DataArray.str.extractall
re.compile
re.findall
pandas.Series.str.findall
"""
pat = self._re_compile(pat=pat, flags=flags, case=case)
def func(x, ipat):
if ipat.groups == 0:
raise ValueError("No capture groups found in pattern.")
return ipat.findall(x)
return self._apply(func=func, func_args=(pat,), dtype=np.object_)
def _partitioner(
self,
*,
func: Callable,
dim: Hashable | None,
sep: str | bytes | Any | None,
) -> T_DataArray:
"""
Implements logic for `partition` and `rpartition`.
"""
sep = self._stringify(sep)
if dim is None:
listfunc = lambda x, isep: list(func(x, isep))
return self._apply(func=listfunc, func_args=(sep,), dtype=np.object_)
# _apply breaks on an empty array in this case
if not self._obj.size:
return self._obj.copy().expand_dims({dim: 0}, axis=-1)
arrfunc = lambda x, isep: np.array(func(x, isep), dtype=self._obj.dtype)
# dtype MUST be object or strings can be truncated
# See: https://github.com/numpy/numpy/issues/8352
return duck_array_ops.astype(
self._apply(
func=arrfunc,
func_args=(sep,),
dtype=np.object_,
output_core_dims=[[dim]],
output_sizes={dim: 3},
),
self._obj.dtype.kind,
)
def partition(
self,
dim: Hashable | None,
sep: str | bytes | Any = " ",
) -> T_DataArray:
"""
Split the strings in the DataArray at the first occurrence of separator `sep`.
This method splits the string at the first occurrence of `sep`,
and returns 3 elements containing the part before the separator,
the separator itself, and the part after the separator.
If the separator is not found, return 3 elements containing the string itself,
followed by two empty strings.
If `sep` is array-like, it is broadcast against the array and applied
elementwise.
Parameters
----------
dim : hashable or None
Name for the dimension to place the 3 elements in.
If `None`, place the results as list elements in an object DataArray.
sep : str or bytes or array-like, default: " "
String to split on.
If array-like, it is broadcast.
Returns
-------
partitioned : same type as values or object array
See Also
--------
DataArray.str.rpartition
str.partition
pandas.Series.str.partition
"""
return self._partitioner(func=self._obj.dtype.type.partition, dim=dim, sep=sep)
def rpartition(
self,
dim: Hashable | None,
sep: str | bytes | Any = " ",
) -> T_DataArray:
"""
Split the strings in the DataArray at the last occurrence of separator `sep`.
This method splits the string at the last occurrence of `sep`,
and returns 3 elements containing the part before the separator,
the separator itself, and the part after the separator.
If the separator is not found, return 3 elements containing two empty strings,
followed by the string itself.
If `sep` is array-like, it is broadcast against the array and applied
elementwise.
Parameters
----------
dim : hashable or None
Name for the dimension to place the 3 elements in.
If `None`, place the results as list elements in an object DataArray.
sep : str or bytes or array-like, default: " "
String to split on.
If array-like, it is broadcast.
Returns
-------
rpartitioned : same type as values or object array
See Also
--------
DataArray.str.partition
str.rpartition
pandas.Series.str.rpartition
"""
return self._partitioner(func=self._obj.dtype.type.rpartition, dim=dim, sep=sep)
def _splitter(
self,
*,
func: Callable,
pre: bool,
dim: Hashable,
sep: str | bytes | Any | None,
maxsplit: int,
) -> DataArray:
"""
Implements logic for `split` and `rsplit`.
"""
if sep is not None:
sep = self._stringify(sep)
if dim is None:
f_none = lambda x, isep: func(x, isep, maxsplit)
return self._apply(func=f_none, func_args=(sep,), dtype=np.object_)
# _apply breaks on an empty array in this case
if not self._obj.size:
return self._obj.copy().expand_dims({dim: 0}, axis=-1)
f_count = lambda x, isep: max(len(func(x, isep, maxsplit)), 1)
maxsplit = (
self._apply(func=f_count, func_args=(sep,), dtype=np.int_).max().data.item()
- 1
)
def _dosplit(mystr, sep, maxsplit=maxsplit, dtype=self._obj.dtype):
res = func(mystr, sep, maxsplit)
if len(res) < maxsplit + 1:
pad = [""] * (maxsplit + 1 - len(res))
if pre:
res += pad
else:
res = pad + res
return np.array(res, dtype=dtype)
# dtype MUST be object or strings can be truncated
# See: https://github.com/numpy/numpy/issues/8352
return duck_array_ops.astype(
self._apply(
func=_dosplit,
func_args=(sep,),
dtype=np.object_,
output_core_dims=[[dim]],
output_sizes={dim: maxsplit},
),
self._obj.dtype.kind,
)
def split(
self,
dim: Hashable | None,
sep: str | bytes | Any = None,
maxsplit: int = -1,
) -> DataArray:
r"""
Split strings in a DataArray around the given separator/delimiter `sep`.
Splits the string in the DataArray from the beginning,
at the specified delimiter string.
If `sep` is array-like, it is broadcast against the array and applied
elementwise.
Parameters
----------
dim : hashable or None
Name for the dimension to place the results in.
If `None`, place the results as list elements in an object DataArray.
sep : str, default: None
String to split on. If ``None`` (the default), split on any whitespace.
If array-like, it is broadcast.
maxsplit : int, default: -1
Limit number of splits in output, starting from the beginning.
If -1 (the default), return all splits.
Returns
-------
splitted : same type as values or object array
Examples
--------
Create a string DataArray
>>> values = xr.DataArray(
... [
... ["abc def", "spam\t\teggs\tswallow", "red_blue"],
... ["test0\ntest1\ntest2\n\ntest3", "", "abra ka\nda\tbra"],
... ],
... dims=["X", "Y"],
... )
Split once and put the results in a new dimension
>>> values.str.split(dim="splitted", maxsplit=1)
<xarray.DataArray (X: 2, Y: 3, splitted: 2)> Size: 864B
array([[['abc', 'def'],
['spam', 'eggs\tswallow'],
['red_blue', '']],
<BLANKLINE>
[['test0', 'test1\ntest2\n\ntest3'],
['', ''],
['abra', 'ka\nda\tbra']]], dtype='<U18')
Dimensions without coordinates: X, Y, splitted
Split as many times as needed and put the results in a new dimension
>>> values.str.split(dim="splitted")
<xarray.DataArray (X: 2, Y: 3, splitted: 4)> Size: 768B
array([[['abc', 'def', '', ''],
['spam', 'eggs', 'swallow', ''],
['red_blue', '', '', '']],
<BLANKLINE>
[['test0', 'test1', 'test2', 'test3'],
['', '', '', ''],
['abra', 'ka', 'da', 'bra']]], dtype='<U8')
Dimensions without coordinates: X, Y, splitted
Split once and put the results in lists
>>> values.str.split(dim=None, maxsplit=1)
<xarray.DataArray (X: 2, Y: 3)> Size: 48B
array([[list(['abc', 'def']), list(['spam', 'eggs\tswallow']),
list(['red_blue'])],
[list(['test0', 'test1\ntest2\n\ntest3']), list([]),
list(['abra', 'ka\nda\tbra'])]], dtype=object)
Dimensions without coordinates: X, Y
Split as many times as needed and put the results in a list
>>> values.str.split(dim=None)
<xarray.DataArray (X: 2, Y: 3)> Size: 48B
array([[list(['abc', 'def']), list(['spam', 'eggs', 'swallow']),
list(['red_blue'])],
[list(['test0', 'test1', 'test2', 'test3']), list([]),
list(['abra', 'ka', 'da', 'bra'])]], dtype=object)
Dimensions without coordinates: X, Y
Split only on spaces
>>> values.str.split(dim="splitted", sep=" ")
<xarray.DataArray (X: 2, Y: 3, splitted: 3)> Size: 2kB
array([[['abc', 'def', ''],
['spam\t\teggs\tswallow', '', ''],
['red_blue', '', '']],
<BLANKLINE>
[['test0\ntest1\ntest2\n\ntest3', '', ''],
['', '', ''],
['abra', '', 'ka\nda\tbra']]], dtype='<U24')
Dimensions without coordinates: X, Y, splitted
See Also
--------
DataArray.str.rsplit
str.split
pandas.Series.str.split
"""
return self._splitter(
func=self._obj.dtype.type.split,
pre=True,
dim=dim,
sep=sep,
maxsplit=maxsplit,
)
def rsplit(
self,
dim: Hashable | None,
sep: str | bytes | Any = None,
maxsplit: int | Any = -1,
) -> DataArray:
r"""
Split strings in a DataArray around the given separator/delimiter `sep`.
Splits the string in the DataArray from the end,
at the specified delimiter string.
If `sep` is array-like, it is broadcast against the array and applied
elementwise.
Parameters
----------
dim : hashable or None
Name for the dimension to place the results in.
If `None`, place the results as list elements in an object DataArray
sep : str, default: None
String to split on. If ``None`` (the default), split on any whitespace.
If array-like, it is broadcast.
maxsplit : int, default: -1
Limit number of splits in output, starting from the end.
If -1 (the default), return all splits.
The final number of split values may be less than this if there are no
DataArray elements with that many values.
Returns
-------
rsplitted : same type as values or object array
Examples
--------
Create a string DataArray
>>> values = xr.DataArray(
... [
... ["abc def", "spam\t\teggs\tswallow", "red_blue"],
... ["test0\ntest1\ntest2\n\ntest3", "", "abra ka\nda\tbra"],
... ],
... dims=["X", "Y"],
... )
Split once and put the results in a new dimension
>>> values.str.rsplit(dim="splitted", maxsplit=1)
<xarray.DataArray (X: 2, Y: 3, splitted: 2)> Size: 816B
array([[['abc', 'def'],
['spam\t\teggs', 'swallow'],
['', 'red_blue']],
<BLANKLINE>
[['test0\ntest1\ntest2', 'test3'],
['', ''],
['abra ka\nda', 'bra']]], dtype='<U17')
Dimensions without coordinates: X, Y, splitted
Split as many times as needed and put the results in a new dimension
>>> values.str.rsplit(dim="splitted")
<xarray.DataArray (X: 2, Y: 3, splitted: 4)> Size: 768B
array([[['', '', 'abc', 'def'],
['', 'spam', 'eggs', 'swallow'],
['', '', '', 'red_blue']],
<BLANKLINE>
[['test0', 'test1', 'test2', 'test3'],
['', '', '', ''],
['abra', 'ka', 'da', 'bra']]], dtype='<U8')
Dimensions without coordinates: X, Y, splitted
Split once and put the results in lists
>>> values.str.rsplit(dim=None, maxsplit=1)
<xarray.DataArray (X: 2, Y: 3)> Size: 48B
array([[list(['abc', 'def']), list(['spam\t\teggs', 'swallow']),
list(['red_blue'])],
[list(['test0\ntest1\ntest2', 'test3']), list([]),
list(['abra ka\nda', 'bra'])]], dtype=object)
Dimensions without coordinates: X, Y
Split as many times as needed and put the results in a list
>>> values.str.rsplit(dim=None)
<xarray.DataArray (X: 2, Y: 3)> Size: 48B
array([[list(['abc', 'def']), list(['spam', 'eggs', 'swallow']),
list(['red_blue'])],
[list(['test0', 'test1', 'test2', 'test3']), list([]),
list(['abra', 'ka', 'da', 'bra'])]], dtype=object)
Dimensions without coordinates: X, Y
Split only on spaces
>>> values.str.rsplit(dim="splitted", sep=" ")
<xarray.DataArray (X: 2, Y: 3, splitted: 3)> Size: 2kB
array([[['', 'abc', 'def'],
['', '', 'spam\t\teggs\tswallow'],
['', '', 'red_blue']],
<BLANKLINE>
[['', '', 'test0\ntest1\ntest2\n\ntest3'],
['', '', ''],
['abra', '', 'ka\nda\tbra']]], dtype='<U24')
Dimensions without coordinates: X, Y, splitted
See Also
--------
DataArray.str.split
str.rsplit
pandas.Series.str.rsplit
"""
return self._splitter(
func=self._obj.dtype.type.rsplit,
pre=False,
dim=dim,
sep=sep,
maxsplit=maxsplit,
)
def get_dummies(
self,
dim: Hashable,
sep: str | bytes | Any = "|",
) -> DataArray:
"""
Return DataArray of dummy/indicator variables.
Each string in the DataArray is split at `sep`.
A new dimension is created with coordinates for each unique result,
and the corresponding element of that dimension is `True` if
that result is present and `False` if not.
If `sep` is array-like, it is broadcast against the array and applied
elementwise.
Parameters
----------
dim : hashable
Name for the dimension to place the results in.
sep : str, default: "|".
String to split on.
If array-like, it is broadcast.
Returns
-------
dummies : array of bool
Examples
--------
Create a string array
>>> values = xr.DataArray(
... [
... ["a|ab~abc|abc", "ab", "a||abc|abcd"],
... ["abcd|ab|a", "abc|ab~abc", "|a"],
... ],
... dims=["X", "Y"],
... )
Extract dummy values
>>> values.str.get_dummies(dim="dummies")
<xarray.DataArray (X: 2, Y: 3, dummies: 5)> Size: 30B
array([[[ True, False, True, False, True],
[False, True, False, False, False],
[ True, False, True, True, False]],
<BLANKLINE>
[[ True, True, False, True, False],
[False, False, True, False, True],
[ True, False, False, False, False]]])
Coordinates:
* dummies (dummies) <U6 120B 'a' 'ab' 'abc' 'abcd' 'ab~abc'
Dimensions without coordinates: X, Y
See Also
--------
pandas.Series.str.get_dummies
"""
# _apply breaks on an empty array in this case
if not self._obj.size:
return self._obj.copy().expand_dims({dim: 0}, axis=-1)
sep = self._stringify(sep)
f_set = lambda x, isep: set(x.split(isep)) - {self._stringify("")}
setarr = self._apply(func=f_set, func_args=(sep,), dtype=np.object_)
vals = sorted(reduce(set_union, setarr.data.ravel()))
func = lambda x: np.array([val in x for val in vals], dtype=np.bool_)
res = _apply_str_ufunc(
func=func,
obj=setarr,
output_core_dims=[[dim]],
output_sizes={dim: len(vals)},
dtype=np.bool_,
)
res.coords[dim] = vals
return res
def decode(self, encoding: str, errors: str = "strict") -> T_DataArray:
"""
Decode character string in the array using indicated encoding.
Parameters
----------
encoding : str
The encoding to use.
Please see the Python documentation `codecs standard encoders <https://docs.python.org/3/library/codecs.html#standard-encodings>`_
section for a list of encodings handlers.
errors : str, default: "strict"
The handler for encoding errors.
Please see the Python documentation `codecs error handlers <https://docs.python.org/3/library/codecs.html#error-handlers>`_
for a list of error handlers.
Returns
-------
decoded : same type as values
"""
if encoding in _cpython_optimized_decoders:
func = lambda x: x.decode(encoding, errors)
else:
decoder = codecs.getdecoder(encoding)
func = lambda x: decoder(x, errors)[0]
return self._apply(func=func, dtype=np.str_)
def encode(self, encoding: str, errors: str = "strict") -> T_DataArray:
"""
Encode character string in the array using indicated encoding.
Parameters
----------
encoding : str
The encoding to use.
Please see the Python documentation `codecs standard encoders <https://docs.python.org/3/library/codecs.html#standard-encodings>`_
section for a list of encodings handlers.
errors : str, default: "strict"
The handler for encoding errors.
Please see the Python documentation `codecs error handlers <https://docs.python.org/3/library/codecs.html#error-handlers>`_
for a list of error handlers.
Returns
-------
encoded : same type as values
"""
if encoding in _cpython_optimized_encoders:
func = lambda x: x.encode(encoding, errors)
else:
encoder = codecs.getencoder(encoding)
func = lambda x: encoder(x, errors)[0]
return self._apply(func=func, dtype=np.bytes_)
|