forked from data-apis/dataframe-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolumn_object.py
656 lines (532 loc) · 19.1 KB
/
column_object.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
from __future__ import annotations
from typing import Any,NoReturn, Sequence, TYPE_CHECKING, Literal
if TYPE_CHECKING:
from ._types import Scalar
from . import DType
__all__ = ['Column']
class Column:
"""
Column object
Note that this column object is not meant to be instantiated directly by
users of the library implementing the dataframe API standard. Rather, use
constructor functions or an already-created dataframe object retrieved via
"""
def __column_namespace__(
self: Column, /, *, api_version: str | None = None
) -> Any:
"""
Returns an object that has all the Dataframe Standard API functions on it.
Parameters
----------
api_version: Optional[str]
String representing the version of the dataframe API specification
to be returned, in ``'YYYY.MM'`` form, for example, ``'2023.04'``.
If it is ``None``, it should return the namespace corresponding to
latest version of the dataframe API specification. If the given
version is invalid or not implemented for the given module, an
error should be raised. Default: ``None``.
Returns
-------
namespace: Any
An object representing the dataframe API namespace. It should have
every top-level function defined in the specification as an
attribute. It may contain other public names as well, but it is
recommended to only include those names that are part of the
specification.
"""
@property
def column(self) -> object:
"""
Return underlying (not-necessarily-Standard-compliant) column.
If a library only implements the Standard, then this can return `self`.
"""
...
def __len__(self) -> int:
"""
Return the number of rows.
"""
def __iter__(self) -> NoReturn:
"""
Iterate over elements.
This is intentionally "poisoned" to discourage inefficient code patterns.
Raises
------
NotImplementedError
"""
raise NotImplementedError("'__iter__' is intentionally not implemented.")
@property
def dtype(self) -> DType:
"""
Return data type of column.
"""
def get_rows(self, indices: Column[int]) -> Column:
"""
Select a subset of rows, similar to `ndarray.take`.
Parameters
----------
indices : Column[int]
Positions of rows to select.
"""
...
def get_value(self, row_number: int) -> Scalar:
"""
Select the value at a row number, similar to `ndarray.__getitem__(<int>)`.
Parameters
----------
row_number : int
Row number of value to return.
Returns
-------
Scalar
Depends on the dtype of the Column, and may vary
across implementations.
"""
...
def sorted_indices(
self,
*,
ascending: bool = True,
nulls_position: Literal['first', 'last'] = 'last',
) -> Column[int]:
"""
Return row numbers which would sort column.
If you need to sort the Column, you can simply do::
col.get_rows(col.sorted_indices())
Parameters
----------
ascending : bool
If `True`, sort in ascending order.
If `False`, sort in descending order.
nulls_position : ``{'first', 'last'}``
Whether null values should be placed at the beginning
or at the end of the result.
Note that the position of NaNs is unspecified and may
vary based on the implementation.
Returns
-------
Column[int]
"""
...
def __eq__(self, other: Column | Scalar) -> Column:
"""
Compare for equality.
Nulls should follow Kleene Logic.
Parameters
----------
other : Column or Scalar
If Column, must have same length.
"Scalar" here is defined implicitly by what scalar types are allowed
for the operation by the underling dtypes.
Returns
-------
Column
"""
def __ne__(self, other: Column | Scalar) -> Column:
"""
Compare for non-equality.
Nulls should follow Kleene Logic.
Parameters
----------
other : Column or Scalar
If Column, must have same length.
"Scalar" here is defined implicitly by what scalar types are allowed
for the operation by the underling dtypes.
Returns
-------
Column
"""
def __ge__(self, other: Column | Scalar) -> Column:
"""
Compare for "greater than or equal to" `other`.
Parameters
----------
other : Column or Scalar
If Column, must have same length.
"Scalar" here is defined implicitly by what scalar types are allowed
for the operation by the underling dtypes.
Returns
-------
Column
"""
def __gt__(self, other: Column | Scalar) -> Column:
"""
Compare for "greater than" `other`.
Parameters
----------
other : Column or Scalar
If Column, must have same length.
"Scalar" here is defined implicitly by what scalar types are allowed
for the operation by the underling dtypes.
Returns
-------
Column
"""
def __le__(self, other: Column | Scalar) -> Column:
"""
Compare for "less than or equal to" `other`.
Parameters
----------
other : Column or Scalar
If Column, must have same length.
"Scalar" here is defined implicitly by what scalar types are allowed
for the operation by the underling dtypes.
Returns
-------
Column
"""
def __lt__(self, other: Column | Scalar) -> Column:
"""
Compare for "less than" `other`.
Parameters
----------
other : Column or Scalar
If Column, must have same length.
"Scalar" here is defined implicitly by what scalar types are allowed
for the operation by the underling dtypes.
Returns
-------
Column
"""
def __and__(self, other: Column[bool] | bool) -> Column[bool]:
"""
Apply logical 'and' to `other` Column (or scalar) and this Column.
Nulls should follow Kleene Logic.
Parameters
----------
other : Column[bool] or bool
If Column, must have same length.
Returns
-------
Column
Raises
------
ValueError
If `self` or `other` is not boolean.
"""
def __or__(self, other: Column[bool] | bool) -> Column[bool]:
"""
Apply logical 'or' to `other` Column (or scalar) and this column.
Nulls should follow Kleene Logic.
Parameters
----------
other : Column[bool] or Scalar
If Column, must have same length.
Returns
-------
Column[bool]
Raises
------
ValueError
If `self` or `other` is not boolean.
"""
def __add__(self, other: Column | Scalar) -> Column:
"""
Add `other` column or scalar to this column.
Parameters
----------
other : Column or Scalar
If Column, must have same length.
"Scalar" here is defined implicitly by what scalar types are allowed
for the operation by the underling dtypes.
Returns
-------
Column
"""
def __sub__(self, other: Column | Scalar) -> Column:
"""
Subtract `other` column or scalar from this column.
Parameters
----------
other : Column or Scalar
If Column, must have same length.
"Scalar" here is defined implicitly by what scalar types are allowed
for the operation by the underling dtypes.
Returns
-------
Column
"""
def __mul__(self, other: Column | Scalar) -> Column:
"""
Multiply `other` column or scalar with this column.
Parameters
----------
other : Column or Scalar
If Column, must have same length.
"Scalar" here is defined implicitly by what scalar types are allowed
for the operation by the underling dtypes.
Returns
-------
Column
"""
def __truediv__(self, other: Column | Scalar) -> Column:
"""
Divide this column by `other` column or scalar. True division, returns floats.
Parameters
----------
other : Column or Scalar
If Column, must have same length.
"Scalar" here is defined implicitly by what scalar types are allowed
for the operation by the underling dtypes.
Returns
-------
Column
"""
def __floordiv__(self, other: Column | Scalar) -> Column:
"""
Floor-divide `other` column or scalar to this column.
Parameters
----------
other : Column or Scalar
If Column, must have same length.
"Scalar" here is defined implicitly by what scalar types are allowed
for the operation by the underling dtypes.
Returns
-------
Column
"""
def __pow__(self, other: Column | Scalar) -> Column:
"""
Raise this column to the power of `other`.
Integer dtype to the power of non-negative integer dtype is integer dtype.
Integer dtype to the power of float dtype is float dtype.
Float dtype to the power of integer dtype or float dtype is float dtype.
Parameters
----------
other : Column or Scalar
If Column, must have same length.
"Scalar" here is defined implicitly by what scalar types are allowed
for the operation by the underling dtypes.
Returns
-------
Column
"""
def __mod__(self, other: Column | Scalar) -> Column:
"""
Returns modulus of this column by `other` (`%` operator).
Parameters
----------
other : Column or Scalar
If Column, must have same length.
"Scalar" here is defined implicitly by what scalar types are allowed
for the operation by the underling dtypes.
Returns
-------
Column
"""
def __divmod__(self, other: Column | Scalar) -> tuple[Column, Column]:
"""
Return quotient and remainder of integer division. See `divmod` builtin function.
Parameters
----------
other : Column or Scalar
If Column, must have same length.
"Scalar" here is defined implicitly by what scalar types are allowed
for the operation by the underling dtypes.
Returns
-------
Column
"""
def __invert__(self) -> Column:
"""
Invert truthiness of (boolean) elements.
Raises
------
ValueError
If any of the Column's columns is not boolean.
"""
def any(self, *, skip_nulls: bool = True) -> bool:
"""
Reduction returns a bool.
Raises
------
ValueError
If column is not boolean.
"""
def all(self, *, skip_nulls: bool = True) -> bool:
"""
Reduction returns a bool.
Raises
------
ValueError
If column is not boolean.
"""
def min(self, *, skip_nulls: bool = True) -> Scalar:
"""
Reduction returns a scalar. Any data type that supports comparisons
must be supported. The returned value has the same dtype as the column.
"""
def max(self, *, skip_nulls: bool = True) -> Scalar:
"""
Reduction returns a scalar. Any data type that supports comparisons
must be supported. The returned value has the same dtype as the column.
"""
def sum(self, *, skip_nulls: bool = True) -> Scalar:
"""
Reduction returns a scalar. Must be supported for numerical and
datetime data types. The returned value has the same dtype as the
column.
"""
def prod(self, *, skip_nulls: bool = True) -> Scalar:
"""
Reduction returns a scalar. Must be supported for numerical data types.
The returned value has the same dtype as the column.
"""
def median(self, *, skip_nulls: bool = True) -> Scalar:
"""
Reduction returns a scalar. Must be supported for numerical and
datetime data types. Returns a float for numerical data types, and
datetime (with the appropriate timedelta format string) for datetime
dtypes.
"""
def mean(self, *, skip_nulls: bool = True) -> Scalar:
"""
Reduction returns a scalar. Must be supported for numerical and
datetime data types. Returns a float for numerical data types, and
datetime (with the appropriate timedelta format string) for datetime
dtypes.
"""
def std(self, *, correction: int | float = 1, skip_nulls: bool = True) -> Scalar:
"""
Reduction returns a scalar. Must be supported for numerical and
datetime data types. Returns a float for numerical data types, and
datetime (with the appropriate timedelta format string) for datetime
dtypes.
Parameters
----------
correction
Degrees of freedom adjustment. Setting this parameter to a value other
than ``0`` has the effect of adjusting the divisor during the
calculation of the standard deviation according to ``N-correction``,
where ``N`` corresponds to the total number of elements over which
the standard deviation is computed. When computing the standard
deviation of a population, setting this parameter to ``0`` is the
standard choice (i.e., the provided column contains data
constituting an entire population). When computing the corrected
sample standard deviation, setting this parameter to ``1`` is the
standard choice (i.e., the provided column contains data sampled
from a larger population; this is commonly referred to as Bessel's
correction). Fractional (float) values are allowed. Default: ``1``.
skip_nulls
Whether to skip null values.
"""
def var(self, *, correction: int | float = 1, skip_nulls: bool = True) -> Scalar:
"""
Reduction returns a scalar. Must be supported for numerical and
datetime data types. Returns a float for numerical data types, and
datetime (with the appropriate timedelta format string) for datetime
dtypes.
Parameters
----------
correction
Correction to apply to the result. For example, ``0`` for sample
standard deviation and ``1`` for population standard deviation.
See `Column.std` for a more detailed description.
skip_nulls
Whether to skip null values.
"""
def cumulative_max(self) -> Column:
"""
Reduction returns a Column. Any data type that supports comparisons
must be supported. The returned value has the same dtype as the column.
"""
def cumulative_min(self) -> Column:
"""
Reduction returns a Column. Any data type that supports comparisons
must be supported. The returned value has the same dtype as the column.
"""
def cumulative_sum(self) -> Column:
"""
Reduction returns a Column. Must be supported for numerical and
datetime data types. The returned value has the same dtype as the
column.
"""
def cumulative_prod(self) -> Column:
"""
Reduction returns a Column. Must be supported for numerical and
datetime data types. The returned value has the same dtype as the
column.
"""
def is_null(self) -> Column:
"""
Check for 'missing' or 'null' entries.
Returns
-------
Column
See also
--------
is_nan
Notes
-----
Does *not* include NaN-like entries.
May optionally include 'NaT' values (if present in an implementation),
but note that the Standard makes no guarantees about them.
"""
def is_nan(self) -> Column:
"""
Check for nan entries.
Returns
-------
Column
See also
--------
is_null
Notes
-----
This only checks for 'NaN'.
Does *not* include 'missing' or 'null' entries.
In particular, does not check for `np.timedelta64('NaT')`.
"""
def is_in(self, values: Column) -> Column[bool]:
"""
Indicate whether the value at each row matches any value in `values`.
Parameters
----------
values : Column
Contains values to compare against. May include ``float('nan')`` and
``null``, in which case ``'nan'`` and ``null`` will
respectively return ``True`` even though ``float('nan') == float('nan')``
isn't ``True``.
The dtype of ``values`` must match the current column's dtype.
Returns
-------
Column[bool]
"""
def unique_indices(self, *, skip_nulls: bool = True) -> Column[int]:
"""
Return indices corresponding to unique values in Column.
Returns
-------
Column[int]
Indices corresponding to unique values.
Notes
-----
There are no ordering guarantees. In particular, if there are multiple
indices corresponding to the same unique value, there is no guarantee
about which one will appear in the result.
If the original Column contains multiple `'NaN'` values, then
only a single index corresponding to those values will be returned.
Likewise for null values (if ``skip_nulls=False``).
To get the unique values, you can do ``col.get_rows(col.unique_indices())``.
"""
...
def fill_nan(self, value: float | 'null', /) -> Column:
"""
Fill floating point ``nan`` values with the given fill value.
Parameters
----------
value : float or `null`
Value used to replace any ``nan`` in the column with. Must be
of the Python scalar type matching the dtype of the column (or
be `null`).
"""
...
def fill_null(self, value: Scalar, /) -> Column:
"""
Fill null values with the given fill value.
Parameters
----------
value : Scalar
Value used to replace any ``null`` values in the column with.
Must be of the Python scalar type matching the dtype of the column.
"""
...