Skip to content

Commit bf8d175

Browse files
authored
GH-49269: [Python][Docs] Add code examples for compute function first/last/first_last (#49270)
### Rationale for this change To improve python documentation ### What changes are included in this PR? Add code examples for compute function first/last/first_last ### Are these changes tested? Yes, doc-test ### Are there any user-facing changes? Yes, doc-only changes * GitHub Issue: #49269 Authored-by: Ruifeng Zheng <ruifengz@apache.org> Signed-off-by: AlenkaF <frim.alenka@gmail.com>
1 parent f2b4e34 commit bf8d175

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

python/pyarrow/_compute_docstrings.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,102 @@
156156
>>> pc.min_max(arr4)
157157
<pyarrow.StructScalar: [('min', 'x'), ('max', 'z')]>
158158
"""
159+
160+
function_doc_additions["first"] = """
161+
Examples
162+
--------
163+
>>> import pyarrow as pa
164+
>>> import pyarrow.compute as pc
165+
>>> arr1 = pa.array([1, 1, 2, 2, 3, 2, 2, 2])
166+
>>> pc.first(arr1)
167+
<pyarrow.Int64Scalar: 1>
168+
169+
Using ``skip_nulls`` to handle null values.
170+
171+
>>> arr2 = pa.array([None, 1.0, 2.0, 3.0])
172+
>>> pc.first(arr2)
173+
<pyarrow.DoubleScalar: 1.0>
174+
>>> pc.first(arr2, skip_nulls=False)
175+
<pyarrow.DoubleScalar: None>
176+
177+
Using ``ScalarAggregateOptions`` to control minimum number of non-null values.
178+
179+
>>> arr3 = pa.array([1.0, None, float("nan"), 3.0])
180+
>>> pc.first(arr3)
181+
<pyarrow.DoubleScalar: 1.0>
182+
>>> pc.first(arr3, options=pc.ScalarAggregateOptions(min_count=3))
183+
<pyarrow.DoubleScalar: 1.0>
184+
>>> pc.first(arr3, options=pc.ScalarAggregateOptions(min_count=4))
185+
<pyarrow.DoubleScalar: None>
186+
187+
See Also
188+
--------
189+
pyarrow.compute.first_last
190+
pyarrow.compute.last
191+
"""
192+
193+
function_doc_additions["last"] = """
194+
Examples
195+
--------
196+
>>> import pyarrow as pa
197+
>>> import pyarrow.compute as pc
198+
>>> arr1 = pa.array([1, 1, 2, 2, 3, 2, 2, 2])
199+
>>> pc.last(arr1)
200+
<pyarrow.Int64Scalar: 2>
201+
202+
Using ``skip_nulls`` to handle null values.
203+
204+
>>> arr2 = pa.array([1.0, 2.0, 3.0, None])
205+
>>> pc.last(arr2)
206+
<pyarrow.DoubleScalar: 3.0>
207+
>>> pc.last(arr2, skip_nulls=False)
208+
<pyarrow.DoubleScalar: None>
209+
210+
Using ``ScalarAggregateOptions`` to control minimum number of non-null values.
211+
212+
>>> arr3 = pa.array([1.0, None, float("nan"), 3.0])
213+
>>> pc.last(arr3)
214+
<pyarrow.DoubleScalar: 3.0>
215+
>>> pc.last(arr3, options=pc.ScalarAggregateOptions(min_count=3))
216+
<pyarrow.DoubleScalar: 3.0>
217+
>>> pc.last(arr3, options=pc.ScalarAggregateOptions(min_count=4))
218+
<pyarrow.DoubleScalar: None>
219+
220+
See Also
221+
--------
222+
pyarrow.compute.first
223+
pyarrow.compute.first_last
224+
"""
225+
226+
function_doc_additions["first_last"] = """
227+
Examples
228+
--------
229+
>>> import pyarrow as pa
230+
>>> import pyarrow.compute as pc
231+
>>> arr1 = pa.array([1, 1, 2, 2, 3, 2, 2, 2])
232+
>>> pc.first_last(arr1)
233+
<pyarrow.StructScalar: [('first', 1), ('last', 2)]>
234+
235+
Using ``skip_nulls`` to handle null values.
236+
237+
>>> arr2 = pa.array([None, 2.0, 3.0, None])
238+
>>> pc.first_last(arr2)
239+
<pyarrow.StructScalar: [('first', 2.0), ('last', 3.0)]>
240+
>>> pc.first_last(arr2, skip_nulls=False)
241+
<pyarrow.StructScalar: [('first', None), ('last', None)]>
242+
243+
Using ``ScalarAggregateOptions`` to control minimum number of non-null values.
244+
245+
>>> arr3 = pa.array([1.0, None, float("nan"), 3.0])
246+
>>> pc.first_last(arr3)
247+
<pyarrow.StructScalar: [('first', 1.0), ('last', 3.0)]>
248+
>>> pc.first_last(arr3, options=pc.ScalarAggregateOptions(min_count=3))
249+
<pyarrow.StructScalar: [('first', 1.0), ('last', 3.0)]>
250+
>>> pc.first_last(arr3, options=pc.ScalarAggregateOptions(min_count=4))
251+
<pyarrow.StructScalar: [('first', None), ('last', None)]>
252+
253+
See Also
254+
--------
255+
pyarrow.compute.first
256+
pyarrow.compute.last
257+
"""

0 commit comments

Comments
 (0)