PythonSTB commited on
Commit
5639c73
·
verified ·
1 Parent(s): b858a33

Delete pyarrow/PYARROW_USER_GUIDE.txt

Browse files
Files changed (1) hide show
  1. pyarrow/PYARROW_USER_GUIDE.txt +0 -402
pyarrow/PYARROW_USER_GUIDE.txt DELETED
@@ -1,402 +0,0 @@
1
- ================================================================================
2
- PYARROW - USER GUIDE (Android Python STB) - Generated by RIMI
3
- ================================================================================
4
- Covers: what pyarrow is, install/verify, arrays, tables, types, IPC,
5
- CSV, JSON, Feather, compute, filesystem, and Android-specific notes.
6
-
7
- Written for: Python 3.12 (RIMI build) on Android
8
- Version: pyarrow 25.0.1 (Arrow C++ 25.0.1)
9
- Scripts dir: /storage/emulated/0/PythonSTB/Scripts/
10
- Installed: /data/user/0/com.pythonstb.rimi/files/python/lib/python3.12/site-packages/
11
- ================================================================================
12
-
13
-
14
- --------------------------------------------------------------------------------
15
- 1) WHAT IS PYARROW?
16
- --------------------------------------------------------------------------------
17
-
18
- PyArrow is the Python bindings for Apache Arrow — a cross-language development
19
- platform for in-memory columnar data. It provides:
20
-
21
- - Apache Arrow arrays and tables (columnar memory format)
22
- - Zero-copy data interchange between Python, pandas, and other languages
23
- - IPC (Inter-Process Communication) for fast serialization
24
- - CSV, JSON, and Feather file readers/writers
25
- - Compute kernels (sum, mean, cast, filter, sort, etc.)
26
- - A local filesystem abstraction
27
-
28
- Arrow is the foundation behind many modern data tools:
29
- pandas 2.x uses Arrow as its default backend
30
- Polars, DuckDB, and Spark all speak Arrow natively
31
-
32
- import pyarrow as pa
33
- print(pa.__version__) # 25.0.1
34
- print(pa.cpp_version) # 25.0.1
35
-
36
-
37
- --------------------------------------------------------------------------------
38
- 2) INSTALL / VERIFY
39
- --------------------------------------------------------------------------------
40
-
41
- Install:
42
- pip install pyarrow-25.0.1-cp312-cp312-android_24_x86_64.whl
43
- or: pyarrow-25.0.1-cp312-cp312-android_24_arm64_v8a.whl
44
-
45
- Quick smoke test:
46
- import pyarrow as pa
47
- a = pa.array([1, 2, 3])
48
- print(a) # [<pyarrow.Int64Scalar: 1>, ...]
49
- print("version:", pa.__version__)
50
- print("cpp:", pa.cpp_version)
51
-
52
- Expected output:
53
- [1, 2, 3]
54
- version: 25.0.1
55
- cpp: 25.0.1
56
-
57
-
58
- --------------------------------------------------------------------------------
59
- 3) ARRAYS - CREATION BASICS
60
- --------------------------------------------------------------------------------
61
-
62
- import pyarrow as pa
63
-
64
- # from Python lists
65
- a = pa.array([1, 2, 3]) # int64
66
- b = pa.array([1.0, 2.5, 3.7]) # float64
67
- c = pa.array(["hello", "world"]) # string
68
- d = pa.array([True, False, True]) # bool
69
-
70
- # explicit type
71
- e = pa.array([1, 2, 3], type=pa.int32())
72
- f = pa.array([1, 2, 3], type=pa.float64())
73
-
74
- # with nulls
75
- g = pa.array([1, None, 3], type=pa.int64())
76
- g.null_count # 1
77
-
78
- # from numpy
79
- import numpy as np
80
- h = pa.array(np.arange(10)) # numpy -> Arrow (zero-copy for numeric types)
81
-
82
- # important attributes
83
- a.type # DataType: int64
84
- a.dtype # same as .type
85
- len(a) # 3
86
- a.to_pylist() # [1, 2, 3]
87
- a.as_py() # [1, 2, 3] (same for scalar)
88
-
89
-
90
- --------------------------------------------------------------------------------
91
- 4) TYPES
92
- --------------------------------------------------------------------------------
93
-
94
- pa.int8() pa.int16() pa.int32() pa.int64()
95
- pa.uint8() pa.uint16() pa.uint32() pa.uint64()
96
- pa.float16() pa.float32() pa.float64()
97
- pa.bool_()
98
- pa.string() pa.large_string()
99
- pa.binary() pa.large_binary()
100
- pa.date32() pa.date64()
101
- pa.timestamp("ns") # nanosecond timestamp
102
- pa.duration("ms") # millisecond duration
103
- pa.null() # all-null type
104
- pa.struct([pa.field("x", pa.int64()), pa.field("y", pa.string())])
105
-
106
- # check type
107
- a = pa.array([1, 2])
108
- a.type == pa.int64() # True
109
- isinstance(a.type, pa.DataType) # True
110
-
111
- # convert between types
112
- a = pa.array([1, 2, 3], type=pa.int64())
113
- b = a.cast(pa.float64()) # explicit cast
114
- c = a.cast(pa.int32()) # downcast
115
-
116
-
117
- --------------------------------------------------------------------------------
118
- 5) TABLES
119
- --------------------------------------------------------------------------------
120
-
121
- import pyarrow as pa
122
-
123
- # from dict
124
- t = pa.table({
125
- "id": [1, 2, 3],
126
- "name": ["alice", "bob", "charlie"],
127
- "score": [95.5, 87.0, 92.3],
128
- })
129
-
130
- t.num_rows # 3
131
- t.num_columns # 3
132
- t.column_names # ["id", "name", "score"]
133
- t.schema # id: int64, name: string, score: float64
134
-
135
- # access columns
136
- t.column("id") # <pyarrow.lib.ChunkedArray ...>
137
- t.column("id").to_pylist() # [1, 2, 3]
138
-
139
- # convert to pandas
140
- df = t.to_pandas()
141
- print(df)
142
- # id name score
143
- # 0 1 alice 95.5
144
- # 1 2 bob 87.0
145
- # 2 3 charlie 92.3
146
-
147
- # from pandas
148
- t2 = pa.Table.from_pandas(df)
149
-
150
- # slice
151
- t.slice(0, 2) # first 2 rows
152
-
153
- # combine tables
154
- t3 = pa.concat_tables([t, t])
155
-
156
- # sort
157
- t.sort_by("score", descending=True)
158
-
159
-
160
- --------------------------------------------------------------------------------
161
- 6) IPC (Inter-Process Communication)
162
- --------------------------------------------------------------------------------
163
-
164
- IPC is Arrow's fast binary serialization format — much faster than CSV or JSON.
165
-
166
- import pyarrow as pa
167
- import pyarrow.ipc as ipc
168
-
169
- t = pa.table({"x": [1, 2, 3], "y": ["a", "b", "c"]})
170
-
171
- # --- File format (random access) ---
172
- path = "/storage/emulated/0/Download/data.arrow"
173
- writer = ipc.new_file(path, t.schema)
174
- writer.write_table(t)
175
- writer.close()
176
-
177
- reader = ipc.open_file(path)
178
- t2 = reader.read_all()
179
- assert t.equals(t2)
180
-
181
- # --- Stream format (sequential access, smaller header) ---
182
- path2 = "/storage/emulated/0/Download/data_stream.arrow"
183
- writer = ipc.new_stream(path2, t.schema)
184
- writer.write_table(t)
185
- writer.close()
186
-
187
- reader = ipc.open_stream(path2)
188
- t3 = reader.read_all()
189
- assert t.equals(t3)
190
-
191
- # --- In-memory IPC (for zero-copy between processes) ---
192
- sink = ipc.BufferOutputStream()
193
- writer = ipc.new_stream(sink, t.schema)
194
- writer.write_table(t)
195
- writer.close()
196
- buf = sink.getvalue().to_pybytes() # bytes
197
- # send buf over socket / pipe, then:
198
- reader = ipc.open_stream(pa.BufferReader(buf))
199
- t4 = reader.read_all()
200
-
201
-
202
- --------------------------------------------------------------------------------
203
- 7) CSV
204
- --------------------------------------------------------------------------------
205
-
206
- import pyarrow as pa
207
- import pyarrow.csv as pcsv
208
-
209
- t = pa.table({"a": [1, 2], "b": [3.0, 4.0]})
210
-
211
- # write
212
- pcsv.write_csv(t, "/storage/emulated/0/Download/data.csv")
213
-
214
- # read (basic)
215
- t2 = pcsv.read_csv("/storage/emulated/0/Download/data.csv")
216
-
217
- # read with options
218
- read_opts = pcsv.ReadOptions(column_names=["x", "y"])
219
- convert_opts = pcsv.ConvertOptions(
220
- column_types={"x": pa.int64(), "y": pa.float64()},
221
- null_values=["NA", "NULL"],
222
- )
223
- t3 = pcsv.read_csv(
224
- "/storage/emulated/0/Download/data.csv",
225
- read_options=read_opts,
226
- convert_options=convert_opts,
227
- )
228
-
229
- # read as pandas directly
230
- df = pcsv.read_csv("/storage/emulated/0/Download/data.csv").to_pandas()
231
-
232
-
233
- --------------------------------------------------------------------------------
234
- 8) JSON
235
- --------------------------------------------------------------------------------
236
-
237
- import pyarrow as pa
238
- import pyarrow.json as pjson
239
-
240
- # JSON Lines (one JSON object per line) — preferred for tabular data
241
- # file content:
242
- # {"a": 1, "b": "hello"}
243
- # {"a": 2, "b": "world"}
244
-
245
- t = pjson.read_json("/storage/emulated/0/Download/data.json")
246
- # t: 2 rows, columns ["a", "b"]
247
- # types inferred automatically (a: int64, b: string)
248
-
249
-
250
- --------------------------------------------------------------------------------
251
- 9) FEATHER (fast columnar format)
252
- --------------------------------------------------------------------------------
253
-
254
- Feather is Arrow's columnar format optimized for pandas read/write.
255
- Faster than CSV, smaller than IPC for single-table files.
256
-
257
- import pyarrow as pa
258
- import pyarrow.feather as pf
259
-
260
- t = pa.table({"x": [1, 2, 3], "y": [4.0, 5.0, 6.0]})
261
-
262
- # write
263
- pf.write_feather(t, "/storage/emulated/0/Download/data.feather")
264
-
265
- # read
266
- t2 = pf.read_feather("/storage/emulated/0/Download/data.feather")
267
- assert t.equals(t2)
268
-
269
- # read as pandas
270
- df = pf.read_feather("/storage/emulated/0/Download/data.feather")
271
-
272
- # older Feather v1 format also supported (pandas read_feather兼容)
273
-
274
-
275
- --------------------------------------------------------------------------------
276
- 10) COMPUTE FUNCTIONS
277
- --------------------------------------------------------------------------------
278
-
279
- import pyarrow as pa
280
- import pyarrow.compute as pc
281
-
282
- a = pa.array([1, 2, 3, 4, 5])
283
-
284
- # aggregations
285
- pc.sum(a) # 15
286
- pc.mean(a) # 3.0
287
- pc.min(a) # 1
288
- pc.max(a) # 5
289
- pc.count(a) # 5
290
-
291
- # element-wise
292
- pc.add(a, pa.array([10, 10, 10, 10, 10]))
293
- pc.multiply(a, pa.scalar(2))
294
- pc.sqrt(a.cast(pa.float64()))
295
- pc.abs(pa.array([-1, 2, -3]))
296
-
297
- # comparison / filtering
298
- mask = pc.greater(a, 3) # [False, False, False, True, True]
299
- pc.filter(a, mask) # [4, 5]
300
- pc.sum(mask.as_py()) if hasattr(mask, 'as_py') else pc.sum(mask) # 2
301
-
302
- # casting
303
- b = pc.cast(a, to=pa.float64())
304
- c = pc.cast(pa.array(["1", "2", "3"]), to=pa.int64())
305
-
306
- # sort
307
- pc.sort(pa.array([3, 1, 4, 1, 5])) # [1, 1, 3, 4, 5]
308
-
309
- # unique / value_counts
310
- pc.unique(pa.array([1, 2, 1, 3, 2])) # [1, 2, 3]
311
- pc.value_counts(pa.array([1, 1, 2])) # [{values: [1,2], counts: [2,1]}]
312
-
313
- # string ops (via compute)
314
- names = pa.array(["alice", "BOB", "charlie"])
315
- pc.upper(names) # ["ALICE", "BOB", "CHARLIE"]
316
- pc.utf8_length(names) # [5, 3, 7]
317
- pc.utf8_lower(names) # ["alice", "bob", "charlie"]
318
-
319
-
320
- --------------------------------------------------------------------------------
321
- 11) FILESYSTEM
322
- --------------------------------------------------------------------------------
323
-
324
- import pyarrow.fs as pfs
325
-
326
- # local filesystem
327
- local = pfs.LocalFileSystem()
328
-
329
- # list directory
330
- local.get_file_info(pfs.FileSelector("/storage/emulated/0/Download"))
331
-
332
- # file info
333
- meta = local.get_file_info("/storage/emulated/0/Download/data.csv")
334
- meta.type # FileType.File
335
- meta.size # bytes
336
- meta.mtime # modification time
337
-
338
- # read / write
339
- with local.open_output_stream("/storage/emulated/0/Download/test.txt") as f:
340
- f.write(b"hello arrow")
341
- with local.open_input_stream("/storage/emulated/0/Download/test.txt") as f:
342
- content = f.read()
343
- # content == b"hello arrow"
344
-
345
- # create / delete
346
- local.create_dir("/storage/emulated/0/Download/testdir")
347
- local.delete_dir("/storage/emulated/0/Download/testdir")
348
-
349
-
350
- --------------------------------------------------------------------------------
351
- 12) ANDROID-SPECIFIC NOTES
352
- --------------------------------------------------------------------------------
353
-
354
- - libarrow_python.so preload:
355
- The __init__.py is patched to call ctypes.CDLL("libarrow_python.so") before
356
- importing any Cython modules. This is required because Android's linker
357
- cannot find shared libraries the same way as Linux desktop. The preload
358
- ensures symbols from libarrow_python.so are available when the .so extension
359
- modules load.
360
-
361
- - Static Arrow C++:
362
- Arrow C++ (libarrow.a, libarrow_compute.a) is linked statically into
363
- pyarrow's .so files. Only libarrow_python.so is a separate shared library
364
- that needs preloading.
365
-
366
- - Dependencies:
367
- Only numpy is required. Install numpy first before using pyarrow.
368
-
369
- - Wheels are tagged android_24_arm64_v8a and android_24_x86_64.
370
- Install the wheel matching your device ABI:
371
- arm64 phone -> android_24_arm64_v8a
372
- x86_64 emulator -> android_24_x86_64
373
-
374
- - Temp files for IPC/CSV/Feather:
375
- Write to /storage/emulated/0/Download/ or use tempfile.gettempdir().
376
-
377
-
378
- --------------------------------------------------------------------------------
379
- 13) TESTED FEATURES
380
- --------------------------------------------------------------------------------
381
-
382
- v import pyarrow 25.0.1
383
- v import numpy (dependency)
384
- v import submodules: compute, csv, json, feather, fs, ipc
385
- v array creation: int64, float64, string
386
- v null handling
387
- v type system: int64, float64, string, bool
388
- v table creation, schema inspection
389
- v table column access + to_pandas
390
- v IPC file round-trip
391
- v IPC stream round-trip
392
- v CSV write / read
393
- v CSV custom options (column_names, column_types)
394
- v JSON read
395
- v Feather round-trip
396
- v compute: sum, cast, filter, add, multiply
397
- v LocalFileSystem get_file_info
398
-
399
-
400
- ================================================================================
401
- Generated by RIMI
402
- ================================================================================