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
|
from __future__ import annotations
import math
from typing import (
TYPE_CHECKING,
Any,
Callable,
Generic,
Iterator,
Literal,
Mapping,
Sequence,
overload,
)
from narwhals._utils import (
_validate_rolling_arguments,
ensure_type,
generate_repr,
is_compliant_series,
is_index_selector,
parse_version,
supports_arrow_c_stream,
)
from narwhals.dependencies import is_numpy_scalar
from narwhals.dtypes import _validate_dtype
from narwhals.exceptions import ComputeError
from narwhals.series_cat import SeriesCatNamespace
from narwhals.series_dt import SeriesDateTimeNamespace
from narwhals.series_list import SeriesListNamespace
from narwhals.series_str import SeriesStringNamespace
from narwhals.series_struct import SeriesStructNamespace
from narwhals.translate import to_native
from narwhals.typing import IntoSeriesT
if TYPE_CHECKING:
from types import ModuleType
import pandas as pd
import polars as pl
import pyarrow as pa
from typing_extensions import Self
from narwhals._compliant import CompliantSeries
from narwhals._utils import Implementation
from narwhals.dataframe import DataFrame, MultiIndexSelector
from narwhals.dtypes import DType
from narwhals.typing import (
ClosedInterval,
FillNullStrategy,
IntoDType,
NonNestedLiteral,
NumericLiteral,
RankMethod,
RollingInterpolationMethod,
SingleIndexSelector,
TemporalLiteral,
_1DArray,
)
class Series(Generic[IntoSeriesT]):
"""Narwhals Series, backed by a native series.
Warning:
This class is not meant to be instantiated directly - instead:
- If the native object is a series from one of the supported backend (e.g.
pandas.Series, polars.Series, pyarrow.ChunkedArray), you can use
[`narwhals.from_native`][]:
```py
narwhals.from_native(native_series, allow_series=True)
narwhals.from_native(native_series, series_only=True)
```
- If the object is a generic sequence (e.g. a list or a tuple of values), you can
create a series via [`narwhals.new_series`][], e.g.:
```py
narwhals.new_series(name="price", values=[10.5, 9.4, 1.2], backend="pandas")
```
"""
@property
def _dataframe(self) -> type[DataFrame[Any]]:
from narwhals.dataframe import DataFrame
return DataFrame
def __init__(
self, series: Any, *, level: Literal["full", "lazy", "interchange"]
) -> None:
self._level: Literal["full", "lazy", "interchange"] = level
if is_compliant_series(series):
self._compliant_series: CompliantSeries[IntoSeriesT] = (
series.__narwhals_series__()
)
else: # pragma: no cover
msg = f"Expected Polars Series or an object which implements `__narwhals_series__`, got: {type(series)}."
raise AssertionError(msg)
@property
def implementation(self) -> Implementation:
"""Return implementation of native Series.
This can be useful when you need to use special-casing for features outside of
Narwhals' scope - for example, when dealing with pandas' Period Dtype.
Returns:
Implementation.
Examples:
>>> import narwhals as nw
>>> import pandas as pd
>>> s_native = pd.Series([1, 2, 3])
>>> s = nw.from_native(s_native, series_only=True)
>>> s.implementation
<Implementation.PANDAS: 'pandas'>
>>> s.implementation.is_pandas()
True
>>> s.implementation.is_pandas_like()
True
>>> s.implementation.is_polars()
False
"""
return self._compliant_series._implementation
def __array__(self, dtype: Any = None, copy: bool | None = None) -> _1DArray: # noqa: FBT001
return self._compliant_series.__array__(dtype=dtype, copy=copy)
@overload
def __getitem__(self, idx: SingleIndexSelector) -> Any: ...
@overload
def __getitem__(self, idx: MultiIndexSelector) -> Self: ...
def __getitem__(self, idx: SingleIndexSelector | MultiIndexSelector) -> Any | Self:
"""Retrieve elements from the object using integer indexing or slicing.
Arguments:
idx: The index, slice, or sequence of indices to retrieve.
- If `idx` is an integer, a single element is returned.
- If `idx` is a slice, a sequence of integers, or another Series
(with integer values) a subset of the Series is returned.
Returns:
A single element if `idx` is an integer, else a subset of the Series.
Examples:
>>> import pyarrow as pa
>>> import narwhals as nw
>>>
>>> s_native = pa.chunked_array([[1, 2, 3]])
>>> nw.from_native(s_native, series_only=True)[0]
1
>>> nw.from_native(s_native, series_only=True)[
... :2
... ].to_native() # doctest:+ELLIPSIS
<pyarrow.lib.ChunkedArray object at ...>
[
[
1,
2
]
]
"""
if isinstance(idx, int) or (
is_numpy_scalar(idx) and idx.dtype.kind in {"i", "u"}
):
idx = int(idx) if not isinstance(idx, int) else idx
return self._compliant_series.item(idx)
if isinstance(idx, self.to_native().__class__):
idx = self._with_compliant(self._compliant_series._with_native(idx))
if not is_index_selector(idx):
msg = (
f"Unexpected type for `Series.__getitem__`: {type(idx)}.\n\n"
"Hints:\n"
"- use `s.item` to select a single item.\n"
"- Use `s[indices]` to select rows positionally.\n"
"- Use `s.filter(mask)` to filter rows based on a boolean mask."
)
raise TypeError(msg)
if isinstance(idx, Series):
return self._with_compliant(self._compliant_series[idx._compliant_series])
assert not isinstance(idx, int) # noqa: S101 # help mypy
return self._with_compliant(self._compliant_series[idx])
def __native_namespace__(self) -> ModuleType:
return self._compliant_series.__native_namespace__()
def __arrow_c_stream__(self, requested_schema: object | None = None) -> object:
"""Export a Series via the Arrow PyCapsule Interface.
Narwhals doesn't implement anything itself here:
- if the underlying series implements the interface, it'll return that
- else, it'll call `to_arrow` and then defer to PyArrow's implementation
See [PyCapsule Interface](https://arrow.apache.org/docs/dev/format/CDataInterface/PyCapsuleInterface.html)
for more.
"""
native_series = self._compliant_series.native
if supports_arrow_c_stream(native_series):
return native_series.__arrow_c_stream__(requested_schema=requested_schema)
try:
import pyarrow as pa # ignore-banned-import
except ModuleNotFoundError as exc: # pragma: no cover
msg = f"'pyarrow>=16.0.0' is required for `Series.__arrow_c_stream__` for object of type {type(native_series)}"
raise ModuleNotFoundError(msg) from exc
if parse_version(pa) < (16, 0): # pragma: no cover
msg = f"'pyarrow>=16.0.0' is required for `Series.__arrow_c_stream__` for object of type {type(native_series)}"
raise ModuleNotFoundError(msg)
from narwhals._arrow.utils import chunked_array
ca = chunked_array(self.to_arrow())
return ca.__arrow_c_stream__(requested_schema=requested_schema)
def to_native(self) -> IntoSeriesT:
"""Convert Narwhals series to native series.
Returns:
Series of class that user started with.
Examples:
>>> import polars as pl
>>> import narwhals as nw
>>>
>>> s_native = pl.Series([1, 2])
>>> s = nw.from_native(s_native, series_only=True)
>>> s.to_native() # doctest: +NORMALIZE_WHITESPACE
shape: (2,)
Series: '' [i64]
[
1
2
]
"""
return self._compliant_series.native
def scatter(self, indices: int | Sequence[int], values: Any) -> Self:
"""Set value(s) at given position(s).
Arguments:
indices: Position(s) to set items at.
values: Values to set.
Returns:
A new Series with values set at given positions.
Note:
This method always returns a new Series, without modifying the original one.
Using this function in a for-loop is an anti-pattern, we recommend building
up your positions and values beforehand and doing an update in one go.
For example, instead of
```python
for i in [1, 3, 2]:
value = some_function(i)
s = s.scatter(i, value)
```
prefer
```python
positions = [1, 3, 2]
values = [some_function(x) for x in positions]
s = s.scatter(positions, values)
```
Examples:
>>> import pyarrow as pa
>>> import narwhals as nw
>>>
>>> df_native = pa.table({"a": [1, 2, 3], "b": [4, 5, 6]})
>>> df_nw = nw.from_native(df_native)
>>> df_nw.with_columns(df_nw["a"].scatter([0, 1], [999, 888])).to_native()
pyarrow.Table
a: int64
b: int64
----
a: [[999,888,3]]
b: [[4,5,6]]
"""
return self._with_compliant(
self._compliant_series.scatter(indices, self._extract_native(values))
)
@property
def shape(self) -> tuple[int]:
"""Get the shape of the Series.
Returns:
A tuple containing the length of the Series.
Examples:
>>> import pandas as pd
>>> import narwhals as nw
>>>
>>> s_native = pd.Series([1, 2, 3])
>>> nw.from_native(s_native, series_only=True).shape
(3,)
"""
return (self._compliant_series.len(),)
def _extract_native(self, arg: Any) -> Any:
from narwhals.series import Series
if isinstance(arg, Series):
return arg._compliant_series
return arg
def _with_compliant(self, series: Any) -> Self:
return self.__class__(series, level=self._level)
def pipe(self, function: Callable[[Any], Self], *args: Any, **kwargs: Any) -> Self:
"""Pipe function call.
Returns:
A new Series with the results of the piped function applied.
Examples:
>>> import polars as pl
>>> import narwhals as nw
>>> s_native = pl.Series([1, 2, 3])
>>> s = nw.from_native(s_native, series_only=True)
>>> s.pipe(lambda x: x + 2).to_native() # doctest: +NORMALIZE_WHITESPACE
shape: (3,)
Series: '' [i64]
[
3
4
5
]
"""
return function(self, *args, **kwargs)
def __repr__(self) -> str: # pragma: no cover
return generate_repr("Narwhals Series", self.to_native().__repr__())
def __len__(self) -> int:
return len(self._compliant_series)
def len(self) -> int:
r"""Return the number of elements in the Series.
Null values count towards the total.
Returns:
The number of elements in the Series.
Examples:
>>> import pyarrow as pa
>>> import narwhals as nw
>>>
>>> s_native = pa.chunked_array([[1, 2, None]])
>>> nw.from_native(s_native, series_only=True).len()
3
"""
return len(self._compliant_series)
@property
def dtype(self) -> DType:
"""Get the data type of the Series.
Returns:
The data type of the Series.
Examples:
>>> import pandas as pd
>>> import narwhals as nw
>>>
>>> s_native = pd.Series([1, 2, 3])
>>> nw.from_native(s_native, series_only=True).dtype
Int64
"""
return self._compliant_series.dtype
@property
def name(self) -> str:
"""Get the name of the Series.
Returns:
The name of the Series.
Examples:
>>> import polars as pl
>>> import narwhals as nw
>>>
>>> s_native = pl.Series("foo", [1, 2, 3])
>>> nw.from_native(s_native, series_only=True).name
'foo'
"""
return self._compliant_series.name
def ewm_mean(
self,
*,
com: float | None = None,
span: float | None = None,
half_life: float | None = None,
alpha: float | None = None,
adjust: bool = True,
min_samples: int = 1,
ignore_nulls: bool = False,
) -> Self:
r"""Compute exponentially-weighted moving average.
Arguments:
com: Specify decay in terms of center of mass, $\gamma$, with <br> $\alpha = \frac{1}{1+\gamma}\forall\gamma\geq0$
span: Specify decay in terms of span, $\theta$, with <br> $\alpha = \frac{2}{\theta + 1} \forall \theta \geq 1$
half_life: Specify decay in terms of half-life, $\tau$, with <br> $\alpha = 1 - \exp \left\{ \frac{ -\ln(2) }{ \tau } \right\} \forall \tau > 0$
alpha: Specify smoothing factor alpha directly, $0 < \alpha \leq 1$.
adjust: Divide by decaying adjustment factor in beginning periods to account for imbalance in relative weightings
- When `adjust=True` (the default) the EW function is calculated
using weights $w_i = (1 - \alpha)^i$
- When `adjust=False` the EW function is calculated recursively by
$$
y_0=x_0
$$
$$
y_t = (1 - \alpha)y_{t - 1} + \alpha x_t
$$
min_samples: Minimum number of observations in window required to have a value (otherwise result is null).
ignore_nulls: Ignore missing values when calculating weights.
- When `ignore_nulls=False` (default), weights are based on absolute
positions.
For example, the weights of $x_0$ and $x_2$ used in
calculating the final weighted average of $[x_0, None, x_2]$ are
$(1-\alpha)^2$ and $1$ if `adjust=True`, and
$(1-\alpha)^2$ and $\alpha$ if `adjust=False`.
- When `ignore_nulls=True`, weights are based
on relative positions. For example, the weights of
$x_0$ and $x_2$ used in calculating the final weighted
average of $[x_0, None, x_2]$ are
$1-\alpha$ and $1$ if `adjust=True`,
and $1-\alpha$ and $\alpha$ if `adjust=False`.
Returns:
Series
Examples:
>>> import pandas as pd
>>> import narwhals as nw
>>>
>>> s_native = pd.Series(name="a", data=[1, 2, 3])
>>> nw.from_native(s_native, series_only=True).ewm_mean(
... com=1, ignore_nulls=False
... ).to_native()
0 1.000000
1 1.666667
2 2.428571
Name: a, dtype: float64
"""
return self._with_compliant(
self._compliant_series.ewm_mean(
com=com,
span=span,
half_life=half_life,
alpha=alpha,
adjust=adjust,
min_samples=min_samples,
ignore_nulls=ignore_nulls,
)
)
def cast(self, dtype: IntoDType) -> Self:
"""Cast between data types.
Arguments:
dtype: Data type that the object will be cast into.
Returns:
A new Series with the specified data type.
Examples:
>>> import pyarrow as pa
>>> import narwhals as nw
>>>
>>> s_native = pa.chunked_array([[True, False, True]])
>>> nw.from_native(s_native, series_only=True).cast(nw.Int64).to_native()
<pyarrow.lib.ChunkedArray object at ...>
[
[
1,
0,
1
]
]
"""
_validate_dtype(dtype)
return self._with_compliant(self._compliant_series.cast(dtype))
def to_frame(self) -> DataFrame[Any]:
"""Convert to dataframe.
Returns:
A DataFrame containing this Series as a single column.
Examples:
>>> import polars as pl
>>> import narwhals as nw
>>>
>>> s_native = pl.Series("a", [1, 2])
>>> nw.from_native(s_native, series_only=True).to_frame().to_native()
shape: (2, 1)
┌─────┐
│ a │
│ --- │
│ i64 │
╞═════╡
│ 1 │
│ 2 │
└─────┘
"""
return self._dataframe(self._compliant_series.to_frame(), level=self._level)
def to_list(self) -> list[Any]:
"""Convert to list.
Notes:
This function converts to Python scalars. It's typically
more efficient to keep your data in the format native to
your original dataframe, so we recommend only calling this
when you absolutely need to.
Returns:
A list of Python objects.
Examples:
>>> import pyarrow as pa
>>> import narwhals as nw
>>>
>>> s_native = pa.chunked_array([[1, 2, 3]])
>>> nw.from_native(s_native, series_only=True).to_list()
[1, 2, 3]
"""
return self._compliant_series.to_list()
def mean(self) -> float:
"""Reduce this Series to the mean value.
Returns:
The average of all elements in the Series.
Examples:
>>> import pandas as pd
>>> import narwhals as nw
>>>
>>> s_native = pd.Series([1.2, 4.2])
>>> nw.from_native(s_native, series_only=True).mean()
np.float64(2.7)
"""
return self._compliant_series.mean()
def median(self) -> float:
"""Reduce this Series to the median value.
Notes:
Results might slightly differ across backends due to differences in the underlying algorithms used to compute the median.
Returns:
The median value of all elements in the Series.
Examples:
>>> import pyarrow as pa
>>> import narwhals as nw
>>>
>>> s_native = pa.chunked_array([[5, 3, 8]])
>>> nw.from_native(s_native, series_only=True).median()
5.0
"""
return self._compliant_series.median()
def skew(self) -> float | None:
"""Calculate the sample skewness of the Series.
Returns:
The sample skewness of the Series.
Examples:
>>> import polars as pl
>>> import narwhals as nw
>>>
>>> s_native = pl.Series([1, 1, 2, 10, 100])
>>> nw.from_native(s_native, series_only=True).skew()
1.4724267269058975
Notes:
The skewness is a measure of the asymmetry of the probability distribution.
A perfectly symmetric distribution has a skewness of 0.
"""
return self._compliant_series.skew()
def count(self) -> int:
"""Returns the number of non-null elements in the Series.
Returns:
The number of non-null elements in the Series.
Examples:
>>> import pyarrow as pa
>>> import narwhals as nw
>>>
>>> s_native = pa.chunked_array([[1, 2, None]])
>>> nw.from_native(s_native, series_only=True).count()
2
"""
return self._compliant_series.count()
def any(self) -> bool:
"""Return whether any of the values in the Series are True.
If there are no non-null elements, the result is `False`.
Notes:
Only works on Series of data type Boolean.
Returns:
A boolean indicating if any values in the Series are True.
Examples:
>>> import pandas as pd
>>> import narwhals as nw
>>>
>>> s_native = pd.Series([False, True, False])
>>> nw.from_native(s_native, series_only=True).any()
np.True_
"""
return self._compliant_series.any()
def all(self) -> bool:
"""Return whether all values in the Series are True.
If there are no non-null elements, the result is `True`.
Returns:
A boolean indicating if all values in the Series are True.
Examples:
>>> import pyarrow as pa
>>> import narwhals as nw
>>>
>>> s_native = pa.chunked_array([[False, True, False]])
>>> nw.from_native(s_native, series_only=True).all()
False
"""
return self._compliant_series.all()
def min(self) -> Any:
"""Get the minimal value in this Series.
Returns:
The minimum value in the Series.
Examples:
>>> import polars as pl
>>> import narwhals as nw
>>>
>>> s_native = pl.Series([1, 2, 3])
>>> nw.from_native(s_native, series_only=True).min()
1
"""
return self._compliant_series.min()
def max(self) -> Any:
"""Get the maximum value in this Series.
Returns:
The maximum value in the Series.
Examples:
>>> import pandas as pd
>>> import narwhals as nw
>>>
>>> s_native = pd.Series([1, 2, 3])
>>> nw.from_native(s_native, series_only=True).max()
np.int64(3)
"""
return self._compliant_series.max()
def arg_min(self) -> int:
"""Returns the index of the minimum value.
Examples:
>>> import pyarrow as pa
>>> import narwhals as nw
>>>
>>> s_native = pa.chunked_array([[1, 2, 3]])
>>> nw.from_native(s_native, series_only=True).arg_min()
0
"""
return self._compliant_series.arg_min()
def arg_max(self) -> int:
"""Returns the index of the maximum value.
Examples:
>>> import polars as pl
>>> import narwhals as nw
>>>
>>> s_native = pl.Series([1, 2, 3])
>>> nw.from_native(s_native, series_only=True).arg_max()
2
"""
return self._compliant_series.arg_max()
def sum(self) -> float:
"""Reduce this Series to the sum value.
If there are no non-null elements, the result is zero.
Returns:
The sum of all elements in the Series.
Examples:
>>> import pyarrow as pa
>>> import narwhals as nw
>>>
>>> s_native = pa.chunked_array([[1, 2, 3]])
>>> nw.from_native(s_native, series_only=True).sum()
6
"""
return self._compliant_series.sum()
def std(self, *, ddof: int = 1) -> float:
"""Get the standard deviation of this Series.
Arguments:
ddof: "Delta Degrees of Freedom": the divisor used in the calculation is N - ddof,
where N represents the number of elements.
Returns:
The standard deviation of all elements in the Series.
Examples:
>>> import polars as pl
>>> import narwhals as nw
>>>
>>> s_native = pl.Series([1, 2, 3])
>>> nw.from_native(s_native, series_only=True).std()
1.0
"""
return self._compliant_series.std(ddof=ddof)
def var(self, *, ddof: int = 1) -> float:
"""Get the variance of this Series.
Arguments:
ddof: "Delta Degrees of Freedom": the divisor used in the calculation is N - ddof,
where N represents the number of elements.
Examples:
>>> import pyarrow as pa
>>> import narwhals as nw
>>>
>>> s_native = pa.chunked_array([[1, 2, 3]])
>>> nw.from_native(s_native, series_only=True).var()
1.0
"""
return self._compliant_series.var(ddof=ddof)
def clip(
self,
lower_bound: Self | NumericLiteral | TemporalLiteral | None = None,
upper_bound: Self | NumericLiteral | TemporalLiteral | None = None,
) -> Self:
r"""Clip values in the Series.
Arguments:
lower_bound: Lower bound value.
upper_bound: Upper bound value.
Returns:
A new Series with values clipped to the specified bounds.
Examples:
>>> import pandas as pd
>>> import narwhals as nw
>>>
>>> s_native = pd.Series([-1, 1, -3, 3, -5, 5])
>>> nw.from_native(s_native, series_only=True).clip(-1, 3).to_native()
0 -1
1 1
2 -1
3 3
4 -1
5 3
dtype: int64
"""
return self._with_compliant(
self._compliant_series.clip(
lower_bound=self._extract_native(lower_bound),
upper_bound=self._extract_native(upper_bound),
)
)
def is_in(self, other: Any) -> Self:
"""Check if the elements of this Series are in the other sequence.
Arguments:
other: Sequence of primitive type.
Returns:
A new Series with boolean values indicating if the elements are in the other sequence.
Examples:
>>> import pyarrow as pa
>>> import narwhals as nw
>>>
>>> s_native = pa.chunked_array([[1, 2, 3]])
>>> s = nw.from_native(s_native, series_only=True)
>>> s.is_in([3, 2, 8]).to_native() # doctest: +ELLIPSIS
<pyarrow.lib.ChunkedArray object at ...>
[
[
false,
true,
true
]
]
"""
return self._with_compliant(
self._compliant_series.is_in(to_native(other, pass_through=True))
)
def arg_true(self) -> Self:
"""Find elements where boolean Series is True.
Returns:
A new Series with the indices of elements that are True.
Examples:
>>> import polars as pl
>>> import narwhals as nw
>>>
>>> s_native = pl.Series([1, None, None, 2])
>>> nw.from_native(
... s_native, series_only=True
... ).is_null().arg_true().to_native() # doctest: +NORMALIZE_WHITESPACE
shape: (2,)
Series: '' [u32]
[
1
2
]
"""
return self._with_compliant(self._compliant_series.arg_true())
def drop_nulls(self) -> Self:
"""Drop null values.
Notes:
pandas handles null values differently from Polars and PyArrow.
See [null_handling](../concepts/null_handling.md/)
for reference.
Returns:
A new Series with null values removed.
Examples:
>>> import pandas as pd
>>> import narwhals as nw
>>>
>>> s_native = pd.Series([2, 4, None, 3, 5])
>>> nw.from_native(s_native, series_only=True).drop_nulls().to_native()
0 2.0
1 4.0
3 3.0
4 5.0
dtype: float64
"""
return self._with_compliant(self._compliant_series.drop_nulls())
def abs(self) -> Self:
"""Calculate the absolute value of each element.
Returns:
A new Series with the absolute values of the original elements.
Examples:
>>> import pyarrow as pa
>>> import narwhals as nw
>>>
>>> s_native = pa.chunked_array([[2, -4, 3]])
>>> nw.from_native(
... s_native, series_only=True
... ).abs().to_native() # doctest: +ELLIPSIS
<pyarrow.lib.ChunkedArray object at ...>
[
[
2,
4,
3
]
]
"""
return self._with_compliant(self._compliant_series.abs())
def cum_sum(self, *, reverse: bool = False) -> Self:
"""Calculate the cumulative sum.
Arguments:
reverse: reverse the operation
Returns:
A new Series with the cumulative sum of non-null values.
Examples:
>>> import pandas as pd
>>> import narwhals as nw
>>>
>>> s_native = pd.Series([2, 4, 3])
>>> nw.from_native(s_native, series_only=True).cum_sum().to_native()
0 2
1 6
2 9
dtype: int64
"""
return self._with_compliant(self._compliant_series.cum_sum(reverse=reverse))
def unique(self, *, maintain_order: bool = False) -> Self:
"""Returns unique values of the series.
Arguments:
maintain_order: Keep the same order as the original series. This may be more
expensive to compute. Settings this to `True` blocks the possibility
to run on the streaming engine for Polars.
Returns:
A new Series with duplicate values removed.
Examples:
>>> import polars as pl
>>> import narwhals as nw
>>>
>>> s_native = pl.Series([2, 4, 4, 6])
>>> s = nw.from_native(s_native, series_only=True)
>>> s.unique(
... maintain_order=True
... ).to_native() # doctest: +NORMALIZE_WHITESPACE
shape: (3,)
Series: '' [i64]
[
2
4
6
]
"""
return self._with_compliant(
self._compliant_series.unique(maintain_order=maintain_order)
)
def diff(self) -> Self:
"""Calculate the difference with the previous element, for each element.
Notes:
pandas may change the dtype here, for example when introducing missing
values in an integer column. To ensure, that the dtype doesn't change,
you may want to use `fill_null` and `cast`. For example, to calculate
the diff and fill missing values with `0` in a Int64 column, you could
do:
s.diff().fill_null(0).cast(nw.Int64)
Returns:
A new Series with the difference between each element and its predecessor.
Examples:
>>> import pyarrow as pa
>>> import narwhals as nw
>>>
>>> s_native = pa.chunked_array([[2, 4, 3]])
>>> nw.from_native(
... s_native, series_only=True
... ).diff().to_native() # doctest: +ELLIPSIS
<pyarrow.lib.ChunkedArray object at ...>
[
[
null,
2,
-1
]
]
"""
return self._with_compliant(self._compliant_series.diff())
def shift(self, n: int) -> Self:
"""Shift values by `n` positions.
Arguments:
n: Number of indices to shift forward. If a negative value is passed,
values are shifted in the opposite direction instead.
Returns:
A new Series with values shifted by n positions.
Notes:
pandas may change the dtype here, for example when introducing missing
values in an integer column. To ensure, that the dtype doesn't change,
you may want to use `fill_null` and `cast`. For example, to shift
and fill missing values with `0` in a Int64 column, you could
do:
s.shift(1).fill_null(0).cast(nw.Int64)
Examples:
>>> import pandas as pd
>>> import narwhals as nw
>>>
>>> s_native = pd.Series([2, 4, 3])
>>> nw.from_native(s_native, series_only=True).shift(1).to_native()
0 NaN
1 2.0
2 4.0
dtype: float64
"""
ensure_type(n, int, param_name="n")
return self._with_compliant(self._compliant_series.shift(n))
def sample(
self,
n: int | None = None,
*,
fraction: float | None = None,
with_replacement: bool = False,
seed: int | None = None,
) -> Self:
"""Sample randomly from this Series.
Arguments:
n: Number of items to return. Cannot be used with fraction.
fraction: Fraction of items to return. Cannot be used with n.
with_replacement: Allow values to be sampled more than once.
seed: Seed for the random number generator. If set to None (default), a random
seed is generated for each sample operation.
Returns:
A new Series containing randomly sampled values from the original Series.
Notes:
The `sample` method returns a Series with a specified number of
randomly selected items chosen from this Series.
The results are not consistent across libraries.
Examples:
>>> import polars as pl
>>> import narwhals as nw
>>>
>>> s_native = pl.Series([1, 2, 3, 4])
>>> s = nw.from_native(s_native, series_only=True)
>>> s.sample(
... fraction=1.0, with_replacement=True
... ).to_native() # doctest: +SKIP
shape: (4,)
Series: '' [i64]
[
1
4
3
4
]
"""
return self._with_compliant(
self._compliant_series.sample(
n=n, fraction=fraction, with_replacement=with_replacement, seed=seed
)
)
def alias(self, name: str) -> Self:
"""Rename the Series.
Notes:
This method is very cheap, but does not guarantee that data
will be copied. For example:
```python
s1: nw.Series
s2 = s1.alias("foo")
arr = s2.to_numpy()
arr[0] = 999
```
may (depending on the backend, and on the version) result in
`s1`'s data being modified. We recommend:
- if you need to alias an object and don't need the original
one around any more, just use `alias` without worrying about it.
- if you were expecting `alias` to copy data, then explicitly call
`.clone` before calling `alias`.
Arguments:
name: The new name.
Returns:
A new Series with the updated name.
Examples:
>>> import pandas as pd
>>> import narwhals as nw
>>>
>>> s_native = pd.Series([1, 2, 3], name="foo")
>>> nw.from_native(s_native, series_only=True).alias("bar").to_native()
0 1
1 2
2 3
Name: bar, dtype: int64
"""
return self._with_compliant(self._compliant_series.alias(name=name))
def rename(self, name: str) -> Self:
"""Rename the Series.
Alias for `Series.alias()`.
Notes:
This method is very cheap, but does not guarantee that data
will be copied. For example:
```python
s1: nw.Series
s2 = s1.rename("foo")
arr = s2.to_numpy()
arr[0] = 999
```
may (depending on the backend, and on the version) result in
`s1`'s data being modified. We recommend:
- if you need to rename an object and don't need the original
one around any more, just use `rename` without worrying about it.
- if you were expecting `rename` to copy data, then explicitly call
`.clone` before calling `rename`.
Arguments:
name: The new name.
Returns:
A new Series with the updated name.
Examples:
>>> import polars as pl
>>> import narwhals as nw
>>>
>>> s_native = pl.Series("foo", [1, 2, 3])
>>> s = nw.from_native(s_native, series_only=True)
>>> s.rename("bar").to_native() # doctest: +NORMALIZE_WHITESPACE
shape: (3,)
Series: 'bar' [i64]
[
1
2
3
]
"""
return self.alias(name=name)
def replace_strict(
self,
old: Sequence[Any] | Mapping[Any, Any],
new: Sequence[Any] | None = None,
*,
return_dtype: IntoDType | None = None,
) -> Self:
"""Replace all values by different values.
This function must replace all non-null input values (else it raises an error).
Arguments:
old: Sequence of values to replace. It also accepts a mapping of values to
their replacement as syntactic sugar for
`replace_strict(old=list(mapping.keys()), new=list(mapping.values()))`.
new: Sequence of values to replace by. Length must match the length of `old`.
return_dtype: The data type of the resulting expression. If set to `None`
(default), the data type is determined automatically based on the other
inputs.
Returns:
A new Series with values replaced according to the mapping.
Examples:
>>> import pandas as pd
>>> import narwhals as nw
>>>
>>> s_native = pd.Series([3, 0, 1, 2], name="a")
>>> nw.from_native(s_native, series_only=True).replace_strict(
... [0, 1, 2, 3], ["zero", "one", "two", "three"], return_dtype=nw.String
... ).to_native()
0 three
1 zero
2 one
3 two
Name: a, dtype: object
"""
if new is None:
if not isinstance(old, Mapping):
msg = "`new` argument is required if `old` argument is not a Mapping type"
raise TypeError(msg)
new = list(old.values())
old = list(old.keys())
return self._with_compliant(
self._compliant_series.replace_strict(old, new, return_dtype=return_dtype)
)
def sort(self, *, descending: bool = False, nulls_last: bool = False) -> Self:
"""Sort this Series. Place null values first.
Arguments:
descending: Sort in descending order.
nulls_last: Place null values last instead of first.
Returns:
A new sorted Series.
Examples:
>>> import polars as pl
>>> import narwhals as nw
>>>
>>> s_native = pl.Series([5, None, 1, 2])
>>> s = nw.from_native(s_native, series_only=True)
>>> s.sort(descending=True).to_native() # doctest: +NORMALIZE_WHITESPACE
shape: (4,)
Series: '' [i64]
[
null
5
2
1
]
"""
return self._with_compliant(
self._compliant_series.sort(descending=descending, nulls_last=nulls_last)
)
def is_null(self) -> Self:
"""Returns a boolean Series indicating which values are null.
Notes:
pandas handles null values differently from Polars and PyArrow.
See [null_handling](../concepts/null_handling.md/)
for reference.
Returns:
A boolean Series indicating which values are null.
Examples:
>>> import pyarrow as pa
>>> import narwhals as nw
>>>
>>> s_native = pa.chunked_array([[1, 2, None]])
>>> nw.from_native(
... s_native, series_only=True
... ).is_null().to_native() # doctest:+ELLIPSIS
<pyarrow.lib.ChunkedArray object at ...>
[
[
false,
false,
true
]
]
"""
return self._with_compliant(self._compliant_series.is_null())
def is_nan(self) -> Self:
"""Returns a boolean Series indicating which values are NaN.
Returns:
A boolean Series indicating which values are NaN.
Notes:
pandas handles null values differently from Polars and PyArrow.
See [null_handling](../concepts/null_handling.md/)
for reference.
Examples:
>>> import pandas as pd
>>> import narwhals as nw
>>>
>>> s_native = pd.Series([0.0, None, 2.0], dtype="Float64")
>>> nw.from_native(s_native, series_only=True).is_nan().to_native()
0 False
1 <NA>
2 False
dtype: boolean
"""
return self._with_compliant(self._compliant_series.is_nan())
def fill_null(
self,
value: Self | NonNestedLiteral = None,
strategy: FillNullStrategy | None = None,
limit: int | None = None,
) -> Self:
"""Fill null values using the specified value.
Arguments:
value: Value used to fill null values.
strategy: Strategy used to fill null values.
limit: Number of consecutive null values to fill when using the 'forward' or 'backward' strategy.
Notes:
pandas handles null values differently from Polars and PyArrow.
See [null_handling](../concepts/null_handling.md/)
for reference.
Returns:
A new Series with null values filled according to the specified value or strategy.
Examples:
>>> import pandas as pd
>>> import narwhals as nw
>>>
>>> s_native = pd.Series([1, 2, None])
>>>
>>> nw.from_native(s_native, series_only=True).fill_null(5).to_native()
0 1.0
1 2.0
2 5.0
dtype: float64
Or using a strategy:
>>> nw.from_native(s_native, series_only=True).fill_null(
... strategy="forward", limit=1
... ).to_native()
0 1.0
1 2.0
2 2.0
dtype: float64
"""
if value is not None and strategy is not None:
msg = "cannot specify both `value` and `strategy`"
raise ValueError(msg)
if value is None and strategy is None:
msg = "must specify either a fill `value` or `strategy`"
raise ValueError(msg)
if strategy is not None and strategy not in {"forward", "backward"}:
msg = f"strategy not supported: {strategy}"
raise ValueError(msg)
return self._with_compliant(
self._compliant_series.fill_null(
value=self._extract_native(value), strategy=strategy, limit=limit
)
)
def is_between(
self,
lower_bound: Any | Self,
upper_bound: Any | Self,
closed: ClosedInterval = "both",
) -> Self:
"""Get a boolean mask of the values that are between the given lower/upper bounds.
Arguments:
lower_bound: Lower bound value.
upper_bound: Upper bound value.
closed: Define which sides of the interval are closed (inclusive).
Notes:
If the value of the `lower_bound` is greater than that of the `upper_bound`,
then the values will be False, as no value can satisfy the condition.
Returns:
A boolean Series indicating which values are between the given bounds.
Examples:
>>> import pyarrow as pa
>>> import narwhals as nw
>>>
>>> s_native = pa.chunked_array([[1, 2, 3, 4, 5]])
>>> s = nw.from_native(s_native, series_only=True)
>>> s.is_between(2, 4, "right").to_native() # doctest: +ELLIPSIS
<pyarrow.lib.ChunkedArray object at ...>
[
[
false,
false,
true,
true,
false
]
]
"""
return self._with_compliant(
self._compliant_series.is_between(
self._extract_native(lower_bound),
self._extract_native(upper_bound),
closed=closed,
)
)
def n_unique(self) -> int:
"""Count the number of unique values.
Returns:
Number of unique values in the Series.
Examples:
>>> import polars as pl
>>> import narwhals as nw
>>>
>>> s_native = pl.Series([1, 2, 2, 3])
>>> nw.from_native(s_native, series_only=True).n_unique()
3
"""
return self._compliant_series.n_unique()
def to_numpy(self) -> _1DArray:
"""Convert to numpy.
Returns:
NumPy ndarray representation of the Series.
Examples:
>>> import pandas as pd
>>> import narwhals as nw
>>>
>>> s_native = pd.Series([1, 2, 3], name="a")
>>> nw.from_native(s_native, series_only=True).to_numpy()
array([1, 2, 3]...)
"""
return self._compliant_series.to_numpy(None, copy=None)
def to_pandas(self) -> pd.Series[Any]:
"""Convert to pandas Series.
Returns:
A pandas Series containing the data from this Series.
Examples:
>>> import polars as pl
>>> import narwhals as nw
>>>
>>> s_native = pl.Series("a", [1, 2, 3])
>>> nw.from_native(s_native, series_only=True).to_pandas()
0 1
1 2
2 3
Name: a, dtype: int64
"""
return self._compliant_series.to_pandas()
def to_polars(self) -> pl.Series:
"""Convert to polars Series.
Returns:
A polars Series containing the data from this Series.
Examples:
>>> import pyarrow as pa
>>> import narwhals as nw
>>>
>>> s_native = pa.chunked_array([[1, 2, 3]])
>>> nw.from_native(
... s_native, series_only=True
... ).to_polars() # doctest: +NORMALIZE_WHITESPACE
shape: (3,)
Series: '' [i64]
[
1
2
3
]
"""
return self._compliant_series.to_polars()
def __add__(self, other: object) -> Self:
return self._with_compliant(
self._compliant_series.__add__(self._extract_native(other))
)
def __radd__(self, other: object) -> Self:
return self._with_compliant(
self._compliant_series.__radd__(self._extract_native(other))
)
def __sub__(self, other: object) -> Self:
return self._with_compliant(
self._compliant_series.__sub__(self._extract_native(other))
)
def __rsub__(self, other: object) -> Self:
return self._with_compliant(
self._compliant_series.__rsub__(self._extract_native(other))
)
def __mul__(self, other: object) -> Self:
return self._with_compliant(
self._compliant_series.__mul__(self._extract_native(other))
)
def __rmul__(self, other: object) -> Self:
return self._with_compliant(
self._compliant_series.__rmul__(self._extract_native(other))
)
def __truediv__(self, other: object) -> Self:
return self._with_compliant(
self._compliant_series.__truediv__(self._extract_native(other))
)
def __rtruediv__(self, other: object) -> Self:
return self._with_compliant(
self._compliant_series.__rtruediv__(self._extract_native(other))
)
def __floordiv__(self, other: object) -> Self:
return self._with_compliant(
self._compliant_series.__floordiv__(self._extract_native(other))
)
def __rfloordiv__(self, other: object) -> Self:
return self._with_compliant(
self._compliant_series.__rfloordiv__(self._extract_native(other))
)
def __pow__(self, other: object) -> Self:
return self._with_compliant(
self._compliant_series.__pow__(self._extract_native(other))
)
def __rpow__(self, other: object) -> Self:
return self._with_compliant(
self._compliant_series.__rpow__(self._extract_native(other))
)
def __mod__(self, other: object) -> Self:
return self._with_compliant(
self._compliant_series.__mod__(self._extract_native(other))
)
def __rmod__(self, other: object) -> Self:
return self._with_compliant(
self._compliant_series.__rmod__(self._extract_native(other))
)
def __eq__(self, other: object) -> Self: # type: ignore[override]
return self._with_compliant(
self._compliant_series.__eq__(self._extract_native(other))
)
def __ne__(self, other: object) -> Self: # type: ignore[override]
return self._with_compliant(
self._compliant_series.__ne__(self._extract_native(other))
)
def __gt__(self, other: Any) -> Self:
return self._with_compliant(
self._compliant_series.__gt__(self._extract_native(other))
)
def __ge__(self, other: Any) -> Self:
return self._with_compliant(
self._compliant_series.__ge__(self._extract_native(other))
)
def __lt__(self, other: Any) -> Self:
return self._with_compliant(
self._compliant_series.__lt__(self._extract_native(other))
)
def __le__(self, other: Any) -> Self:
return self._with_compliant(
self._compliant_series.__le__(self._extract_native(other))
)
def __and__(self, other: Any) -> Self:
return self._with_compliant(
self._compliant_series.__and__(self._extract_native(other))
)
def __rand__(self, other: Any) -> Self:
return self._with_compliant(
self._compliant_series.__rand__(self._extract_native(other))
)
def __or__(self, other: Any) -> Self:
return self._with_compliant(
self._compliant_series.__or__(self._extract_native(other))
)
def __ror__(self, other: Any) -> Self:
return self._with_compliant(
self._compliant_series.__ror__(self._extract_native(other))
)
# unary
def __invert__(self) -> Self:
return self._with_compliant(self._compliant_series.__invert__())
def filter(self, predicate: Any) -> Self:
"""Filter elements in the Series based on a condition.
Returns:
A new Series with elements that satisfy the condition.
Examples:
>>> import pandas as pd
>>> import narwhals as nw
>>>
>>> s_native = pd.Series([4, 10, 15, 34, 50])
>>> s_nw = nw.from_native(s_native, series_only=True)
>>> s_nw.filter(s_nw > 10).to_native()
2 15
3 34
4 50
dtype: int64
"""
return self._with_compliant(
self._compliant_series.filter(self._extract_native(predicate))
)
# --- descriptive ---
def is_duplicated(self) -> Self:
r"""Get a mask of all duplicated rows in the Series.
Returns:
A new Series with boolean values indicating duplicated rows.
Examples:
>>> import pyarrow as pa
>>> import narwhals as nw
>>>
>>> s_native = pa.chunked_array([[1, 2, 3, 1]])
>>> nw.from_native(
... s_native, series_only=True
... ).is_duplicated().to_native() # doctest: +ELLIPSIS
<pyarrow.lib.ChunkedArray object at ...>
[
[
true,
false,
false,
true
]
]
"""
return ~self.is_unique()
def is_empty(self) -> bool:
r"""Check if the series is empty.
Returns:
A boolean indicating if the series is empty.
Examples:
>>> import polars as pl
>>> import narwhals as nw
>>>
>>> s_native = pl.Series([1, 2, 3])
>>> s_nw = nw.from_native(s_native, series_only=True)
>>> s_nw.is_empty()
False
>>> s_nw.filter(s_nw > 10).is_empty()
True
"""
return self._compliant_series.len() == 0
def is_unique(self) -> Self:
r"""Get a mask of all unique rows in the Series.
Returns:
A new Series with boolean values indicating unique rows.
Examples:
>>> import pandas as pd
>>> import narwhals as nw
>>>
>>> s_native = pd.Series([1, 2, 3, 1])
>>> nw.from_native(s_native, series_only=True).is_unique().to_native()
0 False
1 True
2 True
3 False
dtype: bool
"""
return self._with_compliant(self._compliant_series.is_unique())
def null_count(self) -> int:
r"""Count the number of null values.
Notes:
pandas handles null values differently from Polars and PyArrow.
See [null_handling](../concepts/null_handling.md/)
for reference.
Returns:
The number of null values in the Series.
Examples:
>>> import pyarrow as pa
>>> import narwhals as nw
>>>
>>> s_native = pa.chunked_array([[1, None, None]])
>>> nw.from_native(s_native, series_only=True).null_count()
2
"""
return self._compliant_series.null_count()
def is_first_distinct(self) -> Self:
r"""Return a boolean mask indicating the first occurrence of each distinct value.
Returns:
A new Series with boolean values indicating the first occurrence of each distinct value.
Examples:
>>> import polars as pl
>>> import narwhals as nw
>>>
>>> s_native = pl.Series([1, 1, 2, 3, 2])
>>> nw.from_native(
... s_native, series_only=True
... ).is_first_distinct().to_native() # doctest: +NORMALIZE_WHITESPACE
shape: (5,)
Series: '' [bool]
[
true
false
true
true
false
]
"""
return self._with_compliant(self._compliant_series.is_first_distinct())
def is_last_distinct(self) -> Self:
r"""Return a boolean mask indicating the last occurrence of each distinct value.
Returns:
A new Series with boolean values indicating the last occurrence of each distinct value.
Examples:
>>> import pandas as pd
>>> import narwhals as nw
>>>
>>> s_native = pd.Series([1, 1, 2, 3, 2])
>>> nw.from_native(s_native, series_only=True).is_last_distinct().to_native()
0 False
1 True
2 False
3 True
4 True
dtype: bool
"""
return self._with_compliant(self._compliant_series.is_last_distinct())
def is_sorted(self, *, descending: bool = False) -> bool:
r"""Check if the Series is sorted.
Arguments:
descending: Check if the Series is sorted in descending order.
Returns:
A boolean indicating if the Series is sorted.
Examples:
>>> import pyarrow as pa
>>> import narwhals as nw
>>>
>>> s_native = pa.chunked_array([[3, 2, 1]])
>>> s_nw = nw.from_native(s_native, series_only=True)
>>> s_nw.is_sorted(descending=False)
False
>>> s_nw.is_sorted(descending=True)
True
"""
return self._compliant_series.is_sorted(descending=descending)
def value_counts(
self,
*,
sort: bool = False,
parallel: bool = False,
name: str | None = None,
normalize: bool = False,
) -> DataFrame[Any]:
r"""Count the occurrences of unique values.
Arguments:
sort: Sort the output by count in descending order. If set to False (default),
the order of the output is random.
parallel: Execute the computation in parallel. Used for Polars only.
name: Give the resulting count column a specific name; if `normalize` is True
defaults to "proportion", otherwise defaults to "count".
normalize: If true gives relative frequencies of the unique values
Returns:
A DataFrame with two columns
- The original values as first column
- Either count or proportion as second column, depending on normalize parameter.
Examples:
>>> import pandas as pd
>>> import narwhals as nw
>>>
>>> s_native = pd.Series([1, 1, 2, 3, 2], name="s")
>>> nw.from_native(s_native, series_only=True).value_counts(
... sort=True
... ).to_native()
s count
0 1 2
1 2 2
2 3 1
"""
return self._dataframe(
self._compliant_series.value_counts(
sort=sort, parallel=parallel, name=name, normalize=normalize
),
level=self._level,
)
def quantile(
self, quantile: float, interpolation: RollingInterpolationMethod
) -> float:
"""Get quantile value of the series.
Note:
pandas and Polars may have implementation differences for a given interpolation method.
Arguments:
quantile: Quantile between 0.0 and 1.0.
interpolation: Interpolation method.
Returns:
The quantile value.
Examples:
>>> import polars as pl
>>> import narwhals as nw
>>>
>>> s_native = pl.Series(list(range(50)))
>>> s_nw = nw.from_native(s_native, series_only=True)
>>> [
... s_nw.quantile(quantile=q, interpolation="nearest")
... for q in (0.1, 0.25, 0.5, 0.75, 0.9)
... ]
[5.0, 12.0, 25.0, 37.0, 44.0]
"""
return self._compliant_series.quantile(
quantile=quantile, interpolation=interpolation
)
def zip_with(self, mask: Self, other: Self) -> Self:
"""Take values from self or other based on the given mask.
Where mask evaluates true, take values from self. Where mask evaluates false,
take values from other.
Arguments:
mask: Boolean Series
other: Series of same type.
Returns:
A new Series with values selected from self or other based on the mask.
Examples:
>>> import pyarrow as pa
>>> import narwhals as nw
>>> data_native = pa.chunked_array([[1, 2, 3, 4, 5]])
>>> other_native = pa.chunked_array([[5, 4, 3, 2, 1]])
>>> mask_native = pa.chunked_array([[True, False, True, False, True]])
>>>
>>> data_nw = nw.from_native(data_native, series_only=True)
>>> other_nw = nw.from_native(other_native, series_only=True)
>>> mask_nw = nw.from_native(mask_native, series_only=True)
>>>
>>> data_nw.zip_with(mask_nw, other_nw).to_native() # doctest: +ELLIPSIS
<pyarrow.lib.ChunkedArray object at ...>
[
[
1,
4,
3,
2,
5
]
]
"""
return self._with_compliant(
self._compliant_series.zip_with(
self._extract_native(mask), self._extract_native(other)
)
)
def item(self, index: int | None = None) -> Any:
r"""Return the Series as a scalar, or return the element at the given index.
If no index is provided, this is equivalent to `s[0]`, with a check
that the shape is (1,). With an index, this is equivalent to `s[index]`.
Returns:
The scalar value of the Series or the element at the given index.
Examples:
>>> import polars as pl
>>> import narwhals as nw
>>>
>>> nw.from_native(pl.Series("a", [1]), series_only=True).item()
1
>>> nw.from_native(pl.Series("a", [9, 8, 7]), series_only=True).item(-1)
7
"""
return self._compliant_series.item(index=index)
def head(self, n: int = 10) -> Self:
r"""Get the first `n` rows.
Arguments:
n: Number of rows to return.
Returns:
A new Series containing the first n rows.
Examples:
>>> import pandas as pd
>>> import narwhals as nw
>>>
>>> s_native = pd.Series(list(range(10)))
>>> nw.from_native(s_native, series_only=True).head(3).to_native()
0 0
1 1
2 2
dtype: int64
"""
return self._with_compliant(self._compliant_series.head(n))
def tail(self, n: int = 10) -> Self:
r"""Get the last `n` rows.
Arguments:
n: Number of rows to return.
Returns:
A new Series with the last n rows.
Examples:
>>> import pyarrow as pa
>>> import narwhals as nw
>>>
>>> s_native = pa.chunked_array([list(range(10))])
>>> s = nw.from_native(s_native, series_only=True)
>>> s.tail(3).to_native() # doctest: +ELLIPSIS
<pyarrow.lib.ChunkedArray object at ...>
[
[
7,
8,
9
]
]
"""
return self._with_compliant(self._compliant_series.tail(n))
def round(self, decimals: int = 0) -> Self:
r"""Round underlying floating point data by `decimals` digits.
Arguments:
decimals: Number of decimals to round by.
Returns:
A new Series with rounded values.
Notes:
For values exactly halfway between rounded decimal values pandas behaves differently than Polars and Arrow.
pandas rounds to the nearest even value (e.g. -0.5 and 0.5 round to 0.0, 1.5 and 2.5 round to 2.0, 3.5 and
4.5 to 4.0, etc..).
Polars and Arrow round away from 0 (e.g. -0.5 to -1.0, 0.5 to 1.0, 1.5 to 2.0, 2.5 to 3.0, etc..).
Examples:
>>> import polars as pl
>>> import narwhals as nw
>>>
>>> s_native = pl.Series([1.12345, 2.56789, 3.901234])
>>> s = nw.from_native(s_native, series_only=True)
>>> s.round(1).to_native() # doctest: +NORMALIZE_WHITESPACE
shape: (3,)
Series: '' [f64]
[
1.1
2.6
3.9
]
"""
return self._with_compliant(self._compliant_series.round(decimals))
def to_dummies(
self, *, separator: str = "_", drop_first: bool = False
) -> DataFrame[Any]:
r"""Get dummy/indicator variables.
Arguments:
separator: Separator/delimiter used when generating column names.
drop_first: Remove the first category from the variable being encoded.
Returns:
A new DataFrame containing the dummy/indicator variables.
Notes:
pandas and Polars handle null values differently. Polars distinguishes
between NaN and Null, whereas pandas doesn't.
Examples:
>>> import pandas as pd
>>> import narwhals as nw
>>>
>>> s_native = pd.Series([1, 2, 3], name="a")
>>> s_nw = nw.from_native(s_native, series_only=True)
>>> s_nw.to_dummies(drop_first=False).to_native()
a_1 a_2 a_3
0 1 0 0
1 0 1 0
2 0 0 1
>>> s_nw.to_dummies(drop_first=True).to_native()
a_2 a_3
0 0 0
1 1 0
2 0 1
"""
return self._dataframe(
self._compliant_series.to_dummies(separator=separator, drop_first=drop_first),
level=self._level,
)
def gather_every(self, n: int, offset: int = 0) -> Self:
r"""Take every nth value in the Series and return as new Series.
Arguments:
n: Gather every *n*-th row.
offset: Starting index.
Returns:
A new Series with every nth value starting from the offset.
Examples:
>>> import pyarrow as pa
>>> import narwhals as nw
>>>
>>> s_native = pa.chunked_array([[1, 2, 3, 4]])
>>> nw.from_native(s_native, series_only=True).gather_every(
... n=2, offset=1
... ).to_native() # doctest:+ELLIPSIS
<pyarrow.lib.ChunkedArray object at ...>
[
[
2,
4
]
]
"""
return self._with_compliant(
self._compliant_series.gather_every(n=n, offset=offset)
)
def to_arrow(self) -> pa.Array[Any]:
r"""Convert to arrow.
Returns:
A PyArrow Array containing the data from the Series.
Examples:
>>> import polars as pl
>>> import narwhals as nw
>>>
>>> s_native = pl.Series([1, 2, 3, 4])
>>> nw.from_native(
... s_native, series_only=True
... ).to_arrow() # doctest:+NORMALIZE_WHITESPACE
<pyarrow.lib.Int64Array object at ...>
[
1,
2,
3,
4
]
"""
return self._compliant_series.to_arrow()
def mode(self) -> Self:
r"""Compute the most occurring value(s).
Can return multiple values.
Returns:
A new Series containing the mode(s) (values that appear most frequently).
Examples:
>>> import pandas as pd
>>> import narwhals as nw
>>> s_native = pd.Series([1, 1, 2, 2, 3])
>>> nw.from_native(s_native, series_only=True).mode().sort().to_native()
0 1
1 2
dtype: int64
"""
return self._with_compliant(self._compliant_series.mode())
def is_finite(self) -> Self:
"""Returns a boolean Series indicating which values are finite.
Warning:
Different backend handle null values differently. `is_finite` will return
False for NaN and Null's in the Dask and pandas non-nullable backend, while
for Polars, PyArrow and pandas nullable backends null values are kept as such.
Returns:
Expression of `Boolean` data type.
Examples:
>>> import pyarrow as pa
>>> import narwhals as nw
>>>
>>> s_native = pa.chunked_array([[float("nan"), float("inf"), 2.0, None]])
>>> nw.from_native(
... s_native, series_only=True
... ).is_finite().to_native() # doctest: +ELLIPSIS
<pyarrow.lib.ChunkedArray object at ...>
[
[
false,
false,
true,
null
]
]
"""
return self._with_compliant(self._compliant_series.is_finite())
def cum_count(self, *, reverse: bool = False) -> Self:
r"""Return the cumulative count of the non-null values in the series.
Arguments:
reverse: reverse the operation
Returns:
A new Series with the cumulative count of non-null values.
Examples:
>>> import polars as pl
>>> import narwhals as nw
>>>
>>> s_native = pl.Series(["x", "k", None, "d"])
>>> nw.from_native(s_native, series_only=True).cum_count(
... reverse=True
... ).to_native() # doctest:+NORMALIZE_WHITESPACE
shape: (4,)
Series: '' [u32]
[
3
2
1
1
]
"""
return self._with_compliant(self._compliant_series.cum_count(reverse=reverse))
def cum_min(self, *, reverse: bool = False) -> Self:
r"""Return the cumulative min of the non-null values in the series.
Arguments:
reverse: reverse the operation
Returns:
A new Series with the cumulative min of non-null values.
Examples:
>>> import pandas as pd
>>> import narwhals as nw
>>>
>>> s_native = pd.Series([3, 1, None, 2])
>>> nw.from_native(s_native, series_only=True).cum_min().to_native()
0 3.0
1 1.0
2 NaN
3 1.0
dtype: float64
"""
return self._with_compliant(self._compliant_series.cum_min(reverse=reverse))
def cum_max(self, *, reverse: bool = False) -> Self:
r"""Return the cumulative max of the non-null values in the series.
Arguments:
reverse: reverse the operation
Returns:
A new Series with the cumulative max of non-null values.
Examples:
>>> import pyarrow as pa
>>> import narwhals as nw
>>>
>>> s_native = pa.chunked_array([[1, 3, None, 2]])
>>> nw.from_native(
... s_native, series_only=True
... ).cum_max().to_native() # doctest:+ELLIPSIS
<pyarrow.lib.ChunkedArray object at ...>
[
[
1,
3,
null,
3
]
]
"""
return self._with_compliant(self._compliant_series.cum_max(reverse=reverse))
def cum_prod(self, *, reverse: bool = False) -> Self:
r"""Return the cumulative product of the non-null values in the series.
Arguments:
reverse: reverse the operation
Returns:
A new Series with the cumulative product of non-null values.
Examples:
>>> import polars as pl
>>> import narwhals as nw
>>>
>>> s_native = pl.Series([1, 3, None, 2])
>>> nw.from_native(
... s_native, series_only=True
... ).cum_prod().to_native() # doctest:+NORMALIZE_WHITESPACE
shape: (4,)
Series: '' [i64]
[
1
3
null
6
]
"""
return self._with_compliant(self._compliant_series.cum_prod(reverse=reverse))
def rolling_sum(
self, window_size: int, *, min_samples: int | None = None, center: bool = False
) -> Self:
"""Apply a rolling sum (moving sum) over the values.
A window of length `window_size` will traverse the values. The resulting values
will be aggregated to their sum.
The window at a given row will include the row itself and the `window_size - 1`
elements before it.
Arguments:
window_size: The length of the window in number of elements. It must be a
strictly positive integer.
min_samples: The number of values in the window that should be non-null before
computing a result. If set to `None` (default), it will be set equal to
`window_size`. If provided, it must be a strictly positive integer, and
less than or equal to `window_size`
center: Set the labels at the center of the window.
Returns:
A new series.
Examples:
>>> import pandas as pd
>>> import narwhals as nw
>>>
>>> s_native = pd.Series([1.0, 2.0, 3.0, 4.0])
>>> nw.from_native(s_native, series_only=True).rolling_sum(
... window_size=2
... ).to_native()
0 NaN
1 3.0
2 5.0
3 7.0
dtype: float64
"""
window_size, min_samples_int = _validate_rolling_arguments(
window_size=window_size, min_samples=min_samples
)
if len(self) == 0: # pragma: no cover
return self
return self._with_compliant(
self._compliant_series.rolling_sum(
window_size=window_size, min_samples=min_samples_int, center=center
)
)
def rolling_mean(
self, window_size: int, *, min_samples: int | None = None, center: bool = False
) -> Self:
"""Apply a rolling mean (moving mean) over the values.
A window of length `window_size` will traverse the values. The resulting values
will be aggregated to their mean.
The window at a given row will include the row itself and the `window_size - 1`
elements before it.
Arguments:
window_size: The length of the window in number of elements. It must be a
strictly positive integer.
min_samples: The number of values in the window that should be non-null before
computing a result. If set to `None` (default), it will be set equal to
`window_size`. If provided, it must be a strictly positive integer, and
less than or equal to `window_size`
center: Set the labels at the center of the window.
Returns:
A new series.
Examples:
>>> import pyarrow as pa
>>> import narwhals as nw
>>>
>>> s_native = pa.chunked_array([[1.0, 2.0, 3.0, 4.0]])
>>> nw.from_native(s_native, series_only=True).rolling_mean(
... window_size=2
... ).to_native() # doctest:+ELLIPSIS
<pyarrow.lib.ChunkedArray object at ...>
[
[
null,
1.5,
2.5,
3.5
]
]
"""
window_size, min_samples = _validate_rolling_arguments(
window_size=window_size, min_samples=min_samples
)
if len(self) == 0: # pragma: no cover
return self
return self._with_compliant(
self._compliant_series.rolling_mean(
window_size=window_size, min_samples=min_samples, center=center
)
)
def rolling_var(
self,
window_size: int,
*,
min_samples: int | None = None,
center: bool = False,
ddof: int = 1,
) -> Self:
"""Apply a rolling variance (moving variance) over the values.
A window of length `window_size` will traverse the values. The resulting values
will be aggregated to their variance.
The window at a given row will include the row itself and the `window_size - 1`
elements before it.
Arguments:
window_size: The length of the window in number of elements. It must be a
strictly positive integer.
min_samples: The number of values in the window that should be non-null before
computing a result. If set to `None` (default), it will be set equal to
`window_size`. If provided, it must be a strictly positive integer, and
less than or equal to `window_size`.
center: Set the labels at the center of the window.
ddof: Delta Degrees of Freedom; the divisor for a length N window is N - ddof.
Returns:
A new series.
Examples:
>>> import polars as pl
>>> import narwhals as nw
>>>
>>> s_native = pl.Series([1.0, 3.0, 1.0, 4.0])
>>> nw.from_native(s_native, series_only=True).rolling_var(
... window_size=2, min_samples=1
... ).to_native() # doctest:+NORMALIZE_WHITESPACE
shape: (4,)
Series: '' [f64]
[
null
2.0
2.0
4.5
]
"""
window_size, min_samples = _validate_rolling_arguments(
window_size=window_size, min_samples=min_samples
)
if len(self) == 0: # pragma: no cover
return self
return self._with_compliant(
self._compliant_series.rolling_var(
window_size=window_size, min_samples=min_samples, center=center, ddof=ddof
)
)
def rolling_std(
self,
window_size: int,
*,
min_samples: int | None = None,
center: bool = False,
ddof: int = 1,
) -> Self:
"""Apply a rolling standard deviation (moving standard deviation) over the values.
A window of length `window_size` will traverse the values. The resulting values
will be aggregated to their standard deviation.
The window at a given row will include the row itself and the `window_size - 1`
elements before it.
Arguments:
window_size: The length of the window in number of elements. It must be a
strictly positive integer.
min_samples: The number of values in the window that should be non-null before
computing a result. If set to `None` (default), it will be set equal to
`window_size`. If provided, it must be a strictly positive integer, and
less than or equal to `window_size`.
center: Set the labels at the center of the window.
ddof: Delta Degrees of Freedom; the divisor for a length N window is N - ddof.
Returns:
A new series.
Examples:
>>> import pandas as pd
>>> import narwhals as nw
>>>
>>> s_native = pd.Series([1.0, 3.0, 1.0, 4.0])
>>> nw.from_native(s_native, series_only=True).rolling_std(
... window_size=2, min_samples=1
... ).to_native()
0 NaN
1 1.414214
2 1.414214
3 2.121320
dtype: float64
"""
window_size, min_samples = _validate_rolling_arguments(
window_size=window_size, min_samples=min_samples
)
if len(self) == 0: # pragma: no cover
return self
return self._with_compliant(
self._compliant_series.rolling_std(
window_size=window_size, min_samples=min_samples, center=center, ddof=ddof
)
)
def __iter__(self) -> Iterator[Any]:
yield from self._compliant_series.__iter__()
def __contains__(self, other: Any) -> bool:
return self._compliant_series.__contains__(other)
def rank(self, method: RankMethod = "average", *, descending: bool = False) -> Self:
"""Assign ranks to data, dealing with ties appropriately.
Notes:
The resulting dtype may differ between backends.
Arguments:
method: The method used to assign ranks to tied elements.
The following methods are available (default is 'average')
- *"average"*: The average of the ranks that would have been assigned to
all the tied values is assigned to each value.
- *"min"*: The minimum of the ranks that would have been assigned to all
the tied values is assigned to each value. (This is also referred to
as "competition" ranking.)
- *"max"*: The maximum of the ranks that would have been assigned to all
the tied values is assigned to each value.
- *"dense"*: Like "min", but the rank of the next highest element is
assigned the rank immediately after those assigned to the tied elements.
- *"ordinal"*: All values are given a distinct rank, corresponding to the
order that the values occur in the Series.
descending: Rank in descending order.
Returns:
A new series with rank data as values.
Examples:
>>> import pyarrow as pa
>>> import narwhals as nw
>>>
>>> s_native = pa.chunked_array([[3, 6, 1, 1, 6]])
>>> nw.from_native(s_native, series_only=True).rank(
... method="dense"
... ).to_native() # doctest:+ELLIPSIS
<pyarrow.lib.ChunkedArray object at ...>
[
[
2,
3,
1,
1,
3
]
]
"""
supported_rank_methods = {"average", "min", "max", "dense", "ordinal"}
if method not in supported_rank_methods:
msg = (
"Ranking method must be one of {'average', 'min', 'max', 'dense', 'ordinal'}. "
f"Found '{method}'"
)
raise ValueError(msg)
return self._with_compliant(
self._compliant_series.rank(method=method, descending=descending)
)
def hist(
self,
bins: list[float | int] | None = None,
*,
bin_count: int | None = None,
include_breakpoint: bool = True,
) -> DataFrame[Any]:
"""Bin values into buckets and count their occurrences.
Warning:
This functionality is considered **unstable**. It may be changed at any point
without it being considered a breaking change.
Arguments:
bins: A monotonically increasing sequence of values.
bin_count: If no bins provided, this will be used to determine the distance of the bins.
include_breakpoint: Include a column that shows the intervals as categories.
Returns:
A new DataFrame containing the counts of values that occur within each passed bin.
Examples:
>>> import pandas as pd
>>> import narwhals as nw
>>> s_native = pd.Series([1, 3, 8, 8, 2, 1, 3], name="a")
>>> nw.from_native(s_native, series_only=True).hist(bin_count=4)
┌────────────────────┐
| Narwhals DataFrame |
|--------------------|
| breakpoint count|
|0 2.75 3|
|1 4.50 2|
|2 6.25 0|
|3 8.00 2|
└────────────────────┘
"""
if bins is not None and bin_count is not None:
msg = "can only provide one of `bin_count` or `bins`"
raise ComputeError(msg)
if bins is None and bin_count is None:
bin_count = 10 # polars (v1.20) sets bin=10 if neither are provided.
if bins is not None:
for i in range(1, len(bins)):
if bins[i - 1] >= bins[i]:
msg = "bins must increase monotonically"
raise ComputeError(msg)
return self._dataframe(
self._compliant_series.hist(
bins=bins, bin_count=bin_count, include_breakpoint=include_breakpoint
),
level=self._level,
)
def log(self, base: float = math.e) -> Self:
r"""Compute the logarithm to a given base.
Arguments:
base: Given base, defaults to `e`
Returns:
A new series.
Examples:
>>> import pandas as pd
>>> import narwhals as nw
>>> s_native = pd.Series([1, 2, 4], name="a")
>>> s = nw.from_native(s_native, series_only=True)
>>> s.log(base=2)
┌───────────────────────┐
| Narwhals Series |
|-----------------------|
|0 0.0 |
|1 1.0 |
|2 2.0 |
|Name: a, dtype: float64|
└───────────────────────┘
"""
return self._with_compliant(self._compliant_series.log(base=base))
def exp(self) -> Self:
r"""Compute the exponent.
Returns:
A new series.
Examples:
>>> import pandas as pd
>>> import narwhals as nw
>>> s_native = pd.Series([-1, 0, 1], name="a")
>>> s = nw.from_native(s_native, series_only=True)
>>> s.exp()
┌───────────────────────┐
| Narwhals Series |
|-----------------------|
|0 0.367879 |
|1 1.000000 |
|2 2.718282 |
|Name: a, dtype: float64|
└───────────────────────┘
"""
return self._with_compliant(self._compliant_series.exp())
@property
def str(self) -> SeriesStringNamespace[Self]:
return SeriesStringNamespace(self)
@property
def dt(self) -> SeriesDateTimeNamespace[Self]:
return SeriesDateTimeNamespace(self)
@property
def cat(self) -> SeriesCatNamespace[Self]:
return SeriesCatNamespace(self)
@property
def list(self) -> SeriesListNamespace[Self]:
return SeriesListNamespace(self)
@property
def struct(self) -> SeriesStructNamespace[Self]:
return SeriesStructNamespace(self)
|