polars-df 0.25.0 → 0.26.0
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 +33 -0
- data/Cargo.lock +270 -97
- data/LICENSE.txt +1 -1
- data/README.md +1 -3
- data/ext/polars/Cargo.toml +19 -18
- data/ext/polars/src/catalog/unity.rs +15 -20
- data/ext/polars/src/conversion/any_value.rs +53 -29
- data/ext/polars/src/conversion/chunked_array.rs +58 -56
- data/ext/polars/src/conversion/datetime.rs +58 -7
- data/ext/polars/src/conversion/mod.rs +200 -150
- data/ext/polars/src/dataframe/export.rs +15 -12
- data/ext/polars/src/dataframe/general.rs +25 -7
- data/ext/polars/src/dataframe/map.rs +6 -4
- data/ext/polars/src/error.rs +1 -1
- data/ext/polars/src/expr/array.rs +0 -24
- data/ext/polars/src/expr/datatype.rs +13 -3
- data/ext/polars/src/expr/datetime.rs +4 -4
- data/ext/polars/src/expr/general.rs +35 -15
- data/ext/polars/src/expr/list.rs +0 -26
- data/ext/polars/src/expr/rolling.rs +24 -0
- data/ext/polars/src/functions/business.rs +2 -2
- data/ext/polars/src/functions/io.rs +4 -3
- data/ext/polars/src/functions/lazy.rs +65 -46
- data/ext/polars/src/functions/meta.rs +6 -5
- data/ext/polars/src/functions/mod.rs +0 -1
- data/ext/polars/src/functions/range.rs +13 -0
- data/ext/polars/src/functions/utils.rs +4 -2
- data/ext/polars/src/interop/arrow/mod.rs +4 -2
- data/ext/polars/src/interop/arrow/to_rb.rs +1 -1
- data/ext/polars/src/interop/numo/to_numo_series.rs +26 -25
- data/ext/polars/src/io/scan_options.rs +6 -3
- data/ext/polars/src/io/sink_options.rs +2 -0
- data/ext/polars/src/lazyframe/general.rs +243 -17
- data/ext/polars/src/lazyframe/optflags.rs +2 -1
- data/ext/polars/src/lib.rs +39 -35
- data/ext/polars/src/map/lazy.rs +5 -2
- data/ext/polars/src/map/series.rs +19 -18
- data/ext/polars/src/on_startup.rs +25 -6
- data/ext/polars/src/ruby/numo.rs +3 -4
- data/ext/polars/src/ruby/plan_callback.rs +1 -4
- data/ext/polars/src/ruby/rb_modules.rs +2 -4
- data/ext/polars/src/ruby/ruby_udf.rs +7 -9
- data/ext/polars/src/ruby/utils.rs +12 -1
- data/ext/polars/src/series/aggregation.rs +13 -1
- data/ext/polars/src/series/construction.rs +31 -50
- data/ext/polars/src/series/export.rs +33 -38
- data/ext/polars/src/series/general.rs +6 -6
- data/ext/polars/src/series/map.rs +3 -2
- data/ext/polars/src/series/scatter.rs +4 -4
- data/ext/polars/src/utils.rs +31 -7
- data/lib/polars/array_expr.rb +23 -7
- data/lib/polars/array_name_space.rb +16 -2
- data/lib/polars/binary_name_space.rb +32 -0
- data/lib/polars/collect_batches.rb +4 -0
- data/lib/polars/data_frame.rb +144 -11
- data/lib/polars/data_type_group.rb +5 -0
- data/lib/polars/date_time_expr.rb +91 -3
- data/lib/polars/date_time_name_space.rb +7 -1
- data/lib/polars/expr.rb +247 -44
- data/lib/polars/functions/business.rb +2 -2
- data/lib/polars/functions/datatype.rb +30 -0
- data/lib/polars/functions/eager.rb +80 -7
- data/lib/polars/functions/lazy.rb +97 -2
- data/lib/polars/functions/range/linear_space.rb +118 -0
- data/lib/polars/io/csv.rb +27 -5
- data/lib/polars/io/database.rb +2 -3
- data/lib/polars/io/ipc.rb +2 -2
- data/lib/polars/io/lines.rb +172 -0
- data/lib/polars/io/parquet.rb +1 -1
- data/lib/polars/io/sink_options.rb +5 -2
- data/lib/polars/lazy_frame.rb +517 -14
- data/lib/polars/list_expr.rb +21 -7
- data/lib/polars/list_name_space.rb +16 -2
- data/lib/polars/query_opt_flags.rb +23 -5
- data/lib/polars/selectors.rb +2 -2
- data/lib/polars/series.rb +176 -19
- data/lib/polars/sql_context.rb +2 -2
- data/lib/polars/string_cache.rb +19 -72
- data/lib/polars/string_expr.rb +1 -7
- data/lib/polars/string_name_space.rb +1 -7
- data/lib/polars/utils/construction/series.rb +24 -39
- data/lib/polars/utils/convert.rb +16 -6
- data/lib/polars/utils/parse.rb +7 -0
- data/lib/polars/utils/reduce_balanced.rb +43 -0
- data/lib/polars/utils/various.rb +5 -0
- data/lib/polars/version.rb +1 -1
- data/lib/polars.rb +2 -1
- metadata +4 -17
- data/ext/polars/src/functions/string_cache.rs +0 -24
|
@@ -141,6 +141,9 @@ module Polars
|
|
|
141
141
|
#
|
|
142
142
|
# @param columns [Array]
|
|
143
143
|
# One or more column names.
|
|
144
|
+
# @param maintain_order [Boolean]
|
|
145
|
+
# Whether to preserve the order of elements in the list. Setting this
|
|
146
|
+
# to `false` can improve performance, especially within `group_by`.
|
|
144
147
|
#
|
|
145
148
|
# @return [Expr]
|
|
146
149
|
#
|
|
@@ -174,8 +177,8 @@ module Polars
|
|
|
174
177
|
# # ╞═══════════╪═══════════════════════╡
|
|
175
178
|
# # │ [9, 8, 7] ┆ ["foo", "bar", "foo"] │
|
|
176
179
|
# # └───────────┴───────────────────────┘
|
|
177
|
-
def implode(*columns)
|
|
178
|
-
col(*columns).implode
|
|
180
|
+
def implode(*columns, maintain_order: true)
|
|
181
|
+
col(*columns).implode(maintain_order: maintain_order)
|
|
179
182
|
end
|
|
180
183
|
|
|
181
184
|
# Get the standard deviation.
|
|
@@ -1568,6 +1571,25 @@ module Polars
|
|
|
1568
1571
|
result
|
|
1569
1572
|
end
|
|
1570
1573
|
|
|
1574
|
+
# Explain multiple LazyFrames as if passed to `collect_all`.
|
|
1575
|
+
#
|
|
1576
|
+
# Common Subplan Elimination is applied on the combined plan, meaning
|
|
1577
|
+
# that diverging queries will run only once.
|
|
1578
|
+
#
|
|
1579
|
+
# @param lazy_frames [Array]
|
|
1580
|
+
# A list of LazyFrames to collect.
|
|
1581
|
+
# @param optimizations [Object]
|
|
1582
|
+
# The optimization passes done during query optimization.
|
|
1583
|
+
#
|
|
1584
|
+
# @return [String]
|
|
1585
|
+
def explain_all(
|
|
1586
|
+
lazy_frames,
|
|
1587
|
+
optimizations: DEFAULT_QUERY_OPT_FLAGS
|
|
1588
|
+
)
|
|
1589
|
+
lfs = lazy_frames.map { |lf| lf._ldf }
|
|
1590
|
+
Plr.explain_all(lfs, optimizations._rboptflags)
|
|
1591
|
+
end
|
|
1592
|
+
|
|
1571
1593
|
# Run polars expressions without a context.
|
|
1572
1594
|
#
|
|
1573
1595
|
# This is syntactic sugar for running `df.select` on an empty DataFrame.
|
|
@@ -1890,6 +1912,79 @@ module Polars
|
|
|
1890
1912
|
end
|
|
1891
1913
|
end
|
|
1892
1914
|
|
|
1915
|
+
# Generates a sequence of integers.
|
|
1916
|
+
#
|
|
1917
|
+
# The length of the returned sequence will match the context length, and the
|
|
1918
|
+
# datatype will match the one returned by `get_index_dtype()`.
|
|
1919
|
+
#
|
|
1920
|
+
# If you would like to generate sequences with custom offsets / length /
|
|
1921
|
+
# step size / datatypes, it is recommended to use `int_range` instead.
|
|
1922
|
+
#
|
|
1923
|
+
# @note
|
|
1924
|
+
# This functionality is considered **unstable**. It may be changed
|
|
1925
|
+
# at any point without it being considered a breaking change.
|
|
1926
|
+
#
|
|
1927
|
+
# @param name [String]
|
|
1928
|
+
# Name of the returned column.
|
|
1929
|
+
#
|
|
1930
|
+
# @return [Expr]
|
|
1931
|
+
#
|
|
1932
|
+
# @example
|
|
1933
|
+
# df = Polars::DataFrame.new({"x" => ["A", "A", "B", "B", "B"]})
|
|
1934
|
+
# df.with_columns(Polars.row_index, Polars.row_index("another_index"))
|
|
1935
|
+
# # =>
|
|
1936
|
+
# # shape: (5, 3)
|
|
1937
|
+
# # ┌─────┬───────┬───────────────┐
|
|
1938
|
+
# # │ x ┆ index ┆ another_index │
|
|
1939
|
+
# # │ --- ┆ --- ┆ --- │
|
|
1940
|
+
# # │ str ┆ u32 ┆ u32 │
|
|
1941
|
+
# # ╞═════╪═══════╪═══════════════╡
|
|
1942
|
+
# # │ A ┆ 0 ┆ 0 │
|
|
1943
|
+
# # │ A ┆ 1 ┆ 1 │
|
|
1944
|
+
# # │ B ┆ 2 ┆ 2 │
|
|
1945
|
+
# # │ B ┆ 3 ┆ 3 │
|
|
1946
|
+
# # │ B ┆ 4 ┆ 4 │
|
|
1947
|
+
# # └─────┴───────┴───────────────┘
|
|
1948
|
+
#
|
|
1949
|
+
# @example
|
|
1950
|
+
# df.group_by("x").agg(Polars.row_index).sort("x")
|
|
1951
|
+
# # =>
|
|
1952
|
+
# # shape: (2, 2)
|
|
1953
|
+
# # ┌─────┬───────────┐
|
|
1954
|
+
# # │ x ┆ index │
|
|
1955
|
+
# # │ --- ┆ --- │
|
|
1956
|
+
# # │ str ┆ list[u32] │
|
|
1957
|
+
# # ╞═════╪═══════════╡
|
|
1958
|
+
# # │ A ┆ [0, 1] │
|
|
1959
|
+
# # │ B ┆ [0, 1, 2] │
|
|
1960
|
+
# # └─────┴───────────┘
|
|
1961
|
+
#
|
|
1962
|
+
# @example
|
|
1963
|
+
# df.select(Polars.row_index)
|
|
1964
|
+
# # =>
|
|
1965
|
+
# # shape: (5, 1)
|
|
1966
|
+
# # ┌───────┐
|
|
1967
|
+
# # │ index │
|
|
1968
|
+
# # │ --- │
|
|
1969
|
+
# # │ u32 │
|
|
1970
|
+
# # ╞═══════╡
|
|
1971
|
+
# # │ 0 │
|
|
1972
|
+
# # │ 1 │
|
|
1973
|
+
# # │ 2 │
|
|
1974
|
+
# # │ 3 │
|
|
1975
|
+
# # │ 4 │
|
|
1976
|
+
# # └───────┘
|
|
1977
|
+
def row_index(name = "index")
|
|
1978
|
+
# Notes
|
|
1979
|
+
# * Dispatching to `int_range` means that we cannot accept an offset
|
|
1980
|
+
# parameter, as unlike `DataFrame.with_row_index`, `int_range` will simply
|
|
1981
|
+
# truncate instead of raising an error.
|
|
1982
|
+
F.int_range(
|
|
1983
|
+
F.len,
|
|
1984
|
+
dtype: get_index_type
|
|
1985
|
+
).alias(name)
|
|
1986
|
+
end
|
|
1987
|
+
|
|
1893
1988
|
private
|
|
1894
1989
|
|
|
1895
1990
|
def _wrap_acc_lambda(function)
|
|
@@ -1,5 +1,123 @@
|
|
|
1
1
|
module Polars
|
|
2
2
|
module Functions
|
|
3
|
+
# Create sequence of evenly-spaced points.
|
|
4
|
+
#
|
|
5
|
+
# @param start [Object]
|
|
6
|
+
# Lower bound of the range.
|
|
7
|
+
# @param stop [Object]
|
|
8
|
+
# Upper bound of the range.
|
|
9
|
+
# @param num_samples [Object]
|
|
10
|
+
# Number of samples in the output sequence.
|
|
11
|
+
# @param closed ['both', 'left', 'right', 'none']
|
|
12
|
+
# Define which sides of the interval are closed (inclusive).
|
|
13
|
+
# @param eager [Boolean]
|
|
14
|
+
# Evaluate immediately and return a `Series`.
|
|
15
|
+
# If set to `false` (default), return an expression instead.
|
|
16
|
+
#
|
|
17
|
+
# @return [Object]
|
|
18
|
+
#
|
|
19
|
+
# @note
|
|
20
|
+
# This functionality is experimental. It may be changed at any point without it
|
|
21
|
+
# being considered a breaking change.
|
|
22
|
+
#
|
|
23
|
+
# @example
|
|
24
|
+
# Polars.linear_space(0, 1, 3, eager: true)
|
|
25
|
+
# # =>
|
|
26
|
+
# # shape: (3,)
|
|
27
|
+
# # Series: 'literal' [f64]
|
|
28
|
+
# # [
|
|
29
|
+
# # 0.0
|
|
30
|
+
# # 0.5
|
|
31
|
+
# # 1.0
|
|
32
|
+
# # ]
|
|
33
|
+
#
|
|
34
|
+
# @example
|
|
35
|
+
# Polars.linear_space(0, 1, 3, closed: "left", eager: true)
|
|
36
|
+
# # =>
|
|
37
|
+
# # shape: (3,)
|
|
38
|
+
# # Series: 'literal' [f64]
|
|
39
|
+
# # [
|
|
40
|
+
# # 0.0
|
|
41
|
+
# # 0.333333
|
|
42
|
+
# # 0.666667
|
|
43
|
+
# # ]
|
|
44
|
+
#
|
|
45
|
+
# @example
|
|
46
|
+
# Polars.linear_space(0, 1, 3, closed: "right", eager: true)
|
|
47
|
+
# # =>
|
|
48
|
+
# # shape: (3,)
|
|
49
|
+
# # Series: 'literal' [f64]
|
|
50
|
+
# # [
|
|
51
|
+
# # 0.333333
|
|
52
|
+
# # 0.666667
|
|
53
|
+
# # 1.0
|
|
54
|
+
# # ]
|
|
55
|
+
#
|
|
56
|
+
# @example
|
|
57
|
+
# Polars.linear_space(0, 1, 3, closed: "none", eager: true)
|
|
58
|
+
# # =>
|
|
59
|
+
# # shape: (3,)
|
|
60
|
+
# # Series: 'literal' [f64]
|
|
61
|
+
# # [
|
|
62
|
+
# # 0.25
|
|
63
|
+
# # 0.5
|
|
64
|
+
# # 0.75
|
|
65
|
+
# # ]
|
|
66
|
+
#
|
|
67
|
+
# @example `Date` endpoints generate a sequence of `Datetime` values:
|
|
68
|
+
# Polars.linear_space(
|
|
69
|
+
# Date.new(2025, 1, 1),
|
|
70
|
+
# Date.new(2025, 2, 1),
|
|
71
|
+
# 3,
|
|
72
|
+
# closed: "right",
|
|
73
|
+
# eager: true
|
|
74
|
+
# )
|
|
75
|
+
# # =>
|
|
76
|
+
# # shape: (3,)
|
|
77
|
+
# # Series: 'literal' [datetime[μs]]
|
|
78
|
+
# # [
|
|
79
|
+
# # 2025-01-11 08:00:00
|
|
80
|
+
# # 2025-01-21 16:00:00
|
|
81
|
+
# # 2025-02-01 00:00:00
|
|
82
|
+
# # ]
|
|
83
|
+
#
|
|
84
|
+
# @example When `eager: false` (default), an expression is produced. You can generate a sequence using the length of the dataframe:
|
|
85
|
+
# df = Polars::DataFrame.new({"a" => [1, 2, 3, 4, 5]})
|
|
86
|
+
# df.with_columns(Polars.linear_space(0, 1, Polars.len).alias("ls"))
|
|
87
|
+
# # =>
|
|
88
|
+
# # shape: (5, 2)
|
|
89
|
+
# # ┌─────┬──────┐
|
|
90
|
+
# # │ a ┆ ls │
|
|
91
|
+
# # │ --- ┆ --- │
|
|
92
|
+
# # │ i64 ┆ f64 │
|
|
93
|
+
# # ╞═════╪══════╡
|
|
94
|
+
# # │ 1 ┆ 0.0 │
|
|
95
|
+
# # │ 2 ┆ 0.25 │
|
|
96
|
+
# # │ 3 ┆ 0.5 │
|
|
97
|
+
# # │ 4 ┆ 0.75 │
|
|
98
|
+
# # │ 5 ┆ 1.0 │
|
|
99
|
+
# # └─────┴──────┘
|
|
100
|
+
def linear_space(
|
|
101
|
+
start,
|
|
102
|
+
stop,
|
|
103
|
+
num_samples,
|
|
104
|
+
closed: "both",
|
|
105
|
+
eager: false
|
|
106
|
+
)
|
|
107
|
+
start_rbexpr = Utils.parse_into_expression(start)
|
|
108
|
+
end_rbexpr = Utils.parse_into_expression(stop)
|
|
109
|
+
num_samples_rbexpr = Utils.parse_into_expression(num_samples)
|
|
110
|
+
result = Utils.wrap_expr(
|
|
111
|
+
Plr.linear_space(start_rbexpr, end_rbexpr, num_samples_rbexpr, closed)
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
if eager
|
|
115
|
+
return F.select(result).to_series
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
result
|
|
119
|
+
end
|
|
120
|
+
|
|
3
121
|
# Generate a sequence of evenly-spaced values for each row between `start` and `end`.
|
|
4
122
|
#
|
|
5
123
|
# The number of values in each sequence is determined by `num_samples`.
|
data/lib/polars/io/csv.rb
CHANGED
|
@@ -648,6 +648,12 @@ module Polars
|
|
|
648
648
|
# (which defaults to 1 hour) if not given.
|
|
649
649
|
# @param include_file_paths [String]
|
|
650
650
|
# Include the path of the source file(s) as a column with this name.
|
|
651
|
+
# @param missing_columns ['insert', 'raise']
|
|
652
|
+
# Configuration for behavior when columns defined in the schema are
|
|
653
|
+
# missing from the data:
|
|
654
|
+
#
|
|
655
|
+
# * `"insert"`: Insert the missing columns with NULL values.
|
|
656
|
+
# * `"raise"`: Raise an error.
|
|
651
657
|
#
|
|
652
658
|
# @return [LazyFrame]
|
|
653
659
|
def scan_csv(
|
|
@@ -663,7 +669,7 @@ module Polars
|
|
|
663
669
|
null_values: nil,
|
|
664
670
|
missing_utf8_is_empty_string: false,
|
|
665
671
|
ignore_errors: false,
|
|
666
|
-
cache:
|
|
672
|
+
cache: nil,
|
|
667
673
|
with_column_names: nil,
|
|
668
674
|
infer_schema: true,
|
|
669
675
|
infer_schema_length: N_INFER_DEFAULT,
|
|
@@ -685,7 +691,8 @@ module Polars
|
|
|
685
691
|
credential_provider: "auto",
|
|
686
692
|
retries: nil,
|
|
687
693
|
file_cache_ttl: nil,
|
|
688
|
-
include_file_paths: nil
|
|
694
|
+
include_file_paths: nil,
|
|
695
|
+
missing_columns: nil
|
|
689
696
|
)
|
|
690
697
|
if new_columns&.any? && schema_overrides.is_a?(::Array)
|
|
691
698
|
msg = "expected 'schema_overrides' hash, found #{schema_overrides.inspect}"
|
|
@@ -734,6 +741,11 @@ module Polars
|
|
|
734
741
|
storage_options["file_cache_ttl"] = file_cache_ttl
|
|
735
742
|
end
|
|
736
743
|
|
|
744
|
+
if !missing_columns.nil?
|
|
745
|
+
msg = "The `missing_columns` parameter of `scan_csv` is considered unstable."
|
|
746
|
+
Utils.issue_unstable_warning(msg)
|
|
747
|
+
end
|
|
748
|
+
|
|
737
749
|
credential_provider_builder = _init_credential_provider_builder(
|
|
738
750
|
credential_provider, source, storage_options, "scan_csv"
|
|
739
751
|
)
|
|
@@ -768,7 +780,8 @@ module Polars
|
|
|
768
780
|
glob: glob,
|
|
769
781
|
storage_options: storage_options,
|
|
770
782
|
credential_provider: credential_provider_builder,
|
|
771
|
-
include_file_paths: include_file_paths
|
|
783
|
+
include_file_paths: include_file_paths,
|
|
784
|
+
missing_columns: missing_columns
|
|
772
785
|
)
|
|
773
786
|
end
|
|
774
787
|
|
|
@@ -804,7 +817,8 @@ module Polars
|
|
|
804
817
|
glob: true,
|
|
805
818
|
storage_options: nil,
|
|
806
819
|
credential_provider: nil,
|
|
807
|
-
include_file_paths: nil
|
|
820
|
+
include_file_paths: nil,
|
|
821
|
+
missing_columns: nil
|
|
808
822
|
)
|
|
809
823
|
dtype_list = nil
|
|
810
824
|
if !schema_overrides.nil?
|
|
@@ -822,6 +836,13 @@ module Polars
|
|
|
822
836
|
sources = []
|
|
823
837
|
end
|
|
824
838
|
|
|
839
|
+
# TODO: This is a hack. We conditionally set `missing_columns` to mimic
|
|
840
|
+
# existing behavior. This should be removed once the workaround is no
|
|
841
|
+
# longer needed.
|
|
842
|
+
if missing_columns.nil? && !schema.nil? && has_header
|
|
843
|
+
missing_columns = "insert"
|
|
844
|
+
end
|
|
845
|
+
|
|
825
846
|
rblf =
|
|
826
847
|
RbLazyFrame.new_from_csv(
|
|
827
848
|
source,
|
|
@@ -854,7 +875,8 @@ module Polars
|
|
|
854
875
|
schema,
|
|
855
876
|
storage_options,
|
|
856
877
|
credential_provider,
|
|
857
|
-
include_file_paths
|
|
878
|
+
include_file_paths,
|
|
879
|
+
missing_columns
|
|
858
880
|
)
|
|
859
881
|
Utils.wrap_ldf(rblf)
|
|
860
882
|
end
|
data/lib/polars/io/database.rb
CHANGED
data/lib/polars/io/ipc.rb
CHANGED
|
@@ -218,7 +218,7 @@ module Polars
|
|
|
218
218
|
def scan_ipc(
|
|
219
219
|
source,
|
|
220
220
|
n_rows: nil,
|
|
221
|
-
cache:
|
|
221
|
+
cache: nil,
|
|
222
222
|
rechunk: false,
|
|
223
223
|
row_index_name: nil,
|
|
224
224
|
row_index_offset: 0,
|
|
@@ -267,7 +267,7 @@ module Polars
|
|
|
267
267
|
try_parse_hive_dates: try_parse_hive_dates,
|
|
268
268
|
rechunk: rechunk,
|
|
269
269
|
cache: cache,
|
|
270
|
-
storage_options:
|
|
270
|
+
storage_options: storage_options,
|
|
271
271
|
credential_provider: credential_provider_builder
|
|
272
272
|
)
|
|
273
273
|
)
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
module Polars
|
|
2
|
+
module IO
|
|
3
|
+
# Read lines into a string column from a file.
|
|
4
|
+
#
|
|
5
|
+
# @note
|
|
6
|
+
# This functionality is considered **unstable**. It may be changed
|
|
7
|
+
# at any point without it being considered a breaking change.
|
|
8
|
+
#
|
|
9
|
+
# @param source [Object]
|
|
10
|
+
# Path(s) to a file or directory
|
|
11
|
+
# When needing to authenticate for scanning cloud locations, see the
|
|
12
|
+
# `storage_options` parameter.
|
|
13
|
+
# @param name [String]
|
|
14
|
+
# Name to use for the output column.
|
|
15
|
+
# @param n_rows [Integer]
|
|
16
|
+
# Stop reading from parquet file after reading `n_rows`.
|
|
17
|
+
# @param row_index_name [String]
|
|
18
|
+
# If not nil, this will insert a row index column with the given name into the
|
|
19
|
+
# DataFrame
|
|
20
|
+
# @param row_index_offset [Integer]
|
|
21
|
+
# Offset to start the row index column (only used if the name is set)
|
|
22
|
+
# @param glob [Boolean]
|
|
23
|
+
# Expand path given via globbing rules.
|
|
24
|
+
# @param storage_options [Hash]
|
|
25
|
+
# Options that indicate how to connect to a cloud provider.
|
|
26
|
+
#
|
|
27
|
+
# The cloud providers currently supported are AWS, GCP, and Azure.
|
|
28
|
+
# See supported keys here:
|
|
29
|
+
#
|
|
30
|
+
# * [aws](https://docs.rs/object_store/latest/object_store/aws/enum.AmazonS3ConfigKey.html)
|
|
31
|
+
# * [gcp](https://docs.rs/object_store/latest/object_store/gcp/enum.GoogleConfigKey.html)
|
|
32
|
+
# * [azure](https://docs.rs/object_store/latest/object_store/azure/enum.AzureConfigKey.html)
|
|
33
|
+
# * Hugging Face (`hf://`): Accepts an API key under the `token` parameter: \
|
|
34
|
+
# `{'token': '...'}`, or by setting the `HF_TOKEN` environment variable.
|
|
35
|
+
#
|
|
36
|
+
# If `storage_options` is not provided, Polars will try to infer the information
|
|
37
|
+
# from environment variables.
|
|
38
|
+
# @param credential_provider [Object]
|
|
39
|
+
# Provide a function that can be called to provide cloud storage
|
|
40
|
+
# credentials. The function is expected to return a dictionary of
|
|
41
|
+
# credential keys along with an optional credential expiry time.
|
|
42
|
+
# @param include_file_paths [String]
|
|
43
|
+
# Include the path of the source file(s) as a column with this name.
|
|
44
|
+
#
|
|
45
|
+
# @return [DataFrame]
|
|
46
|
+
#
|
|
47
|
+
# @example
|
|
48
|
+
# Polars.read_lines(StringIO.new("Hello\nworld"))
|
|
49
|
+
# # =>
|
|
50
|
+
# # shape: (2, 1)
|
|
51
|
+
# # ┌───────┐
|
|
52
|
+
# # │ line │
|
|
53
|
+
# # │ --- │
|
|
54
|
+
# # │ str │
|
|
55
|
+
# # ╞═══════╡
|
|
56
|
+
# # │ Hello │
|
|
57
|
+
# # │ world │
|
|
58
|
+
# # └───────┘
|
|
59
|
+
def read_lines(
|
|
60
|
+
source,
|
|
61
|
+
name: "line",
|
|
62
|
+
n_rows: nil,
|
|
63
|
+
row_index_name: nil,
|
|
64
|
+
row_index_offset: 0,
|
|
65
|
+
glob: true,
|
|
66
|
+
storage_options: nil,
|
|
67
|
+
credential_provider: "auto",
|
|
68
|
+
include_file_paths: nil
|
|
69
|
+
)
|
|
70
|
+
scan_lines(
|
|
71
|
+
source,
|
|
72
|
+
name: name,
|
|
73
|
+
n_rows: n_rows,
|
|
74
|
+
row_index_name: row_index_name,
|
|
75
|
+
row_index_offset: row_index_offset,
|
|
76
|
+
glob: glob,
|
|
77
|
+
storage_options: storage_options,
|
|
78
|
+
credential_provider: credential_provider,
|
|
79
|
+
include_file_paths: include_file_paths
|
|
80
|
+
).collect
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Construct a LazyFrame which scans lines into a string column from a file.
|
|
84
|
+
#
|
|
85
|
+
# @note
|
|
86
|
+
# This functionality is considered **unstable**. It may be changed
|
|
87
|
+
# at any point without it being considered a breaking change.
|
|
88
|
+
#
|
|
89
|
+
# @param source [Object]
|
|
90
|
+
# Path(s) to a file or directory
|
|
91
|
+
# When needing to authenticate for scanning cloud locations, see the
|
|
92
|
+
# `storage_options` parameter.
|
|
93
|
+
# @param name [String]
|
|
94
|
+
# Name to use for the output column.
|
|
95
|
+
# @param n_rows [Integer]
|
|
96
|
+
# Stop reading from parquet file after reading `n_rows`.
|
|
97
|
+
# @param row_index_name [String]
|
|
98
|
+
# If not nil, this will insert a row index column with the given name into the
|
|
99
|
+
# DataFrame
|
|
100
|
+
# @param row_index_offset
|
|
101
|
+
# Offset to start the row index column (only used if the name is set)
|
|
102
|
+
# @param glob [Boolean]
|
|
103
|
+
# Expand path given via globbing rules.
|
|
104
|
+
# @param storage_options [Hash]
|
|
105
|
+
# Options that indicate how to connect to a cloud provider.
|
|
106
|
+
#
|
|
107
|
+
# The cloud providers currently supported are AWS, GCP, and Azure.
|
|
108
|
+
# See supported keys here:
|
|
109
|
+
#
|
|
110
|
+
# * [aws](https://docs.rs/object_store/latest/object_store/aws/enum.AmazonS3ConfigKey.html)
|
|
111
|
+
# * [gcp](https://docs.rs/object_store/latest/object_store/gcp/enum.GoogleConfigKey.html)
|
|
112
|
+
# * [azure](https://docs.rs/object_store/latest/object_store/azure/enum.AzureConfigKey.html)
|
|
113
|
+
# * Hugging Face (`hf://`): Accepts an API key under the `token` parameter: \
|
|
114
|
+
# `{'token': '...'}`, or by setting the `HF_TOKEN` environment variable.
|
|
115
|
+
#
|
|
116
|
+
# If `storage_options` is not provided, Polars will try to infer the information
|
|
117
|
+
# from environment variables.
|
|
118
|
+
# @param credential_provider [Object]
|
|
119
|
+
# Provide a function that can be called to provide cloud storage
|
|
120
|
+
# credentials. The function is expected to return a dictionary of
|
|
121
|
+
# credential keys along with an optional credential expiry time.
|
|
122
|
+
# @param include_file_paths [String]
|
|
123
|
+
# Include the path of the source file(s) as a column with this name.
|
|
124
|
+
#
|
|
125
|
+
# @return [LazyFrame]
|
|
126
|
+
#
|
|
127
|
+
# @example
|
|
128
|
+
# Polars.scan_lines(StringIO.new("Hello\nworld")).collect
|
|
129
|
+
# # =>
|
|
130
|
+
# # shape: (2, 1)
|
|
131
|
+
# # ┌───────┐
|
|
132
|
+
# # │ line │
|
|
133
|
+
# # │ --- │
|
|
134
|
+
# # │ str │
|
|
135
|
+
# # ╞═══════╡
|
|
136
|
+
# # │ Hello │
|
|
137
|
+
# # │ world │
|
|
138
|
+
# # └───────┘
|
|
139
|
+
def scan_lines(
|
|
140
|
+
source,
|
|
141
|
+
name: "line",
|
|
142
|
+
n_rows: nil,
|
|
143
|
+
row_index_name: nil,
|
|
144
|
+
row_index_offset: 0,
|
|
145
|
+
glob: true,
|
|
146
|
+
storage_options: nil,
|
|
147
|
+
credential_provider: "auto",
|
|
148
|
+
include_file_paths: nil
|
|
149
|
+
)
|
|
150
|
+
sources = get_sources(source)
|
|
151
|
+
|
|
152
|
+
credential_provider_builder = _init_credential_provider_builder(
|
|
153
|
+
credential_provider, sources, storage_options, "scan_lines"
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
rblf = RbLazyFrame.new_from_scan_lines(
|
|
157
|
+
sources,
|
|
158
|
+
ScanOptions.new(
|
|
159
|
+
row_index: !row_index_name.nil? ? [row_index_name, row_index_offset] : nil,
|
|
160
|
+
pre_slice: !n_rows.nil? ? [0, n_rows] : nil,
|
|
161
|
+
include_file_paths: include_file_paths,
|
|
162
|
+
glob: glob,
|
|
163
|
+
storage_options: storage_options,
|
|
164
|
+
credential_provider: credential_provider_builder
|
|
165
|
+
),
|
|
166
|
+
name
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
Utils.wrap_ldf(rblf)
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
end
|
data/lib/polars/io/parquet.rb
CHANGED
|
@@ -350,7 +350,7 @@ module Polars
|
|
|
350
350
|
try_parse_hive_dates: try_parse_hive_dates,
|
|
351
351
|
rechunk: rechunk,
|
|
352
352
|
cache: cache,
|
|
353
|
-
storage_options: storage_options
|
|
353
|
+
storage_options: storage_options,
|
|
354
354
|
credential_provider: credential_provider_builder,
|
|
355
355
|
column_mapping: _column_mapping,
|
|
356
356
|
default_values: _default_values,
|
|
@@ -2,23 +2,26 @@ module Polars
|
|
|
2
2
|
module IO
|
|
3
3
|
# @private
|
|
4
4
|
class SinkOptions
|
|
5
|
-
attr_reader :mkdir, :maintain_order, :sync_on_close, :storage_options, :credential_provider
|
|
5
|
+
attr_reader :mkdir, :maintain_order, :sync_on_close, :storage_options, :credential_provider, :sinked_paths_callback
|
|
6
6
|
|
|
7
7
|
def initialize(
|
|
8
8
|
mkdir: nil,
|
|
9
9
|
maintain_order: nil,
|
|
10
10
|
sync_on_close: nil,
|
|
11
11
|
storage_options: nil,
|
|
12
|
-
credential_provider: nil
|
|
12
|
+
credential_provider: nil,
|
|
13
|
+
sinked_paths_callback: nil
|
|
13
14
|
)
|
|
14
15
|
@mkdir = mkdir
|
|
15
16
|
@maintain_order = maintain_order
|
|
16
17
|
@sync_on_close = sync_on_close
|
|
17
18
|
@storage_options = storage_options
|
|
18
19
|
@credential_provider = credential_provider
|
|
20
|
+
@sinked_paths_callback = sinked_paths_callback
|
|
19
21
|
end
|
|
20
22
|
end
|
|
21
23
|
end
|
|
22
24
|
|
|
25
|
+
# @private
|
|
23
26
|
SinkOptions = IO::SinkOptions
|
|
24
27
|
end
|