polars-df 0.21.0 → 0.21.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +16 -0
- data/Cargo.lock +1 -1
- data/ext/polars/Cargo.toml +7 -1
- data/ext/polars/src/conversion/mod.rs +92 -4
- data/ext/polars/src/exceptions.rs +1 -0
- data/ext/polars/src/expr/array.rs +73 -4
- data/ext/polars/src/expr/binary.rs +26 -1
- data/ext/polars/src/expr/bitwise.rs +39 -0
- data/ext/polars/src/expr/categorical.rs +20 -0
- data/ext/polars/src/expr/datatype.rs +24 -1
- data/ext/polars/src/expr/datetime.rs +58 -0
- data/ext/polars/src/expr/general.rs +84 -5
- data/ext/polars/src/expr/list.rs +24 -0
- data/ext/polars/src/expr/meta.rs +11 -0
- data/ext/polars/src/expr/mod.rs +1 -0
- data/ext/polars/src/expr/name.rs +8 -0
- data/ext/polars/src/expr/rolling.rs +20 -0
- data/ext/polars/src/expr/string.rs +59 -0
- data/ext/polars/src/expr/struct.rs +9 -1
- data/ext/polars/src/functions/io.rs +19 -0
- data/ext/polars/src/functions/lazy.rs +4 -0
- data/ext/polars/src/lazyframe/general.rs +51 -0
- data/ext/polars/src/lib.rs +119 -10
- data/ext/polars/src/map/dataframe.rs +2 -2
- data/ext/polars/src/map/series.rs +1 -1
- data/ext/polars/src/series/aggregation.rs +44 -0
- data/ext/polars/src/series/general.rs +64 -4
- data/lib/polars/array_expr.rb +382 -3
- data/lib/polars/array_name_space.rb +281 -0
- data/lib/polars/binary_expr.rb +67 -0
- data/lib/polars/binary_name_space.rb +43 -0
- data/lib/polars/cat_expr.rb +224 -0
- data/lib/polars/cat_name_space.rb +138 -0
- data/lib/polars/config.rb +2 -2
- data/lib/polars/convert.rb +6 -6
- data/lib/polars/data_frame.rb +684 -19
- data/lib/polars/data_type_expr.rb +52 -0
- data/lib/polars/data_types.rb +14 -2
- data/lib/polars/date_time_expr.rb +251 -0
- data/lib/polars/date_time_name_space.rb +299 -0
- data/lib/polars/expr.rb +1213 -180
- data/lib/polars/functions/datatype.rb +21 -0
- data/lib/polars/functions/lazy.rb +13 -0
- data/lib/polars/io/csv.rb +1 -1
- data/lib/polars/io/json.rb +4 -4
- data/lib/polars/io/ndjson.rb +4 -4
- data/lib/polars/io/parquet.rb +27 -5
- data/lib/polars/lazy_frame.rb +936 -20
- data/lib/polars/list_expr.rb +196 -4
- data/lib/polars/list_name_space.rb +201 -4
- data/lib/polars/meta_expr.rb +64 -0
- data/lib/polars/name_expr.rb +36 -0
- data/lib/polars/schema.rb +79 -3
- data/lib/polars/selector.rb +72 -0
- data/lib/polars/selectors.rb +3 -3
- data/lib/polars/series.rb +1051 -54
- data/lib/polars/string_expr.rb +411 -6
- data/lib/polars/string_name_space.rb +722 -49
- data/lib/polars/struct_expr.rb +103 -0
- data/lib/polars/struct_name_space.rb +19 -1
- data/lib/polars/utils/various.rb +18 -1
- data/lib/polars/utils.rb +5 -1
- data/lib/polars/version.rb +1 -1
- data/lib/polars.rb +2 -0
- metadata +4 -1
data/lib/polars/series.rb
CHANGED
@@ -287,7 +287,7 @@ module Polars
|
|
287
287
|
self != other
|
288
288
|
end
|
289
289
|
|
290
|
-
# Method equivalent of equality operator `series != other` where `
|
290
|
+
# Method equivalent of equality operator `series != other` where `nil == nil`.
|
291
291
|
#
|
292
292
|
# This differs from the standard `ne` where null values are propagated.
|
293
293
|
#
|
@@ -407,7 +407,7 @@ module Polars
|
|
407
407
|
# @return [Series]
|
408
408
|
def !
|
409
409
|
if dtype == Boolean
|
410
|
-
return Utils.wrap_s(_s.
|
410
|
+
return Utils.wrap_s(_s.not_)
|
411
411
|
end
|
412
412
|
raise NotImplementedError
|
413
413
|
end
|
@@ -447,7 +447,7 @@ module Polars
|
|
447
447
|
item = len + item
|
448
448
|
end
|
449
449
|
|
450
|
-
return _s.
|
450
|
+
return _s.get_index(item)
|
451
451
|
end
|
452
452
|
|
453
453
|
if item.is_a?(Range)
|
@@ -496,6 +496,37 @@ module Polars
|
|
496
496
|
end
|
497
497
|
end
|
498
498
|
|
499
|
+
# Return the Series as a scalar, or return the element at the given index.
|
500
|
+
#
|
501
|
+
# If no index is provided, this is equivalent to `s[0]`, with a check
|
502
|
+
# that the shape is (1,). With an index, this is equivalent to `s[index]`.
|
503
|
+
#
|
504
|
+
# @return [Object]
|
505
|
+
#
|
506
|
+
# @example
|
507
|
+
# s1 = Polars::Series.new("a", [1])
|
508
|
+
# s1.item
|
509
|
+
# # => 1
|
510
|
+
#
|
511
|
+
# @example
|
512
|
+
# s2 = Polars::Series.new("a", [9, 8, 7])
|
513
|
+
# s2.cum_sum.item(-1)
|
514
|
+
# # => 24
|
515
|
+
def item(index = nil)
|
516
|
+
if index.nil?
|
517
|
+
if len != 1
|
518
|
+
msg = (
|
519
|
+
"can only call '.item' if the Series is of length 1," +
|
520
|
+
" or an explicit index is provided (Series is of length #{len})"
|
521
|
+
)
|
522
|
+
raise ArgumentError, msg
|
523
|
+
end
|
524
|
+
return _s.get_index(0)
|
525
|
+
end
|
526
|
+
|
527
|
+
_s.get_index_signed(index)
|
528
|
+
end
|
529
|
+
|
499
530
|
# Return an estimation of the total (heap) allocated size of the Series.
|
500
531
|
#
|
501
532
|
# Estimated size is given in the specified unit (bytes by default).
|
@@ -543,7 +574,26 @@ module Polars
|
|
543
574
|
# # 1.732051
|
544
575
|
# # ]
|
545
576
|
def sqrt
|
546
|
-
|
577
|
+
super
|
578
|
+
end
|
579
|
+
|
580
|
+
# Compute the cube root of the elements.
|
581
|
+
#
|
582
|
+
# @return [Series]
|
583
|
+
#
|
584
|
+
# @example
|
585
|
+
# s = Polars::Series.new([1, 2, 3])
|
586
|
+
# s.cbrt
|
587
|
+
# # =>
|
588
|
+
# # shape: (3,)
|
589
|
+
# # Series: '' [f64]
|
590
|
+
# # [
|
591
|
+
# # 1.0
|
592
|
+
# # 1.259921
|
593
|
+
# # 1.44225
|
594
|
+
# # ]
|
595
|
+
def cbrt
|
596
|
+
super
|
547
597
|
end
|
548
598
|
|
549
599
|
# Check if any boolean value in the column is `true`.
|
@@ -640,6 +690,25 @@ module Polars
|
|
640
690
|
super
|
641
691
|
end
|
642
692
|
|
693
|
+
# Compute the natural logarithm of the input array plus one, element-wise.
|
694
|
+
#
|
695
|
+
# @return [Series]
|
696
|
+
#
|
697
|
+
# @example
|
698
|
+
# s = Polars::Series.new([1, 2, 3])
|
699
|
+
# s.log1p
|
700
|
+
# # =>
|
701
|
+
# # shape: (3,)
|
702
|
+
# # Series: '' [f64]
|
703
|
+
# # [
|
704
|
+
# # 0.693147
|
705
|
+
# # 1.098612
|
706
|
+
# # 1.386294
|
707
|
+
# # ]
|
708
|
+
def log1p
|
709
|
+
super
|
710
|
+
end
|
711
|
+
|
643
712
|
# Compute the base 10 logarithm of the input array, element-wise.
|
644
713
|
#
|
645
714
|
# @return [Series]
|
@@ -875,6 +944,44 @@ module Polars
|
|
875
944
|
to_frame.select(Polars.col(name).product).to_series[0]
|
876
945
|
end
|
877
946
|
|
947
|
+
# Raise to the power of the given exponent.
|
948
|
+
#
|
949
|
+
# If the exponent is float, the result follows the dtype of exponent.
|
950
|
+
# Otherwise, it follows dtype of base.
|
951
|
+
#
|
952
|
+
# @param exponent [Numeric]
|
953
|
+
# The exponent. Accepts Series input.
|
954
|
+
#
|
955
|
+
# @return [Series]
|
956
|
+
#
|
957
|
+
# @example Raising integers to positive integers results in integers:
|
958
|
+
# s = Polars::Series.new("foo", [1, 2, 3, 4])
|
959
|
+
# s.pow(3)
|
960
|
+
# # =>
|
961
|
+
# # shape: (4,)
|
962
|
+
# # Series: 'foo' [i64]
|
963
|
+
# # [
|
964
|
+
# # 1
|
965
|
+
# # 8
|
966
|
+
# # 27
|
967
|
+
# # 64
|
968
|
+
# # ]
|
969
|
+
#
|
970
|
+
# @example In order to raise integers to negative integers, you can cast either the base or the exponent to float:
|
971
|
+
# s.pow(-3.0)
|
972
|
+
# # =>
|
973
|
+
# # shape: (4,)
|
974
|
+
# # Series: 'foo' [f64]
|
975
|
+
# # [
|
976
|
+
# # 1.0
|
977
|
+
# # 0.125
|
978
|
+
# # 0.037037
|
979
|
+
# # 0.015625
|
980
|
+
# # ]
|
981
|
+
def pow(exponent)
|
982
|
+
to_frame.select_seq(F.col(name).pow(exponent)).to_series
|
983
|
+
end
|
984
|
+
|
878
985
|
# Get the minimal value in this Series.
|
879
986
|
#
|
880
987
|
# @return [Object]
|
@@ -1100,8 +1207,8 @@ module Polars
|
|
1100
1207
|
|
1101
1208
|
# Bin continuous values into discrete categories based on their quantiles.
|
1102
1209
|
#
|
1103
|
-
# @param quantiles [
|
1104
|
-
# Either
|
1210
|
+
# @param quantiles [Object]
|
1211
|
+
# Either an array of quantile probabilities between 0 and 1 or a positive
|
1105
1212
|
# integer determining the number of bins with uniform probability.
|
1106
1213
|
# @param labels [Array]
|
1107
1214
|
# Names of the categories. The number of labels must be equal to the number
|
@@ -1237,6 +1344,64 @@ module Polars
|
|
1237
1344
|
super
|
1238
1345
|
end
|
1239
1346
|
|
1347
|
+
# Bin values into buckets and count their occurrences.
|
1348
|
+
#
|
1349
|
+
# @note
|
1350
|
+
# This functionality is considered **unstable**. It may be changed
|
1351
|
+
# at any point without it being considered a breaking change.
|
1352
|
+
#
|
1353
|
+
# @param bins [Object]
|
1354
|
+
# Bin edges. If nil given, we determine the edges based on the data.
|
1355
|
+
# @param bin_count [Integer]
|
1356
|
+
# If `bins` is not provided, `bin_count` uniform bins are created that fully
|
1357
|
+
# encompass the data.
|
1358
|
+
# @param include_category [Boolean]
|
1359
|
+
# Include a column that shows the intervals as categories.
|
1360
|
+
# @param include_breakpoint [Boolean]
|
1361
|
+
# Include a column that indicates the upper breakpoint.
|
1362
|
+
#
|
1363
|
+
# @return [DataFrame]
|
1364
|
+
#
|
1365
|
+
# @example
|
1366
|
+
# a = Polars::Series.new("a", [1, 3, 8, 8, 2, 1, 3])
|
1367
|
+
# a.hist(bin_count: 4)
|
1368
|
+
# # =>
|
1369
|
+
# # shape: (4, 3)
|
1370
|
+
# # ┌────────────┬─────────────┬───────┐
|
1371
|
+
# # │ breakpoint ┆ category ┆ count │
|
1372
|
+
# # │ --- ┆ --- ┆ --- │
|
1373
|
+
# # │ f64 ┆ cat ┆ u32 │
|
1374
|
+
# # ╞════════════╪═════════════╪═══════╡
|
1375
|
+
# # │ 2.75 ┆ [1.0, 2.75] ┆ 3 │
|
1376
|
+
# # │ 4.5 ┆ (2.75, 4.5] ┆ 2 │
|
1377
|
+
# # │ 6.25 ┆ (4.5, 6.25] ┆ 0 │
|
1378
|
+
# # │ 8.0 ┆ (6.25, 8.0] ┆ 2 │
|
1379
|
+
# # └────────────┴─────────────┴───────┘
|
1380
|
+
def hist(
|
1381
|
+
bins: nil,
|
1382
|
+
bin_count: nil,
|
1383
|
+
include_category: true,
|
1384
|
+
include_breakpoint: true
|
1385
|
+
)
|
1386
|
+
out = (
|
1387
|
+
to_frame
|
1388
|
+
.select_seq(
|
1389
|
+
F.col(name).hist(
|
1390
|
+
bins: bins,
|
1391
|
+
bin_count: bin_count,
|
1392
|
+
include_category: include_category,
|
1393
|
+
include_breakpoint: include_breakpoint
|
1394
|
+
)
|
1395
|
+
)
|
1396
|
+
.to_series
|
1397
|
+
)
|
1398
|
+
if !include_breakpoint && !include_category
|
1399
|
+
out.to_frame
|
1400
|
+
else
|
1401
|
+
out.struct.unnest
|
1402
|
+
end
|
1403
|
+
end
|
1404
|
+
|
1240
1405
|
# Count the unique values in a Series.
|
1241
1406
|
#
|
1242
1407
|
# @param sort [Boolean]
|
@@ -1466,6 +1631,29 @@ module Polars
|
|
1466
1631
|
end
|
1467
1632
|
alias_method :cumsum, :cum_sum
|
1468
1633
|
|
1634
|
+
# Return the cumulative count of the non-null values in the column.
|
1635
|
+
#
|
1636
|
+
# @param reverse [Boolean]
|
1637
|
+
# Reverse the operation.
|
1638
|
+
#
|
1639
|
+
# @return [Series]
|
1640
|
+
#
|
1641
|
+
# @example
|
1642
|
+
# s = Polars::Series.new(["x", "k", nil, "d"])
|
1643
|
+
# s.cum_count
|
1644
|
+
# # =>
|
1645
|
+
# # shape: (4,)
|
1646
|
+
# # Series: '' [u32]
|
1647
|
+
# # [
|
1648
|
+
# # 1
|
1649
|
+
# # 2
|
1650
|
+
# # 2
|
1651
|
+
# # 3
|
1652
|
+
# # ]
|
1653
|
+
def cum_count(reverse: false)
|
1654
|
+
super
|
1655
|
+
end
|
1656
|
+
|
1469
1657
|
# Get an array with the cumulative min computed at every element.
|
1470
1658
|
#
|
1471
1659
|
# @param reverse [Boolean]
|
@@ -1539,29 +1727,6 @@ module Polars
|
|
1539
1727
|
end
|
1540
1728
|
alias_method :cumprod, :cum_prod
|
1541
1729
|
|
1542
|
-
# Get the first `n` rows.
|
1543
|
-
#
|
1544
|
-
# Alias for {#head}.
|
1545
|
-
#
|
1546
|
-
# @param n [Integer]
|
1547
|
-
# Number of rows to return.
|
1548
|
-
#
|
1549
|
-
# @return [Series]
|
1550
|
-
#
|
1551
|
-
# @example
|
1552
|
-
# s = Polars::Series.new("a", [1, 2, 3])
|
1553
|
-
# s.limit(2)
|
1554
|
-
# # =>
|
1555
|
-
# # shape: (2,)
|
1556
|
-
# # Series: 'a' [i64]
|
1557
|
-
# # [
|
1558
|
-
# # 1
|
1559
|
-
# # 2
|
1560
|
-
# # ]
|
1561
|
-
def limit(n = 10)
|
1562
|
-
to_frame.select(F.col(name).limit(n)).to_series
|
1563
|
-
end
|
1564
|
-
|
1565
1730
|
# Get a slice of this Series.
|
1566
1731
|
#
|
1567
1732
|
# @param offset [Integer]
|
@@ -1715,13 +1880,41 @@ module Polars
|
|
1715
1880
|
to_frame.select(F.col(name).tail(n)).to_series
|
1716
1881
|
end
|
1717
1882
|
|
1883
|
+
# Get the first `n` rows.
|
1884
|
+
#
|
1885
|
+
# Alias for {#head}.
|
1886
|
+
#
|
1887
|
+
# @param n [Integer]
|
1888
|
+
# Number of rows to return.
|
1889
|
+
#
|
1890
|
+
# @return [Series]
|
1891
|
+
#
|
1892
|
+
# @example
|
1893
|
+
# s = Polars::Series.new("a", [1, 2, 3])
|
1894
|
+
# s.limit(2)
|
1895
|
+
# # =>
|
1896
|
+
# # shape: (2,)
|
1897
|
+
# # Series: 'a' [i64]
|
1898
|
+
# # [
|
1899
|
+
# # 1
|
1900
|
+
# # 2
|
1901
|
+
# # ]
|
1902
|
+
def limit(n = 10)
|
1903
|
+
to_frame.select(F.col(name).limit(n)).to_series
|
1904
|
+
end
|
1905
|
+
|
1718
1906
|
# Take every nth value in the Series and return as new Series.
|
1719
1907
|
#
|
1908
|
+
# @param n [Integer]
|
1909
|
+
# Gather every *n*-th row.
|
1910
|
+
# @param offset [Integer]
|
1911
|
+
# Start the row index at this offset.
|
1912
|
+
#
|
1720
1913
|
# @return [Series]
|
1721
1914
|
#
|
1722
1915
|
# @example
|
1723
1916
|
# s = Polars::Series.new("a", [1, 2, 3, 4])
|
1724
|
-
# s.
|
1917
|
+
# s.gather_every(2)
|
1725
1918
|
# # =>
|
1726
1919
|
# # shape: (2,)
|
1727
1920
|
# # Series: 'a' [i64]
|
@@ -1729,9 +1922,20 @@ module Polars
|
|
1729
1922
|
# # 1
|
1730
1923
|
# # 3
|
1731
1924
|
# # ]
|
1732
|
-
|
1925
|
+
#
|
1926
|
+
# @example
|
1927
|
+
# s.gather_every(2, 1)
|
1928
|
+
# # =>
|
1929
|
+
# # shape: (2,)
|
1930
|
+
# # Series: 'a' [i64]
|
1931
|
+
# # [
|
1932
|
+
# # 2
|
1933
|
+
# # 4
|
1934
|
+
# # ]
|
1935
|
+
def gather_every(n, offset = 0)
|
1733
1936
|
super
|
1734
1937
|
end
|
1938
|
+
alias_method :take_every, :gather_every
|
1735
1939
|
|
1736
1940
|
# Sort this Series.
|
1737
1941
|
#
|
@@ -1799,6 +2003,44 @@ module Polars
|
|
1799
2003
|
super
|
1800
2004
|
end
|
1801
2005
|
|
2006
|
+
# Return the `k` largest elements of the `by` column.
|
2007
|
+
#
|
2008
|
+
# Non-null elements are always preferred over null elements, regardless of
|
2009
|
+
# the value of `reverse`. The output is not guaranteed to be in any
|
2010
|
+
# particular order, call `sort` after this function if you wish the
|
2011
|
+
# output to be sorted.
|
2012
|
+
#
|
2013
|
+
# @param by [Object]
|
2014
|
+
# Column used to determine the largest elements.
|
2015
|
+
# Accepts expression input. Strings are parsed as column names.
|
2016
|
+
# @param k [Integer]
|
2017
|
+
# Number of elements to return.
|
2018
|
+
# @param reverse [Object]
|
2019
|
+
# Consider the `k` smallest elements of the `by` column (instead of the `k`
|
2020
|
+
# largest). This can be specified per column by passing a sequence of
|
2021
|
+
# booleans.
|
2022
|
+
#
|
2023
|
+
# @return [Series]
|
2024
|
+
#
|
2025
|
+
# @example
|
2026
|
+
# s = Polars::Series.new("a", [2, 5, 1, 4, 3])
|
2027
|
+
# s.top_k_by("a", k: 3)
|
2028
|
+
# # =>
|
2029
|
+
# # shape: (3,)
|
2030
|
+
# # Series: 'a' [i64]
|
2031
|
+
# # [
|
2032
|
+
# # 5
|
2033
|
+
# # 4
|
2034
|
+
# # 3
|
2035
|
+
# # ]
|
2036
|
+
def top_k_by(
|
2037
|
+
by,
|
2038
|
+
k: 5,
|
2039
|
+
reverse: false
|
2040
|
+
)
|
2041
|
+
super
|
2042
|
+
end
|
2043
|
+
|
1802
2044
|
# Return the `k` smallest elements.
|
1803
2045
|
#
|
1804
2046
|
# @param k [Integer]
|
@@ -1821,6 +2063,44 @@ module Polars
|
|
1821
2063
|
super
|
1822
2064
|
end
|
1823
2065
|
|
2066
|
+
# Return the `k` smallest elements of the `by` column.
|
2067
|
+
#
|
2068
|
+
# Non-null elements are always preferred over null elements, regardless of
|
2069
|
+
# the value of `reverse`. The output is not guaranteed to be in any
|
2070
|
+
# particular order, call `sort` after this function if you wish the
|
2071
|
+
# output to be sorted.
|
2072
|
+
#
|
2073
|
+
# @param by [Object]
|
2074
|
+
# Column used to determine the smallest elements.
|
2075
|
+
# Accepts expression input. Strings are parsed as column names.
|
2076
|
+
# @param k [Integer]
|
2077
|
+
# Number of elements to return.
|
2078
|
+
# @param reverse [Object]
|
2079
|
+
# Consider the `k` largest elements of the `by` column( (instead of the `k`
|
2080
|
+
# smallest). This can be specified per column by passing a sequence of
|
2081
|
+
# booleans.
|
2082
|
+
#
|
2083
|
+
# @return [Series]
|
2084
|
+
#
|
2085
|
+
# @example
|
2086
|
+
# s = Polars::Series.new("a", [2, 5, 1, 4, 3])
|
2087
|
+
# s.bottom_k_by("a", k: 3)
|
2088
|
+
# # =>
|
2089
|
+
# # shape: (3,)
|
2090
|
+
# # Series: 'a' [i64]
|
2091
|
+
# # [
|
2092
|
+
# # 1
|
2093
|
+
# # 2
|
2094
|
+
# # 3
|
2095
|
+
# # ]
|
2096
|
+
def bottom_k_by(
|
2097
|
+
by,
|
2098
|
+
k: 5,
|
2099
|
+
reverse: false
|
2100
|
+
)
|
2101
|
+
super
|
2102
|
+
end
|
2103
|
+
|
1824
2104
|
# Get the index values that would sort this Series.
|
1825
2105
|
#
|
1826
2106
|
# @param reverse [Boolean]
|
@@ -1990,7 +2270,7 @@ module Polars
|
|
1990
2270
|
#
|
1991
2271
|
# @example
|
1992
2272
|
# s = Polars::Series.new("a", [1, 2, 3, 4])
|
1993
|
-
# s.
|
2273
|
+
# s.gather([1, 3])
|
1994
2274
|
# # =>
|
1995
2275
|
# # shape: (2,)
|
1996
2276
|
# # Series: 'a' [i64]
|
@@ -1998,9 +2278,10 @@ module Polars
|
|
1998
2278
|
# # 2
|
1999
2279
|
# # 4
|
2000
2280
|
# # ]
|
2001
|
-
def
|
2002
|
-
|
2281
|
+
def gather(indices)
|
2282
|
+
super
|
2003
2283
|
end
|
2284
|
+
alias_method :take, :gather
|
2004
2285
|
|
2005
2286
|
# Count the null values in this Series.
|
2006
2287
|
#
|
@@ -2047,6 +2328,48 @@ module Polars
|
|
2047
2328
|
end
|
2048
2329
|
alias_method :empty?, :is_empty
|
2049
2330
|
|
2331
|
+
# Check if the Series is sorted.
|
2332
|
+
#
|
2333
|
+
# @param descending [Boolean]
|
2334
|
+
# Check if the Series is sorted in descending order
|
2335
|
+
# @param nulls_last [Boolean]
|
2336
|
+
# Set nulls at the end of the Series in sorted check.
|
2337
|
+
#
|
2338
|
+
# @return [Boolean]
|
2339
|
+
#
|
2340
|
+
# @example
|
2341
|
+
# s = Polars::Series.new([1, 3, 2])
|
2342
|
+
# s.is_sorted
|
2343
|
+
# # => false
|
2344
|
+
#
|
2345
|
+
# @example
|
2346
|
+
# s = Polars::Series.new([3, 2, 1])
|
2347
|
+
# s.is_sorted(descending: true)
|
2348
|
+
# # => true
|
2349
|
+
def is_sorted(descending: false, nulls_last: false)
|
2350
|
+
_s.is_sorted(descending, nulls_last)
|
2351
|
+
end
|
2352
|
+
alias_method :sorted?, :is_sorted
|
2353
|
+
|
2354
|
+
# Negate a boolean Series.
|
2355
|
+
#
|
2356
|
+
# @return [Series]
|
2357
|
+
#
|
2358
|
+
# @example
|
2359
|
+
# s = Polars::Series.new("a", [true, false, false])
|
2360
|
+
# s.not_
|
2361
|
+
# # =>
|
2362
|
+
# # shape: (3,)
|
2363
|
+
# # Series: 'a' [bool]
|
2364
|
+
# # [
|
2365
|
+
# # false
|
2366
|
+
# # true
|
2367
|
+
# # true
|
2368
|
+
# # ]
|
2369
|
+
def not_
|
2370
|
+
self.class._from_rbseries(_s.not_)
|
2371
|
+
end
|
2372
|
+
|
2050
2373
|
# Returns a boolean Series indicating which values are null.
|
2051
2374
|
#
|
2052
2375
|
# @return [Series]
|
@@ -2292,6 +2615,28 @@ module Polars
|
|
2292
2615
|
end
|
2293
2616
|
alias_method :is_first, :is_first_distinct
|
2294
2617
|
|
2618
|
+
|
2619
|
+
# Return a boolean mask indicating the last occurrence of each distinct value.
|
2620
|
+
#
|
2621
|
+
# @return [Series]
|
2622
|
+
#
|
2623
|
+
# @example
|
2624
|
+
# s = Polars::Series.new([1, 1, 2, 3, 2])
|
2625
|
+
# s.is_last_distinct
|
2626
|
+
# # =>
|
2627
|
+
# # shape: (5,)
|
2628
|
+
# # Series: '' [bool]
|
2629
|
+
# # [
|
2630
|
+
# # false
|
2631
|
+
# # true
|
2632
|
+
# # false
|
2633
|
+
# # true
|
2634
|
+
# # true
|
2635
|
+
# # ]
|
2636
|
+
def is_last_distinct
|
2637
|
+
super
|
2638
|
+
end
|
2639
|
+
|
2295
2640
|
# Get mask of all duplicated values.
|
2296
2641
|
#
|
2297
2642
|
# @return [Series]
|
@@ -2509,15 +2854,133 @@ module Polars
|
|
2509
2854
|
super
|
2510
2855
|
end
|
2511
2856
|
|
2512
|
-
#
|
2857
|
+
# Get a boolean mask of the values that are between the given lower/upper bounds.
|
2513
2858
|
#
|
2514
|
-
# @
|
2859
|
+
# @param lower_bound [Object]
|
2860
|
+
# Lower bound value. Accepts expression input. Non-expression inputs
|
2861
|
+
# (including strings) are parsed as literals.
|
2862
|
+
# @param upper_bound [Object]
|
2863
|
+
# Upper bound value. Accepts expression input. Non-expression inputs
|
2864
|
+
# (including strings) are parsed as literals.
|
2865
|
+
# @param closed ['both', 'left', 'right', 'none']
|
2866
|
+
# Define which sides of the interval are closed (inclusive).
|
2867
|
+
#
|
2868
|
+
# @return [Series]
|
2869
|
+
#
|
2870
|
+
# @note
|
2871
|
+
# If the value of the `lower_bound` is greater than that of the `upper_bound`
|
2872
|
+
# then the result will be False, as no value can satisfy the condition.
|
2515
2873
|
#
|
2516
2874
|
# @example
|
2517
|
-
# s = Polars::Series.new("
|
2518
|
-
# s.
|
2519
|
-
# # =>
|
2520
|
-
|
2875
|
+
# s = Polars::Series.new("num", [1, 2, 3, 4, 5])
|
2876
|
+
# s.is_between(2, 4)
|
2877
|
+
# # =>
|
2878
|
+
# # shape: (5,)
|
2879
|
+
# # Series: 'num' [bool]
|
2880
|
+
# # [
|
2881
|
+
# # false
|
2882
|
+
# # true
|
2883
|
+
# # true
|
2884
|
+
# # true
|
2885
|
+
# # false
|
2886
|
+
# # ]
|
2887
|
+
#
|
2888
|
+
# @example Use the `closed` argument to include or exclude the values at the bounds:
|
2889
|
+
# s.is_between(2, 4, closed: "left")
|
2890
|
+
# # =>
|
2891
|
+
# # shape: (5,)
|
2892
|
+
# # Series: 'num' [bool]
|
2893
|
+
# # [
|
2894
|
+
# # false
|
2895
|
+
# # true
|
2896
|
+
# # true
|
2897
|
+
# # false
|
2898
|
+
# # false
|
2899
|
+
# # ]
|
2900
|
+
#
|
2901
|
+
# @example You can also use strings as well as numeric/temporal values:
|
2902
|
+
# s = Polars::Series.new("s", ["a", "b", "c", "d", "e"])
|
2903
|
+
# s.is_between("b", "d", closed: "both")
|
2904
|
+
# # =>
|
2905
|
+
# # shape: (5,)
|
2906
|
+
# # Series: 's' [bool]
|
2907
|
+
# # [
|
2908
|
+
# # false
|
2909
|
+
# # true
|
2910
|
+
# # true
|
2911
|
+
# # true
|
2912
|
+
# # false
|
2913
|
+
# # ]
|
2914
|
+
def is_between(
|
2915
|
+
lower_bound,
|
2916
|
+
upper_bound,
|
2917
|
+
closed: "both"
|
2918
|
+
)
|
2919
|
+
if closed == "none"
|
2920
|
+
out = (self > lower_bound) & (self < upper_bound)
|
2921
|
+
elsif closed == "both"
|
2922
|
+
out = (self >= lower_bound) & (self <= upper_bound)
|
2923
|
+
elsif closed == "right"
|
2924
|
+
out = (self > lower_bound) & (self <= upper_bound)
|
2925
|
+
elsif closed == "left"
|
2926
|
+
out = (self >= lower_bound) & (self < upper_bound)
|
2927
|
+
end
|
2928
|
+
|
2929
|
+
if out.is_a?(Expr)
|
2930
|
+
out = F.select(out).to_series
|
2931
|
+
end
|
2932
|
+
|
2933
|
+
out
|
2934
|
+
end
|
2935
|
+
|
2936
|
+
# Get a boolean mask of the values being close to the other values.
|
2937
|
+
#
|
2938
|
+
# @param abs_tol [Float]
|
2939
|
+
# Absolute tolerance. This is the maximum allowed absolute difference between
|
2940
|
+
# two values. Must be non-negative.
|
2941
|
+
# @param rel_tol [Float]
|
2942
|
+
# Relative tolerance. This is the maximum allowed difference between two
|
2943
|
+
# values, relative to the larger absolute value. Must be in the range [0, 1).
|
2944
|
+
# @param nans_equal [Boolean]
|
2945
|
+
# Whether NaN values should be considered equal.
|
2946
|
+
#
|
2947
|
+
# @return [Series]
|
2948
|
+
#
|
2949
|
+
# @example
|
2950
|
+
# s = Polars::Series.new("s", [1.0, 1.2, 1.4, 1.45, 1.6])
|
2951
|
+
# s.is_close(1.4, abs_tol: 0.1)
|
2952
|
+
# # =>
|
2953
|
+
# # shape: (5,)
|
2954
|
+
# # Series: 's' [bool]
|
2955
|
+
# # [
|
2956
|
+
# # false
|
2957
|
+
# # false
|
2958
|
+
# # true
|
2959
|
+
# # true
|
2960
|
+
# # false
|
2961
|
+
# # ]
|
2962
|
+
def is_close(
|
2963
|
+
other,
|
2964
|
+
abs_tol: 0.0,
|
2965
|
+
rel_tol: 1e-09,
|
2966
|
+
nans_equal: false
|
2967
|
+
)
|
2968
|
+
F.select(
|
2969
|
+
F.lit(self).is_close(
|
2970
|
+
other, abs_tol: abs_tol, rel_tol: rel_tol, nans_equal: nans_equal
|
2971
|
+
)
|
2972
|
+
).to_series
|
2973
|
+
end
|
2974
|
+
|
2975
|
+
# Check if this Series datatype is numeric.
|
2976
|
+
#
|
2977
|
+
# @return [Boolean]
|
2978
|
+
#
|
2979
|
+
# @example
|
2980
|
+
# s = Polars::Series.new("a", [1, 2, 3])
|
2981
|
+
# s.is_numeric
|
2982
|
+
# # => true
|
2983
|
+
def is_numeric
|
2521
2984
|
[Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64, Float32, Float64].include?(dtype)
|
2522
2985
|
end
|
2523
2986
|
alias_method :numeric?, :is_numeric
|
@@ -2672,23 +3135,69 @@ module Polars
|
|
2672
3135
|
end
|
2673
3136
|
alias_method :set_at_idx, :scatter
|
2674
3137
|
|
3138
|
+
# Get the index of the first occurrence of a value, or `nil` if it's not found.
|
3139
|
+
#
|
3140
|
+
# @param element [Object]
|
3141
|
+
# Value to find.
|
3142
|
+
#
|
3143
|
+
# @return [Object]
|
3144
|
+
#
|
3145
|
+
# @example
|
3146
|
+
# s = Polars::Series.new("a", [1, nil, 17])
|
3147
|
+
# s.index_of(17)
|
3148
|
+
# # => 2
|
3149
|
+
#
|
3150
|
+
# @example
|
3151
|
+
# s.index_of(nil) # search for a null
|
3152
|
+
# # => 1
|
3153
|
+
#
|
3154
|
+
# @example
|
3155
|
+
# s.index_of(55).nil?
|
3156
|
+
# # => true
|
3157
|
+
def index_of(element)
|
3158
|
+
F.select(F.lit(self).index_of(element)).item
|
3159
|
+
end
|
3160
|
+
|
2675
3161
|
# Create an empty copy of the current Series.
|
2676
3162
|
#
|
2677
3163
|
# The copy has identical name/dtype but no data.
|
2678
3164
|
#
|
3165
|
+
# @param n [Integer]
|
3166
|
+
# Number of (empty) elements to return in the cleared frame.
|
3167
|
+
#
|
2679
3168
|
# @return [Series]
|
2680
3169
|
#
|
2681
3170
|
# @example
|
2682
3171
|
# s = Polars::Series.new("a", [nil, true, false])
|
2683
|
-
# s.
|
3172
|
+
# s.clear
|
2684
3173
|
# # =>
|
2685
3174
|
# # shape: (0,)
|
2686
3175
|
# # Series: 'a' [bool]
|
2687
3176
|
# # [
|
2688
3177
|
# # ]
|
2689
|
-
|
2690
|
-
|
3178
|
+
#
|
3179
|
+
# @example
|
3180
|
+
# s.clear(n: 2)
|
3181
|
+
# # =>
|
3182
|
+
# # shape: (2,)
|
3183
|
+
# # Series: 'a' [bool]
|
3184
|
+
# # [
|
3185
|
+
# # null
|
3186
|
+
# # null
|
3187
|
+
# # ]
|
3188
|
+
def clear(n: 0)
|
3189
|
+
if n < 0
|
3190
|
+
msg = "`n` should be greater than or equal to 0, got #{n}"
|
3191
|
+
raise ArgumentError, msg
|
3192
|
+
end
|
3193
|
+
# faster path
|
3194
|
+
if n == 0
|
3195
|
+
return self.class._from_rbseries(_s.clear)
|
3196
|
+
end
|
3197
|
+
s = len > 0 ? self.class.new(name, [], dtype: dtype) : clone
|
3198
|
+
n > 0 ? s.extend_constant(nil, n) : s
|
2691
3199
|
end
|
3200
|
+
alias_method :cleared, :clear
|
2692
3201
|
|
2693
3202
|
# clone handled by initialize_copy
|
2694
3203
|
|
@@ -2767,6 +3276,30 @@ module Polars
|
|
2767
3276
|
super
|
2768
3277
|
end
|
2769
3278
|
|
3279
|
+
# Fill missing values with the next non-null value.
|
3280
|
+
#
|
3281
|
+
# This is an alias of `.fill_null(strategy: "backward")`.
|
3282
|
+
#
|
3283
|
+
# @param limit [Integer]
|
3284
|
+
# The number of consecutive null values to backward fill.
|
3285
|
+
#
|
3286
|
+
# @return [Series]
|
3287
|
+
def backward_fill(limit: nil)
|
3288
|
+
fill_null(strategy: "backward", limit: limit)
|
3289
|
+
end
|
3290
|
+
|
3291
|
+
# Fill missing values with the next non-null value.
|
3292
|
+
#
|
3293
|
+
# This is an alias of `.fill_null(strategy: "forward")`.
|
3294
|
+
#
|
3295
|
+
# @param limit [Integer]
|
3296
|
+
# The number of consecutive null values to forward fill.
|
3297
|
+
#
|
3298
|
+
# @return [Series]
|
3299
|
+
def forward_fill(limit: nil)
|
3300
|
+
fill_null(strategy: "forward", limit: limit)
|
3301
|
+
end
|
3302
|
+
|
2770
3303
|
# Rounds down to the nearest integer value.
|
2771
3304
|
#
|
2772
3305
|
# Only works on floating point Series.
|
@@ -2831,6 +3364,28 @@ module Polars
|
|
2831
3364
|
super
|
2832
3365
|
end
|
2833
3366
|
|
3367
|
+
# Round to a number of significant figures.
|
3368
|
+
#
|
3369
|
+
# @param digits [Integer]
|
3370
|
+
# Number of significant figures to round to.
|
3371
|
+
#
|
3372
|
+
# @return [Series]
|
3373
|
+
#
|
3374
|
+
# @example
|
3375
|
+
# s = Polars::Series.new([0.01234, 3.333, 3450.0])
|
3376
|
+
# s.round_sig_figs(2)
|
3377
|
+
# # =>
|
3378
|
+
# # shape: (3,)
|
3379
|
+
# # Series: '' [f64]
|
3380
|
+
# # [
|
3381
|
+
# # 0.012
|
3382
|
+
# # 3.3
|
3383
|
+
# # 3500.0
|
3384
|
+
# # ]
|
3385
|
+
def round_sig_figs(digits)
|
3386
|
+
super
|
3387
|
+
end
|
3388
|
+
|
2834
3389
|
# Compute the dot/inner product between two Series.
|
2835
3390
|
#
|
2836
3391
|
# @param other [Object]
|
@@ -2951,6 +3506,25 @@ module Polars
|
|
2951
3506
|
super
|
2952
3507
|
end
|
2953
3508
|
|
3509
|
+
# Compute the element-wise value for the cotangent.
|
3510
|
+
#
|
3511
|
+
# @return [Series]
|
3512
|
+
#
|
3513
|
+
# @example
|
3514
|
+
# s = Polars::Series.new("a", [0.0, Math::PI / 2.0, Math::PI])
|
3515
|
+
# s.cot
|
3516
|
+
# # =>
|
3517
|
+
# # shape: (3,)
|
3518
|
+
# # Series: 'a' [f64]
|
3519
|
+
# # [
|
3520
|
+
# # inf
|
3521
|
+
# # 6.1232e-17
|
3522
|
+
# # -8.1656e15
|
3523
|
+
# # ]
|
3524
|
+
def cot
|
3525
|
+
super
|
3526
|
+
end
|
3527
|
+
|
2954
3528
|
# Compute the element-wise value for the inverse sine.
|
2955
3529
|
#
|
2956
3530
|
# @return [Series]
|
@@ -3209,7 +3783,7 @@ module Polars
|
|
3209
3783
|
# @param periods [Integer]
|
3210
3784
|
# Number of places to shift (may be negative).
|
3211
3785
|
# @param fill_value [Object]
|
3212
|
-
# Fill
|
3786
|
+
# Fill nil values with the result of this expression.
|
3213
3787
|
#
|
3214
3788
|
# @return [Series]
|
3215
3789
|
def shift_and_fill(periods, fill_value)
|
@@ -3273,7 +3847,7 @@ module Polars
|
|
3273
3847
|
# elementwise with the values in the window.
|
3274
3848
|
# @param min_periods [Integer]
|
3275
3849
|
# The number of values in the window that should be non-null before computing
|
3276
|
-
# a result. If
|
3850
|
+
# a result. If nil, it will be set equal to window size.
|
3277
3851
|
# @param center [Boolean]
|
3278
3852
|
# Set the labels at the center of the window
|
3279
3853
|
#
|
@@ -3314,7 +3888,7 @@ module Polars
|
|
3314
3888
|
# elementwise with the values in the window.
|
3315
3889
|
# @param min_periods [Integer]
|
3316
3890
|
# The number of values in the window that should be non-null before computing
|
3317
|
-
# a result. If
|
3891
|
+
# a result. If nil, it will be set equal to window size.
|
3318
3892
|
# @param center [Boolean]
|
3319
3893
|
# Set the labels at the center of the window
|
3320
3894
|
#
|
@@ -3355,7 +3929,7 @@ module Polars
|
|
3355
3929
|
# elementwise with the values in the window.
|
3356
3930
|
# @param min_periods [Integer]
|
3357
3931
|
# The number of values in the window that should be non-null before computing
|
3358
|
-
# a result. If
|
3932
|
+
# a result. If nil, it will be set equal to window size.
|
3359
3933
|
# @param center [Boolean]
|
3360
3934
|
# Set the labels at the center of the window
|
3361
3935
|
#
|
@@ -3396,7 +3970,7 @@ module Polars
|
|
3396
3970
|
# elementwise with the values in the window.
|
3397
3971
|
# @param min_periods [Integer]
|
3398
3972
|
# The number of values in the window that should be non-null before computing
|
3399
|
-
# a result. If
|
3973
|
+
# a result. If nil, it will be set equal to window size.
|
3400
3974
|
# @param center [Boolean]
|
3401
3975
|
# Set the labels at the center of the window
|
3402
3976
|
#
|
@@ -3437,7 +4011,7 @@ module Polars
|
|
3437
4011
|
# elementwise with the values in the window.
|
3438
4012
|
# @param min_periods [Integer]
|
3439
4013
|
# The number of values in the window that should be non-null before computing
|
3440
|
-
# a result. If
|
4014
|
+
# a result. If nil, it will be set equal to window size.
|
3441
4015
|
# @param center [Boolean]
|
3442
4016
|
# Set the labels at the center of the window
|
3443
4017
|
# @param ddof [Integer]
|
@@ -3482,7 +4056,7 @@ module Polars
|
|
3482
4056
|
# elementwise with the values in the window.
|
3483
4057
|
# @param min_periods [Integer]
|
3484
4058
|
# The number of values in the window that should be non-null before computing
|
3485
|
-
# a result. If
|
4059
|
+
# a result. If nil, it will be set equal to window size.
|
3486
4060
|
# @param center [Boolean]
|
3487
4061
|
# Set the labels at the center of the window
|
3488
4062
|
# @param ddof [Integer]
|
@@ -3526,7 +4100,7 @@ module Polars
|
|
3526
4100
|
# elementwise with the values in the window.
|
3527
4101
|
# @param min_periods [Integer]
|
3528
4102
|
# The number of values in the window that should be non-null before computing
|
3529
|
-
# a result. If
|
4103
|
+
# a result. If nil, it will be set equal to window size.
|
3530
4104
|
# @param center [Boolean]
|
3531
4105
|
# Set the labels at the center of the window
|
3532
4106
|
#
|
@@ -3568,7 +4142,7 @@ module Polars
|
|
3568
4142
|
# elementwise with the values in the window.
|
3569
4143
|
# @param min_periods [Integer]
|
3570
4144
|
# The number of values in the window that should be non-null before computing
|
3571
|
-
# a result. If
|
4145
|
+
# a result. If nil, it will be set equal to window size.
|
3572
4146
|
# @param center [Boolean]
|
3573
4147
|
# Set the labels at the center of the window
|
3574
4148
|
#
|
@@ -3642,11 +4216,56 @@ module Polars
|
|
3642
4216
|
super
|
3643
4217
|
end
|
3644
4218
|
|
4219
|
+
# Compute a rolling kurtosis.
|
4220
|
+
#
|
4221
|
+
# @note
|
4222
|
+
# This functionality is considered **unstable**. It may be changed
|
4223
|
+
# at any point without it being considered a breaking change.
|
4224
|
+
#
|
4225
|
+
# The window at a given row will include the row itself, and the `window_size - 1`
|
4226
|
+
# elements before it.
|
4227
|
+
#
|
4228
|
+
# @param window_size [Integer]
|
4229
|
+
# Integer size of the rolling window.
|
4230
|
+
# @param fisher [Boolean]
|
4231
|
+
# If true, Fisher's definition is used (normal ==> 0.0). If false,
|
4232
|
+
# Pearson's definition is used (normal ==> 3.0).
|
4233
|
+
# @param bias [Boolean]
|
4234
|
+
# If false, the calculations are corrected for statistical bias.
|
4235
|
+
# @param min_samples [Integer]
|
4236
|
+
# The number of values in the window that should be non-null before computing
|
4237
|
+
# a result. If set to `nil` (default), it will be set equal to `window_size`.
|
4238
|
+
# @param center [Boolean]
|
4239
|
+
# Set the labels at the center of the window.
|
4240
|
+
#
|
4241
|
+
# @return [Series]
|
4242
|
+
#
|
4243
|
+
# @example
|
4244
|
+
# Polars::Series.new([1, 4, 2, 9]).rolling_kurtosis(3)
|
4245
|
+
# # =>
|
4246
|
+
# # shape: (4,)
|
4247
|
+
# # Series: '' [f64]
|
4248
|
+
# # [
|
4249
|
+
# # null
|
4250
|
+
# # null
|
4251
|
+
# # -1.5
|
4252
|
+
# # -1.5
|
4253
|
+
# # ]
|
4254
|
+
def rolling_kurtosis(
|
4255
|
+
window_size,
|
4256
|
+
fisher: true,
|
4257
|
+
bias: true,
|
4258
|
+
min_samples: nil,
|
4259
|
+
center: false
|
4260
|
+
)
|
4261
|
+
super
|
4262
|
+
end
|
4263
|
+
|
3645
4264
|
# Sample from this Series.
|
3646
4265
|
#
|
3647
4266
|
# @param n [Integer]
|
3648
4267
|
# Number of items to return. Cannot be used with `frac`. Defaults to 1 if
|
3649
|
-
# `frac` is
|
4268
|
+
# `frac` is nil.
|
3650
4269
|
# @param frac [Float]
|
3651
4270
|
# Fraction of items to return. Cannot be used with `n`.
|
3652
4271
|
# @param with_replacement [Boolean]
|
@@ -3654,7 +4273,7 @@ module Polars
|
|
3654
4273
|
# @param shuffle [Boolean]
|
3655
4274
|
# Shuffle the order of sampled data points.
|
3656
4275
|
# @param seed [Integer]
|
3657
|
-
# Seed for the random number generator. If set to
|
4276
|
+
# Seed for the random number generator. If set to nil (default), a random
|
3658
4277
|
# seed is used.
|
3659
4278
|
#
|
3660
4279
|
# @return [Series]
|
@@ -4152,6 +4771,60 @@ module Polars
|
|
4152
4771
|
super
|
4153
4772
|
end
|
4154
4773
|
|
4774
|
+
# Return the lower bound of this Series' dtype as a unit Series.
|
4775
|
+
#
|
4776
|
+
# @return [Series]
|
4777
|
+
#
|
4778
|
+
# @example
|
4779
|
+
# s = Polars::Series.new("s", [-1, 0, 1], dtype: Polars::Int32)
|
4780
|
+
# s.lower_bound
|
4781
|
+
# # =>
|
4782
|
+
# # shape: (1,)
|
4783
|
+
# # Series: 's' [i32]
|
4784
|
+
# # [
|
4785
|
+
# # -2147483648
|
4786
|
+
# # ]
|
4787
|
+
#
|
4788
|
+
# @example
|
4789
|
+
# s = Polars::Series.new("s", [1.0, 2.5, 3.0], dtype: Polars::Float32)
|
4790
|
+
# s.lower_bound
|
4791
|
+
# # =>
|
4792
|
+
# # shape: (1,)
|
4793
|
+
# # Series: 's' [f32]
|
4794
|
+
# # [
|
4795
|
+
# # -inf
|
4796
|
+
# # ]
|
4797
|
+
def lower_bound
|
4798
|
+
super
|
4799
|
+
end
|
4800
|
+
|
4801
|
+
# Return the upper bound of this Series' dtype as a unit Series.
|
4802
|
+
#
|
4803
|
+
# @return [Series]
|
4804
|
+
#
|
4805
|
+
# @example
|
4806
|
+
# s = Polars::Series.new("s", [-1, 0, 1], dtype: Polars::Int8)
|
4807
|
+
# s.upper_bound
|
4808
|
+
# # =>
|
4809
|
+
# # shape: (1,)
|
4810
|
+
# # Series: 's' [i8]
|
4811
|
+
# # [
|
4812
|
+
# # 127
|
4813
|
+
# # ]
|
4814
|
+
#
|
4815
|
+
# @example
|
4816
|
+
# s = Polars::Series.new("s", [1.0, 2.5, 3.0], dtype: Polars::Float64)
|
4817
|
+
# s.upper_bound
|
4818
|
+
# # =>
|
4819
|
+
# # shape: (1,)
|
4820
|
+
# # Series: 's' [f64]
|
4821
|
+
# # [
|
4822
|
+
# # inf
|
4823
|
+
# # ]
|
4824
|
+
def upper_bound
|
4825
|
+
super
|
4826
|
+
end
|
4827
|
+
|
4155
4828
|
# Replace values by different values.
|
4156
4829
|
#
|
4157
4830
|
# @param old [Object]
|
@@ -4224,6 +4897,120 @@ module Polars
|
|
4224
4897
|
super
|
4225
4898
|
end
|
4226
4899
|
|
4900
|
+
# Replace all values by different values.
|
4901
|
+
#
|
4902
|
+
# @param old [Object]
|
4903
|
+
# Value or sequence of values to replace.
|
4904
|
+
# Also accepts a mapping of values to their replacement as syntactic sugar for
|
4905
|
+
# `replace_strict(old: Polars::Series.new(mapping.keys), new: Polars::Series.new(mapping.values))`.
|
4906
|
+
# @param new [Object]
|
4907
|
+
# Value or sequence of values to replace by.
|
4908
|
+
# Length must match the length of `old` or have length 1.
|
4909
|
+
# @param default [Object]
|
4910
|
+
# Set values that were not replaced to this value. If no default is specified,
|
4911
|
+
# (default), an error is raised if any values were not replaced.
|
4912
|
+
# Accepts expression input. Non-expression inputs are parsed as literals.
|
4913
|
+
# @param return_dtype [Object]
|
4914
|
+
# The data type of the resulting Series. If set to `nil` (default),
|
4915
|
+
# the data type is determined automatically based on the other inputs.
|
4916
|
+
#
|
4917
|
+
# @return [Series]
|
4918
|
+
#
|
4919
|
+
# @example Replace values by passing sequences to the `old` and `new` parameters.
|
4920
|
+
# s = Polars::Series.new([1, 2, 2, 3])
|
4921
|
+
# s.replace_strict([1, 2, 3], [100, 200, 300])
|
4922
|
+
# # =>
|
4923
|
+
# # shape: (4,)
|
4924
|
+
# # Series: '' [i64]
|
4925
|
+
# # [
|
4926
|
+
# # 100
|
4927
|
+
# # 200
|
4928
|
+
# # 200
|
4929
|
+
# # 300
|
4930
|
+
# # ]
|
4931
|
+
#
|
4932
|
+
# @example Passing a mapping with replacements is also supported as syntactic sugar.
|
4933
|
+
# mapping = {1 => 100, 2 => 200, 3 => 300}
|
4934
|
+
# s.replace_strict(mapping)
|
4935
|
+
# # =>
|
4936
|
+
# # shape: (4,)
|
4937
|
+
# # Series: '' [i64]
|
4938
|
+
# # [
|
4939
|
+
# # 100
|
4940
|
+
# # 200
|
4941
|
+
# # 200
|
4942
|
+
# # 300
|
4943
|
+
# # ]
|
4944
|
+
#
|
4945
|
+
# @example By default, an error is raised if any non-null values were not replaced. Specify a default to set all values that were not matched.
|
4946
|
+
# mapping = {2 => 200, 3 => 300}
|
4947
|
+
# s.replace_strict(mapping, default: -1)
|
4948
|
+
# # =>
|
4949
|
+
# # shape: (4,)
|
4950
|
+
# # Series: '' [i64]
|
4951
|
+
# # [
|
4952
|
+
# # -1
|
4953
|
+
# # 200
|
4954
|
+
# # 200
|
4955
|
+
# # 300
|
4956
|
+
# # ]
|
4957
|
+
#
|
4958
|
+
# @example The default can be another Series.
|
4959
|
+
# default = Polars::Series.new([2.5, 5.0, 7.5, 10.0])
|
4960
|
+
# s.replace_strict(2, 200, default: default)
|
4961
|
+
# # =>
|
4962
|
+
# # shape: (4,)
|
4963
|
+
# # Series: '' [f64]
|
4964
|
+
# # [
|
4965
|
+
# # 2.5
|
4966
|
+
# # 200.0
|
4967
|
+
# # 200.0
|
4968
|
+
# # 10.0
|
4969
|
+
# # ]
|
4970
|
+
#
|
4971
|
+
# @example Replacing by values of a different data type sets the return type based on a combination of the `new` data type and the `default` data type.
|
4972
|
+
# s = Polars::Series.new(["x", "y", "z"])
|
4973
|
+
# mapping = {"x" => 1, "y" => 2, "z" => 3}
|
4974
|
+
# s.replace_strict(mapping)
|
4975
|
+
# # =>
|
4976
|
+
# # shape: (3,)
|
4977
|
+
# # Series: '' [i64]
|
4978
|
+
# # [
|
4979
|
+
# # 1
|
4980
|
+
# # 2
|
4981
|
+
# # 3
|
4982
|
+
# # ]
|
4983
|
+
#
|
4984
|
+
# @example
|
4985
|
+
# s.replace_strict(mapping, default: "x")
|
4986
|
+
# # =>
|
4987
|
+
# # shape: (3,)
|
4988
|
+
# # Series: '' [str]
|
4989
|
+
# # [
|
4990
|
+
# # "1"
|
4991
|
+
# # "2"
|
4992
|
+
# # "3"
|
4993
|
+
# # ]
|
4994
|
+
#
|
4995
|
+
# @example Set the `return_dtype` parameter to control the resulting data type directly.
|
4996
|
+
# s.replace_strict(mapping, return_dtype: Polars::UInt8)
|
4997
|
+
# # =>
|
4998
|
+
# # shape: (3,)
|
4999
|
+
# # Series: '' [u8]
|
5000
|
+
# # [
|
5001
|
+
# # 1
|
5002
|
+
# # 2
|
5003
|
+
# # 3
|
5004
|
+
# # ]
|
5005
|
+
def replace_strict(
|
5006
|
+
old,
|
5007
|
+
new = Expr::NO_DEFAULT,
|
5008
|
+
default: Expr::NO_DEFAULT,
|
5009
|
+
return_dtype: nil
|
5010
|
+
)
|
5011
|
+
super
|
5012
|
+
end
|
5013
|
+
|
4227
5014
|
# Reshape this Series to a flat Series or a Series of Lists.
|
4228
5015
|
#
|
4229
5016
|
# @param dims [Array]
|
@@ -4313,6 +5100,68 @@ module Polars
|
|
4313
5100
|
super
|
4314
5101
|
end
|
4315
5102
|
|
5103
|
+
# Compute time-based exponentially weighted moving average.
|
5104
|
+
#
|
5105
|
+
# @param by [Object]
|
5106
|
+
# Times to calculate average by. Should be `DateTime`, `Date`, `UInt64`,
|
5107
|
+
# `UInt32`, `Int64`, or `Int32` data type.
|
5108
|
+
# @param half_life [String]
|
5109
|
+
# Unit over which observation decays to half its value.
|
5110
|
+
#
|
5111
|
+
# Can be created either from a timedelta, or
|
5112
|
+
# by using the following string language:
|
5113
|
+
#
|
5114
|
+
# - 1ns (1 nanosecond)
|
5115
|
+
# - 1us (1 microsecond)
|
5116
|
+
# - 1ms (1 millisecond)
|
5117
|
+
# - 1s (1 second)
|
5118
|
+
# - 1m (1 minute)
|
5119
|
+
# - 1h (1 hour)
|
5120
|
+
# - 1d (1 day)
|
5121
|
+
# - 1w (1 week)
|
5122
|
+
# - 1i (1 index count)
|
5123
|
+
#
|
5124
|
+
# Or combine them:
|
5125
|
+
# "3d12h4m25s" # 3 days, 12 hours, 4 minutes, and 25 seconds
|
5126
|
+
#
|
5127
|
+
# Note that `half_life` is treated as a constant duration - calendar
|
5128
|
+
# durations such as months (or even days in the time-zone-aware case)
|
5129
|
+
# are not supported, please express your duration in an approximately
|
5130
|
+
# equivalent number of hours (e.g. '370h' instead of '1mo').
|
5131
|
+
#
|
5132
|
+
# @return [Series]
|
5133
|
+
#
|
5134
|
+
# @example
|
5135
|
+
# df = Polars::DataFrame.new(
|
5136
|
+
# {
|
5137
|
+
# "values" => [0, 1, 2, nil, 4],
|
5138
|
+
# "times" => [
|
5139
|
+
# Date.new(2020, 1, 1),
|
5140
|
+
# Date.new(2020, 1, 3),
|
5141
|
+
# Date.new(2020, 1, 10),
|
5142
|
+
# Date.new(2020, 1, 15),
|
5143
|
+
# Date.new(2020, 1, 17)
|
5144
|
+
# ]
|
5145
|
+
# }
|
5146
|
+
# ).sort("times")
|
5147
|
+
# df["values"].ewm_mean_by(df["times"], half_life: "4d")
|
5148
|
+
# # =>
|
5149
|
+
# # shape: (5,)
|
5150
|
+
# # Series: 'values' [f64]
|
5151
|
+
# # [
|
5152
|
+
# # 0.0
|
5153
|
+
# # 0.292893
|
5154
|
+
# # 1.492474
|
5155
|
+
# # null
|
5156
|
+
# # 3.254508
|
5157
|
+
# # ]
|
5158
|
+
def ewm_mean_by(
|
5159
|
+
by,
|
5160
|
+
half_life:
|
5161
|
+
)
|
5162
|
+
super
|
5163
|
+
end
|
5164
|
+
|
4316
5165
|
# Exponentially-weighted moving standard deviation.
|
4317
5166
|
#
|
4318
5167
|
# @return [Series]
|
@@ -4461,6 +5310,140 @@ module Polars
|
|
4461
5310
|
super
|
4462
5311
|
end
|
4463
5312
|
|
5313
|
+
# Get the chunks of this Series as a list of Series.
|
5314
|
+
#
|
5315
|
+
# @return [Array]
|
5316
|
+
#
|
5317
|
+
# @example
|
5318
|
+
# s1 = Polars::Series.new("a", [1, 2, 3])
|
5319
|
+
# s2 = Polars::Series.new("a", [4, 5, 6])
|
5320
|
+
# s = Polars.concat([s1, s2], rechunk: false)
|
5321
|
+
# s.get_chunks
|
5322
|
+
# # =>
|
5323
|
+
# # [shape: (3,)
|
5324
|
+
# # Series: 'a' [i64]
|
5325
|
+
# # [
|
5326
|
+
# # 1
|
5327
|
+
# # 2
|
5328
|
+
# # 3
|
5329
|
+
# # ], shape: (3,)
|
5330
|
+
# # Series: 'a' [i64]
|
5331
|
+
# # [
|
5332
|
+
# # 4
|
5333
|
+
# # 5
|
5334
|
+
# # 6
|
5335
|
+
# # ]]
|
5336
|
+
def get_chunks
|
5337
|
+
_s.get_chunks
|
5338
|
+
end
|
5339
|
+
|
5340
|
+
# Aggregate values into a list.
|
5341
|
+
#
|
5342
|
+
# @return [Series]
|
5343
|
+
#
|
5344
|
+
# @example
|
5345
|
+
# s = Polars::Series.new("a", [1, 2, 3])
|
5346
|
+
# s.implode
|
5347
|
+
# # =>
|
5348
|
+
# # shape: (1,)
|
5349
|
+
# # Series: 'a' [list[i64]]
|
5350
|
+
# # [
|
5351
|
+
# # [1, 2, 3]
|
5352
|
+
# # ]
|
5353
|
+
def implode
|
5354
|
+
super
|
5355
|
+
end
|
5356
|
+
|
5357
|
+
# Evaluate the number of set bits.
|
5358
|
+
#
|
5359
|
+
# @return [Series]
|
5360
|
+
def bitwise_count_ones
|
5361
|
+
super
|
5362
|
+
end
|
5363
|
+
|
5364
|
+
# Evaluate the number of unset bits.
|
5365
|
+
#
|
5366
|
+
# @return [Series]
|
5367
|
+
def bitwise_count_zeros
|
5368
|
+
super
|
5369
|
+
end
|
5370
|
+
|
5371
|
+
# Evaluate the number most-significant set bits before seeing an unset bit.
|
5372
|
+
#
|
5373
|
+
# @return [Series]
|
5374
|
+
def bitwise_leading_ones
|
5375
|
+
super
|
5376
|
+
end
|
5377
|
+
|
5378
|
+
# Evaluate the number most-significant unset bits before seeing a set bit.
|
5379
|
+
#
|
5380
|
+
# @return [Series]
|
5381
|
+
def bitwise_leading_zeros
|
5382
|
+
super
|
5383
|
+
end
|
5384
|
+
|
5385
|
+
# Evaluate the number least-significant set bits before seeing an unset bit.
|
5386
|
+
#
|
5387
|
+
# @return [Series]
|
5388
|
+
def bitwise_trailing_ones
|
5389
|
+
super
|
5390
|
+
end
|
5391
|
+
|
5392
|
+
# Evaluate the number least-significant unset bits before seeing a set bit.
|
5393
|
+
#
|
5394
|
+
# @return [Series]
|
5395
|
+
def bitwise_trailing_zeros
|
5396
|
+
super
|
5397
|
+
end
|
5398
|
+
|
5399
|
+
# Perform an aggregation of bitwise ANDs.
|
5400
|
+
#
|
5401
|
+
# @return [Object]
|
5402
|
+
def bitwise_and
|
5403
|
+
_s.bitwise_and
|
5404
|
+
end
|
5405
|
+
|
5406
|
+
# Perform an aggregation of bitwise ORs.
|
5407
|
+
#
|
5408
|
+
# @return [Object]
|
5409
|
+
def bitwise_or
|
5410
|
+
_s.bitwise_or
|
5411
|
+
end
|
5412
|
+
|
5413
|
+
# Perform an aggregation of bitwise XORs.
|
5414
|
+
#
|
5415
|
+
# @return [Object]
|
5416
|
+
def bitwise_xor
|
5417
|
+
_s.bitwise_xor
|
5418
|
+
end
|
5419
|
+
|
5420
|
+
# Get the first element of the Series.
|
5421
|
+
#
|
5422
|
+
# Returns `nil` if the Series is empty.
|
5423
|
+
#
|
5424
|
+
# @return [Object]
|
5425
|
+
def first
|
5426
|
+
_s.first
|
5427
|
+
end
|
5428
|
+
|
5429
|
+
# Get the last element of the Series.
|
5430
|
+
#
|
5431
|
+
# Returns `nil` if the Series is empty.
|
5432
|
+
#
|
5433
|
+
# @return [Object]
|
5434
|
+
def last
|
5435
|
+
_s.last
|
5436
|
+
end
|
5437
|
+
|
5438
|
+
# Approximate count of unique values.
|
5439
|
+
#
|
5440
|
+
# This is done using the HyperLogLog++ algorithm for cardinality estimation.
|
5441
|
+
#
|
5442
|
+
# @return [Object]
|
5443
|
+
def approx_n_unique
|
5444
|
+
_s.approx_n_unique
|
5445
|
+
end
|
5446
|
+
|
4464
5447
|
# Create an object namespace of all list related methods.
|
4465
5448
|
#
|
4466
5449
|
# @return [ListNameSpace]
|
@@ -4510,6 +5493,20 @@ module Polars
|
|
4510
5493
|
StructNameSpace.new(self)
|
4511
5494
|
end
|
4512
5495
|
|
5496
|
+
# Repeat the elements in this Series as specified in the given expression.
|
5497
|
+
#
|
5498
|
+
# The repeated elements are expanded into a List.
|
5499
|
+
#
|
5500
|
+
# @param by [Object]
|
5501
|
+
# Numeric column that determines how often the values will be repeated.
|
5502
|
+
# The column will be coerced to UInt32. Give this dtype to make the coercion
|
5503
|
+
# a no-op.
|
5504
|
+
#
|
5505
|
+
# @return [Object]
|
5506
|
+
def repeat_by(by)
|
5507
|
+
super
|
5508
|
+
end
|
5509
|
+
|
4513
5510
|
private
|
4514
5511
|
|
4515
5512
|
def initialize_copy(other)
|