polars-df 0.15.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 +274 -0
- data/Cargo.lock +1465 -867
- data/Cargo.toml +3 -0
- data/LICENSE.txt +2 -2
- data/README.md +87 -37
- data/ext/polars/Cargo.toml +47 -16
- data/ext/polars/src/c_api/allocator.rs +7 -0
- data/ext/polars/src/c_api/mod.rs +1 -0
- data/ext/polars/src/catalog/mod.rs +1 -0
- data/ext/polars/src/catalog/unity.rs +470 -0
- data/ext/polars/src/conversion/any_value.rs +99 -84
- data/ext/polars/src/conversion/categorical.rs +30 -0
- data/ext/polars/src/conversion/chunked_array.rs +71 -62
- data/ext/polars/src/conversion/datetime.rs +63 -0
- data/ext/polars/src/conversion/mod.rs +796 -312
- data/ext/polars/src/dataframe/construction.rs +6 -18
- data/ext/polars/src/dataframe/export.rs +30 -39
- data/ext/polars/src/dataframe/general.rs +294 -362
- data/ext/polars/src/dataframe/io.rs +33 -150
- data/ext/polars/src/dataframe/map.rs +175 -0
- data/ext/polars/src/dataframe/mod.rs +37 -5
- data/ext/polars/src/dataframe/serde.rs +23 -8
- data/ext/polars/src/error.rs +44 -7
- data/ext/polars/src/exceptions.rs +21 -8
- data/ext/polars/src/expr/array.rs +86 -22
- data/ext/polars/src/expr/binary.rs +50 -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 +51 -0
- data/ext/polars/src/expr/datetime.rs +99 -41
- data/ext/polars/src/expr/extension.rs +12 -0
- data/ext/polars/src/expr/general.rs +252 -128
- data/ext/polars/src/expr/list.rs +56 -60
- data/ext/polars/src/expr/meta.rs +30 -35
- data/ext/polars/src/expr/mod.rs +28 -6
- data/ext/polars/src/expr/name.rs +29 -14
- data/ext/polars/src/expr/rolling.rs +111 -3
- data/ext/polars/src/expr/selector.rs +219 -0
- data/ext/polars/src/expr/serde.rs +28 -0
- data/ext/polars/src/expr/string.rs +118 -20
- data/ext/polars/src/expr/struct.rs +14 -1
- data/ext/polars/src/file.rs +194 -86
- data/ext/polars/src/functions/aggregation.rs +13 -12
- data/ext/polars/src/functions/business.rs +2 -3
- data/ext/polars/src/functions/eager.rs +3 -2
- data/ext/polars/src/functions/io.rs +90 -18
- data/ext/polars/src/functions/lazy.rs +267 -118
- data/ext/polars/src/functions/meta.rs +8 -7
- data/ext/polars/src/functions/misc.rs +1 -1
- data/ext/polars/src/functions/mod.rs +2 -1
- data/ext/polars/src/functions/range.rs +88 -31
- data/ext/polars/src/functions/strings.rs +6 -0
- data/ext/polars/src/functions/utils.rs +8 -0
- data/ext/polars/src/interop/arrow/mod.rs +52 -1
- data/ext/polars/src/interop/arrow/{to_ruby.rs → to_rb.rs} +37 -7
- data/ext/polars/src/interop/arrow/to_rust.rs +43 -0
- data/ext/polars/src/interop/numo/to_numo_df.rs +1 -1
- data/ext/polars/src/interop/numo/to_numo_series.rs +72 -50
- data/ext/polars/src/io/cloud_options.rs +107 -0
- data/ext/polars/src/io/mod.rs +4 -0
- data/ext/polars/src/io/scan_options.rs +113 -0
- data/ext/polars/src/io/sink_options.rs +46 -0
- data/ext/polars/src/io/sink_output.rs +21 -0
- data/ext/polars/src/lazyframe/exitable.rs +39 -0
- data/ext/polars/src/lazyframe/general.rs +846 -368
- data/ext/polars/src/lazyframe/mod.rs +58 -5
- data/ext/polars/src/lazyframe/optflags.rs +59 -0
- data/ext/polars/src/lazyframe/serde.rs +36 -4
- data/ext/polars/src/lazyframe/sink.rs +46 -0
- data/ext/polars/src/lazygroupby.rs +38 -9
- data/ext/polars/src/lib.rs +574 -165
- data/ext/polars/src/map/lazy.rs +44 -74
- data/ext/polars/src/map/mod.rs +18 -254
- data/ext/polars/src/map/series.rs +241 -1087
- data/ext/polars/src/on_startup.rs +192 -9
- data/ext/polars/src/prelude.rs +1 -0
- data/ext/polars/src/rb_modules.rs +10 -57
- data/ext/polars/src/ruby/exceptions.rs +26 -0
- data/ext/polars/src/ruby/gvl.rs +104 -0
- data/ext/polars/src/ruby/lazy.rs +46 -0
- data/ext/polars/src/ruby/mod.rs +11 -0
- data/ext/polars/src/ruby/numo.rs +52 -0
- data/ext/polars/src/ruby/plan_callback.rs +198 -0
- data/ext/polars/src/ruby/rb_modules.rs +16 -0
- data/ext/polars/src/ruby/ruby_convert_registry.rs +51 -0
- data/ext/polars/src/ruby/ruby_function.rs +11 -0
- data/ext/polars/src/ruby/ruby_udf.rs +164 -0
- data/ext/polars/src/ruby/thread.rs +65 -0
- data/ext/polars/src/ruby/utils.rs +39 -0
- data/ext/polars/src/series/aggregation.rs +116 -91
- data/ext/polars/src/series/arithmetic.rs +16 -22
- data/ext/polars/src/series/comparison.rs +101 -222
- data/ext/polars/src/series/construction.rs +80 -70
- data/ext/polars/src/series/export.rs +98 -56
- data/ext/polars/src/series/general.rs +323 -440
- data/ext/polars/src/series/import.rs +22 -5
- data/ext/polars/src/series/map.rs +103 -0
- data/ext/polars/src/series/mod.rs +57 -15
- data/ext/polars/src/series/scatter.rs +139 -82
- data/ext/polars/src/sql.rs +16 -9
- data/ext/polars/src/testing/frame.rs +31 -0
- data/ext/polars/src/testing/mod.rs +5 -0
- data/ext/polars/src/testing/series.rs +31 -0
- data/ext/polars/src/timeout.rs +105 -0
- data/ext/polars/src/utils.rs +105 -4
- data/lib/polars/array_expr.rb +500 -22
- data/lib/polars/array_name_space.rb +384 -10
- data/lib/polars/batched_csv_reader.rb +48 -66
- data/lib/polars/binary_expr.rb +217 -0
- data/lib/polars/binary_name_space.rb +155 -1
- data/lib/polars/cat_expr.rb +224 -0
- data/lib/polars/cat_name_space.rb +132 -32
- data/lib/polars/catalog/unity/catalog_info.rb +20 -0
- data/lib/polars/catalog/unity/column_info.rb +31 -0
- data/lib/polars/catalog/unity/namespace_info.rb +21 -0
- data/lib/polars/catalog/unity/table_info.rb +50 -0
- data/lib/polars/catalog.rb +448 -0
- data/lib/polars/collect_batches.rb +22 -0
- data/lib/polars/config.rb +3 -3
- data/lib/polars/convert.rb +201 -36
- data/lib/polars/data_frame.rb +2851 -1017
- data/lib/polars/data_frame_plot.rb +173 -0
- data/lib/polars/data_type_expr.rb +52 -0
- data/lib/polars/data_type_group.rb +6 -0
- data/lib/polars/data_types.rb +118 -18
- data/lib/polars/date_time_expr.rb +426 -84
- data/lib/polars/date_time_name_space.rb +384 -111
- data/lib/polars/dynamic_group_by.rb +102 -10
- data/lib/polars/exceptions.rb +50 -5
- data/lib/polars/expr.rb +2159 -915
- data/lib/polars/extension_expr.rb +39 -0
- data/lib/polars/extension_name_space.rb +39 -0
- data/lib/polars/functions/aggregation/horizontal.rb +11 -6
- data/lib/polars/functions/aggregation/vertical.rb +2 -3
- data/lib/polars/functions/as_datatype.rb +290 -8
- data/lib/polars/functions/business.rb +95 -0
- data/lib/polars/functions/col.rb +6 -5
- data/lib/polars/functions/datatype.rb +62 -0
- data/lib/polars/functions/eager.rb +426 -24
- data/lib/polars/functions/escape_regex.rb +21 -0
- data/lib/polars/functions/lazy.rb +813 -195
- data/lib/polars/functions/lit.rb +21 -10
- data/lib/polars/functions/range/int_range.rb +74 -2
- data/lib/polars/functions/range/linear_space.rb +195 -0
- data/lib/polars/functions/range/time_range.rb +1 -1
- data/lib/polars/functions/repeat.rb +7 -12
- data/lib/polars/functions/whenthen.rb +2 -2
- data/lib/polars/group_by.rb +188 -58
- data/lib/polars/iceberg_dataset.rb +108 -0
- data/lib/polars/in_process_query.rb +37 -0
- data/lib/polars/io/cloud.rb +18 -0
- data/lib/polars/io/csv.rb +336 -128
- data/lib/polars/io/database.rb +19 -4
- data/lib/polars/io/delta.rb +134 -0
- data/lib/polars/io/iceberg.rb +34 -0
- data/lib/polars/io/ipc.rb +63 -63
- data/lib/polars/io/json.rb +16 -0
- data/lib/polars/io/lines.rb +172 -0
- data/lib/polars/io/ndjson.rb +176 -20
- data/lib/polars/io/parquet.rb +173 -95
- data/lib/polars/io/scan_options.rb +55 -0
- data/lib/polars/io/sink_options.rb +27 -0
- data/lib/polars/io/utils.rb +17 -0
- data/lib/polars/lazy_frame.rb +3017 -622
- data/lib/polars/lazy_group_by.rb +436 -2
- data/lib/polars/list_expr.rb +551 -59
- data/lib/polars/list_name_space.rb +465 -51
- data/lib/polars/meta_expr.rb +146 -24
- data/lib/polars/name_expr.rb +87 -2
- data/lib/polars/query_opt_flags.rb +264 -0
- data/lib/polars/rolling_group_by.rb +90 -5
- data/lib/polars/scan_cast_options.rb +86 -0
- data/lib/polars/schema.rb +128 -0
- data/lib/polars/selector.rb +245 -0
- data/lib/polars/selectors.rb +1048 -201
- data/lib/polars/series.rb +2522 -774
- data/lib/polars/series_plot.rb +72 -0
- data/lib/polars/slice.rb +1 -1
- data/lib/polars/sql_context.rb +13 -6
- data/lib/polars/string_cache.rb +19 -72
- data/lib/polars/string_expr.rb +561 -107
- data/lib/polars/string_name_space.rb +781 -109
- data/lib/polars/struct_expr.rb +139 -18
- data/lib/polars/struct_name_space.rb +19 -1
- data/lib/polars/testing.rb +24 -273
- data/lib/polars/utils/constants.rb +2 -0
- data/lib/polars/utils/construction/data_frame.rb +410 -0
- data/lib/polars/utils/construction/series.rb +350 -0
- data/lib/polars/utils/construction/utils.rb +9 -0
- data/lib/polars/utils/convert.rb +18 -8
- data/lib/polars/utils/deprecation.rb +11 -0
- data/lib/polars/utils/parse.rb +62 -9
- data/lib/polars/utils/reduce_balanced.rb +43 -0
- data/lib/polars/utils/serde.rb +22 -0
- data/lib/polars/utils/unstable.rb +19 -0
- data/lib/polars/utils/various.rb +86 -1
- data/lib/polars/utils.rb +63 -48
- data/lib/polars/version.rb +1 -1
- data/lib/polars.rb +85 -2
- metadata +80 -28
- data/ext/polars/src/allocator.rs +0 -13
- data/ext/polars/src/batched_csv.rs +0 -138
- data/ext/polars/src/functions/string_cache.rs +0 -25
- data/ext/polars/src/map/dataframe.rs +0 -338
- data/lib/polars/plot.rb +0 -109
data/lib/polars/io/database.rb
CHANGED
|
@@ -51,16 +51,32 @@ module Polars
|
|
|
51
51
|
when :decimal
|
|
52
52
|
Decimal
|
|
53
53
|
when :float
|
|
54
|
+
# TODO uncomment in future release
|
|
55
|
+
# if column_type.limit && column_type.limit <= 24
|
|
56
|
+
# Float32
|
|
57
|
+
# else
|
|
58
|
+
# Float64
|
|
59
|
+
# end
|
|
54
60
|
Float64
|
|
55
61
|
when :integer
|
|
62
|
+
# TODO uncomment in future release
|
|
63
|
+
# case column_type.limit
|
|
64
|
+
# when 1
|
|
65
|
+
# Int8
|
|
66
|
+
# when 2
|
|
67
|
+
# Int16
|
|
68
|
+
# when 4
|
|
69
|
+
# Int32
|
|
70
|
+
# else
|
|
71
|
+
# Int64
|
|
72
|
+
# end
|
|
56
73
|
Int64
|
|
57
74
|
when :string, :text
|
|
58
75
|
String
|
|
59
76
|
when :time
|
|
60
77
|
Time
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
# Struct
|
|
78
|
+
when :json, :jsonb
|
|
79
|
+
Struct
|
|
64
80
|
end
|
|
65
81
|
|
|
66
82
|
schema_overrides[k] ||= polars_type if polars_type
|
|
@@ -68,6 +84,5 @@ module Polars
|
|
|
68
84
|
|
|
69
85
|
DataFrame.new(data, schema_overrides: schema_overrides)
|
|
70
86
|
end
|
|
71
|
-
alias_method :read_sql, :read_database
|
|
72
87
|
end
|
|
73
88
|
end
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
module Polars
|
|
2
|
+
module IO
|
|
3
|
+
# Reads into a DataFrame from a Delta lake table.
|
|
4
|
+
#
|
|
5
|
+
# @param source [Object]
|
|
6
|
+
# DeltaTable or a Path or URI to the root of the Delta lake table.
|
|
7
|
+
# @param version [Object]
|
|
8
|
+
# Numerical version or timestamp version of the Delta lake table.
|
|
9
|
+
# @param columns [Array]
|
|
10
|
+
# Columns to select. Accepts a list of column names.
|
|
11
|
+
# @param rechunk [Boolean]
|
|
12
|
+
# Make sure that all columns are contiguous in memory by
|
|
13
|
+
# aggregating the chunks into a single array.
|
|
14
|
+
# @param storage_options [Hash]
|
|
15
|
+
# Extra options for the storage backends supported by `deltalake-rb`.
|
|
16
|
+
# @param delta_table_options [Hash]
|
|
17
|
+
# Additional keyword arguments while reading a Delta lake Table.
|
|
18
|
+
#
|
|
19
|
+
# @return [DataFrame]
|
|
20
|
+
def read_delta(
|
|
21
|
+
source,
|
|
22
|
+
version: nil,
|
|
23
|
+
columns: nil,
|
|
24
|
+
rechunk: nil,
|
|
25
|
+
storage_options: nil,
|
|
26
|
+
delta_table_options: nil
|
|
27
|
+
)
|
|
28
|
+
df =
|
|
29
|
+
scan_delta(
|
|
30
|
+
source,
|
|
31
|
+
version: version,
|
|
32
|
+
storage_options: storage_options,
|
|
33
|
+
delta_table_options: delta_table_options,
|
|
34
|
+
rechunk: rechunk
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
if !columns.nil?
|
|
38
|
+
df = df.select(columns)
|
|
39
|
+
end
|
|
40
|
+
df.collect
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Lazily read from a Delta lake table.
|
|
44
|
+
#
|
|
45
|
+
# @param source [Object]
|
|
46
|
+
# DeltaTable or a Path or URI to the root of the Delta lake table.
|
|
47
|
+
# @param version [Object]
|
|
48
|
+
# Numerical version or timestamp version of the Delta lake table.
|
|
49
|
+
# @param storage_options [Hash]
|
|
50
|
+
# Extra options for the storage backends supported by `deltalake-rb`.
|
|
51
|
+
# @param delta_table_options [Hash]
|
|
52
|
+
# Additional keyword arguments while reading a Delta lake Table.
|
|
53
|
+
# @param rechunk [Boolean]
|
|
54
|
+
# Make sure that all columns are contiguous in memory by
|
|
55
|
+
# aggregating the chunks into a single array.
|
|
56
|
+
#
|
|
57
|
+
# @return [LazyFrame]
|
|
58
|
+
def scan_delta(
|
|
59
|
+
source,
|
|
60
|
+
version: nil,
|
|
61
|
+
storage_options: nil,
|
|
62
|
+
delta_table_options: nil,
|
|
63
|
+
rechunk: nil
|
|
64
|
+
)
|
|
65
|
+
dl_tbl =
|
|
66
|
+
_get_delta_lake_table(
|
|
67
|
+
source,
|
|
68
|
+
version: version,
|
|
69
|
+
storage_options: storage_options,
|
|
70
|
+
delta_table_options: delta_table_options
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
dl_tbl.to_polars(eager: false, rechunk: rechunk || false)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
private
|
|
77
|
+
|
|
78
|
+
def _resolve_delta_lake_uri(table_uri, strict: true)
|
|
79
|
+
require "uri"
|
|
80
|
+
|
|
81
|
+
parsed_result = URI(table_uri)
|
|
82
|
+
|
|
83
|
+
resolved_uri =
|
|
84
|
+
if parsed_result.scheme == ""
|
|
85
|
+
Utils.normalize_filepath(table_uri)
|
|
86
|
+
else
|
|
87
|
+
table_uri
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
resolved_uri
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def _get_delta_lake_table(
|
|
94
|
+
table_path,
|
|
95
|
+
version: nil,
|
|
96
|
+
storage_options: nil,
|
|
97
|
+
delta_table_options: nil
|
|
98
|
+
)
|
|
99
|
+
_check_if_delta_available
|
|
100
|
+
|
|
101
|
+
if table_path.is_a?(DeltaLake::Table)
|
|
102
|
+
return table_path
|
|
103
|
+
end
|
|
104
|
+
delta_table_options ||= {}
|
|
105
|
+
resolved_uri = _resolve_delta_lake_uri(table_path)
|
|
106
|
+
if !version.is_a?(::String) && !version.is_a?(::Time)
|
|
107
|
+
dl_tbl =
|
|
108
|
+
DeltaLake::Table.new(
|
|
109
|
+
resolved_uri,
|
|
110
|
+
version: version,
|
|
111
|
+
storage_options: storage_options,
|
|
112
|
+
**delta_table_options
|
|
113
|
+
)
|
|
114
|
+
else
|
|
115
|
+
dl_tbl =
|
|
116
|
+
DeltaLake::Table.new(
|
|
117
|
+
resolved_uri,
|
|
118
|
+
storage_options: storage_options,
|
|
119
|
+
**delta_table_options
|
|
120
|
+
)
|
|
121
|
+
dl_tbl.load_as_version(version)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
dl_tbl = DeltaLake::Table.new(table_path)
|
|
125
|
+
dl_tbl
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def _check_if_delta_available
|
|
129
|
+
if !defined?(DeltaLake)
|
|
130
|
+
raise Error, "Delta Lake not available"
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
module Polars
|
|
2
|
+
module IO
|
|
3
|
+
# Lazily read from an Apache Iceberg table.
|
|
4
|
+
#
|
|
5
|
+
# @param source [Object]
|
|
6
|
+
# A Iceberg Ruby table, or a direct path to the metadata.
|
|
7
|
+
# @param snapshot_id [Integer]
|
|
8
|
+
# The snapshot ID to scan from.
|
|
9
|
+
# @param storage_options [Hash]
|
|
10
|
+
# Extra options for the storage backends.
|
|
11
|
+
#
|
|
12
|
+
# @return [LazyFrame]
|
|
13
|
+
def scan_iceberg(
|
|
14
|
+
source,
|
|
15
|
+
snapshot_id: nil,
|
|
16
|
+
storage_options: nil
|
|
17
|
+
)
|
|
18
|
+
require "iceberg"
|
|
19
|
+
|
|
20
|
+
unless source.is_a?(Iceberg::Table)
|
|
21
|
+
raise Todo
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
dataset =
|
|
25
|
+
IcebergDataset.new(
|
|
26
|
+
source,
|
|
27
|
+
snapshot_id:,
|
|
28
|
+
storage_options:
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
dataset.to_lazyframe
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
data/lib/polars/io/ipc.rb
CHANGED
|
@@ -15,10 +15,10 @@ module Polars
|
|
|
15
15
|
# Only uncompressed IPC files can be memory mapped.
|
|
16
16
|
# @param storage_options [Hash]
|
|
17
17
|
# Extra options that make sense for a particular storage connection.
|
|
18
|
-
# @param
|
|
18
|
+
# @param row_index_name [String]
|
|
19
19
|
# If not nil, this will insert a row count column with give name into the
|
|
20
20
|
# DataFrame.
|
|
21
|
-
# @param
|
|
21
|
+
# @param row_index_offset [Integer]
|
|
22
22
|
# Offset to start the row_count column (only use if the name is set).
|
|
23
23
|
# @param rechunk [Boolean]
|
|
24
24
|
# Make sure that all data is contiguous.
|
|
@@ -30,8 +30,8 @@ module Polars
|
|
|
30
30
|
n_rows: nil,
|
|
31
31
|
memory_map: true,
|
|
32
32
|
storage_options: nil,
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
row_index_name: nil,
|
|
34
|
+
row_index_offset: 0,
|
|
35
35
|
rechunk: true
|
|
36
36
|
)
|
|
37
37
|
storage_options ||= {}
|
|
@@ -40,8 +40,8 @@ module Polars
|
|
|
40
40
|
data,
|
|
41
41
|
columns: columns,
|
|
42
42
|
n_rows: n_rows,
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
row_index_name: row_index_name,
|
|
44
|
+
row_index_offset: row_index_offset,
|
|
45
45
|
rechunk: rechunk,
|
|
46
46
|
memory_map: memory_map
|
|
47
47
|
)
|
|
@@ -53,8 +53,8 @@ module Polars
|
|
|
53
53
|
file,
|
|
54
54
|
columns: nil,
|
|
55
55
|
n_rows: nil,
|
|
56
|
-
|
|
57
|
-
|
|
56
|
+
row_index_name: nil,
|
|
57
|
+
row_index_offset: 0,
|
|
58
58
|
rechunk: true,
|
|
59
59
|
memory_map: true
|
|
60
60
|
)
|
|
@@ -76,7 +76,7 @@ module Polars
|
|
|
76
76
|
columns,
|
|
77
77
|
projection,
|
|
78
78
|
n_rows,
|
|
79
|
-
Utils.parse_row_index_args(
|
|
79
|
+
Utils.parse_row_index_args(row_index_name, row_index_offset),
|
|
80
80
|
memory_map
|
|
81
81
|
)
|
|
82
82
|
Utils.wrap_df(rbdf)
|
|
@@ -182,13 +182,25 @@ module Polars
|
|
|
182
182
|
# Cache the result after reading.
|
|
183
183
|
# @param rechunk [Boolean]
|
|
184
184
|
# Reallocate to contiguous memory when all chunks/ files are parsed.
|
|
185
|
-
# @param
|
|
185
|
+
# @param row_index_name [String]
|
|
186
186
|
# If not nil, this will insert a row count column with give name into the
|
|
187
187
|
# DataFrame.
|
|
188
|
-
# @param
|
|
188
|
+
# @param row_index_offset [Integer]
|
|
189
189
|
# Offset to start the row_count column (only use if the name is set).
|
|
190
|
+
# @param glob [Boolean]
|
|
191
|
+
# Expand path given via globbing rules.
|
|
190
192
|
# @param storage_options [Hash]
|
|
191
193
|
# Extra options that make sense for a particular storage connection.
|
|
194
|
+
# @param credential_provider [Object]
|
|
195
|
+
# Provide a function that can be called to provide cloud storage
|
|
196
|
+
# credentials. The function is expected to return a hash of
|
|
197
|
+
# credential keys along with an optional credential expiry time.
|
|
198
|
+
# @param retries [Integer]
|
|
199
|
+
# Number of retries if accessing a cloud instance fails.
|
|
200
|
+
# @param file_cache_ttl [Integer]
|
|
201
|
+
# Amount of time to keep downloaded cloud files since their last access time,
|
|
202
|
+
# in seconds. Uses the `POLARS_FILE_CACHE_TTL` environment variable
|
|
203
|
+
# (which defaults to 1 hour) if not given.
|
|
192
204
|
# @param hive_partitioning [Boolean]
|
|
193
205
|
# Infer statistics and schema from Hive partitioned URL and use them
|
|
194
206
|
# to prune reads. This is unset by default (i.e. `nil`), meaning it is
|
|
@@ -206,70 +218,58 @@ module Polars
|
|
|
206
218
|
def scan_ipc(
|
|
207
219
|
source,
|
|
208
220
|
n_rows: nil,
|
|
209
|
-
cache:
|
|
210
|
-
rechunk:
|
|
211
|
-
|
|
212
|
-
|
|
221
|
+
cache: nil,
|
|
222
|
+
rechunk: false,
|
|
223
|
+
row_index_name: nil,
|
|
224
|
+
row_index_offset: 0,
|
|
225
|
+
glob: true,
|
|
213
226
|
storage_options: nil,
|
|
227
|
+
credential_provider: "auto",
|
|
228
|
+
retries: nil,
|
|
229
|
+
file_cache_ttl: nil,
|
|
214
230
|
hive_partitioning: nil,
|
|
215
231
|
hive_schema: nil,
|
|
216
232
|
try_parse_hive_dates: true,
|
|
217
|
-
include_file_paths: nil
|
|
233
|
+
include_file_paths: nil,
|
|
234
|
+
_record_batch_statistics: false
|
|
218
235
|
)
|
|
219
|
-
|
|
220
|
-
source,
|
|
221
|
-
n_rows: n_rows,
|
|
222
|
-
cache: cache,
|
|
223
|
-
rechunk: rechunk,
|
|
224
|
-
row_count_name: row_count_name,
|
|
225
|
-
row_count_offset: row_count_offset,
|
|
226
|
-
storage_options: storage_options,
|
|
227
|
-
hive_partitioning: hive_partitioning,
|
|
228
|
-
hive_schema: hive_schema,
|
|
229
|
-
try_parse_hive_dates: try_parse_hive_dates,
|
|
230
|
-
include_file_paths: include_file_paths
|
|
231
|
-
)
|
|
232
|
-
end
|
|
236
|
+
sources = get_sources(source)
|
|
233
237
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
row_count_name: nil,
|
|
241
|
-
row_count_offset: 0,
|
|
242
|
-
storage_options: nil,
|
|
243
|
-
hive_partitioning: nil,
|
|
244
|
-
hive_schema: nil,
|
|
245
|
-
try_parse_hive_dates: true,
|
|
246
|
-
include_file_paths: nil
|
|
247
|
-
)
|
|
248
|
-
sources = []
|
|
249
|
-
if Utils.pathlike?(source)
|
|
250
|
-
source = Utils.normalize_filepath(source)
|
|
251
|
-
elsif source.is_a?(::Array)
|
|
252
|
-
if Utils.is_path_or_str_sequence(source)
|
|
253
|
-
sources = source.map { |s| Utils.normalize_filepath(s) }
|
|
254
|
-
else
|
|
255
|
-
sources = source
|
|
256
|
-
end
|
|
238
|
+
if !retries.nil?
|
|
239
|
+
msg = "the `retries` parameter was deprecated in 0.25.0; specify 'max_retries' in `storage_options` instead."
|
|
240
|
+
Utils.issue_deprecation_warning(msg)
|
|
241
|
+
storage_options = storage_options || {}
|
|
242
|
+
storage_options["max_retries"] = retries
|
|
243
|
+
end
|
|
257
244
|
|
|
258
|
-
|
|
245
|
+
if !file_cache_ttl.nil?
|
|
246
|
+
msg = "the `file_cache_ttl` parameter was deprecated in 0.25.0; specify 'file_cache_ttl' in `storage_options` instead."
|
|
247
|
+
Utils.issue_deprecation_warning(msg)
|
|
248
|
+
storage_options = storage_options || {}
|
|
249
|
+
storage_options["file_cache_ttl"] = file_cache_ttl
|
|
259
250
|
end
|
|
260
251
|
|
|
252
|
+
credential_provider_builder = _init_credential_provider_builder(
|
|
253
|
+
credential_provider, sources, storage_options, "scan_parquet"
|
|
254
|
+
)
|
|
255
|
+
|
|
261
256
|
rblf =
|
|
262
257
|
RbLazyFrame.new_from_ipc(
|
|
263
|
-
source,
|
|
264
258
|
sources,
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
259
|
+
_record_batch_statistics,
|
|
260
|
+
ScanOptions.new(
|
|
261
|
+
row_index: !row_index_name.nil? ? [row_index_name, row_index_offset] : nil,
|
|
262
|
+
pre_slice: !n_rows.nil? ? [0, n_rows] : nil,
|
|
263
|
+
include_file_paths: include_file_paths,
|
|
264
|
+
glob: glob,
|
|
265
|
+
hive_partitioning: hive_partitioning,
|
|
266
|
+
hive_schema: hive_schema,
|
|
267
|
+
try_parse_hive_dates: try_parse_hive_dates,
|
|
268
|
+
rechunk: rechunk,
|
|
269
|
+
cache: cache,
|
|
270
|
+
storage_options: storage_options,
|
|
271
|
+
credential_provider: credential_provider_builder
|
|
272
|
+
)
|
|
273
273
|
)
|
|
274
274
|
Utils.wrap_ldf(rblf)
|
|
275
275
|
end
|
data/lib/polars/io/json.rb
CHANGED
|
@@ -4,6 +4,22 @@ module Polars
|
|
|
4
4
|
#
|
|
5
5
|
# @param source [Object]
|
|
6
6
|
# Path to a file or a file-like object.
|
|
7
|
+
# @param schema [Object]
|
|
8
|
+
# The DataFrame schema may be declared in several ways:
|
|
9
|
+
#
|
|
10
|
+
# * As a hash of \\\\{name:type} pairs; if type is nil, it will be auto-inferred.
|
|
11
|
+
# * As an array of column names; in this case types are automatically inferred.
|
|
12
|
+
# * As an array of [name,type] pairs; this is equivalent to the hash form.
|
|
13
|
+
#
|
|
14
|
+
# If you supply an array of column names that does not match the names in the
|
|
15
|
+
# underlying data, the names given here will overwrite them. The number
|
|
16
|
+
# of names given in the schema should match the underlying data dimensions.
|
|
17
|
+
# @param schema_overrides [Hash]
|
|
18
|
+
# Support type specification or override of one or more columns; note that
|
|
19
|
+
# any dtypes inferred from the schema param will be overridden.
|
|
20
|
+
# @param infer_schema_length [Integer]
|
|
21
|
+
# The maximum number of rows to scan for schema inference.
|
|
22
|
+
# If set to `nil`, the full data may be scanned *(this is slow)*.
|
|
7
23
|
#
|
|
8
24
|
# @return [DataFrame]
|
|
9
25
|
def read_json(
|
|
@@ -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
|