/
general.py
817 lines (737 loc) · 22.9 KB
/
general.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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
# Licensed to Modin Development Team under one or more contributor license agreements.
# See the NOTICE file distributed with this work for additional information regarding
# copyright ownership. The Modin Development Team licenses this file to you under the
# Apache License, Version 2.0 (the "License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific language
# governing permissions and limitations under the License.
"""Implement pandas general API."""
from __future__ import annotations
import warnings
from typing import Hashable, Iterable, Mapping, Optional, Union
import numpy as np
import pandas
from pandas._libs.lib import NoDefault, no_default
from pandas._typing import ArrayLike, DtypeBackend, Scalar, npt
from pandas.core.dtypes.common import is_list_like
from modin.core.storage_formats import BaseQueryCompiler
from modin.error_message import ErrorMessage
from modin.logging import enable_logging
from modin.pandas.io import to_pandas
from modin.utils import _inherit_docstrings
from .base import BasePandasDataset
from .dataframe import DataFrame
from .series import Series
@_inherit_docstrings(pandas.isna, apilink="pandas.isna")
@enable_logging
def isna(
obj,
) -> bool | npt.NDArray[np.bool_] | Series | DataFrame: # noqa: PR01, RT01, D200
"""
Detect missing values for an array-like object.
"""
if isinstance(obj, BasePandasDataset):
return obj.isna()
else:
return pandas.isna(obj)
isnull = isna
@_inherit_docstrings(pandas.notna, apilink="pandas.notna")
@enable_logging
def notna(
obj,
) -> bool | npt.NDArray[np.bool_] | Series | DataFrame: # noqa: PR01, RT01, D200
"""
Detect non-missing values for an array-like object.
"""
if isinstance(obj, BasePandasDataset):
return obj.notna()
else:
return pandas.notna(obj)
notnull = notna
@_inherit_docstrings(pandas.merge, apilink="pandas.merge")
@enable_logging
def merge(
left,
right,
how: str = "inner",
on=None,
left_on=None,
right_on=None,
left_index: bool = False,
right_index: bool = False,
sort: bool = False,
suffixes=("_x", "_y"),
copy: Optional[bool] = None,
indicator: bool = False,
validate=None,
) -> DataFrame: # noqa: PR01, RT01, D200
"""
Merge DataFrame or named Series objects with a database-style join.
"""
if isinstance(left, Series):
if left.name is None:
raise ValueError("Cannot merge a Series without a name")
else:
left = left.to_frame()
if not isinstance(left, DataFrame):
raise TypeError(
f"Can only merge Series or DataFrame objects, a {type(left)} was passed"
)
return left.merge(
right,
how=how,
on=on,
left_on=left_on,
right_on=right_on,
left_index=left_index,
right_index=right_index,
sort=sort,
suffixes=suffixes,
copy=copy,
indicator=indicator,
validate=validate,
)
@_inherit_docstrings(pandas.merge_ordered, apilink="pandas.merge_ordered")
@enable_logging
def merge_ordered(
left,
right,
on=None,
left_on=None,
right_on=None,
left_by=None,
right_by=None,
fill_method=None,
suffixes=("_x", "_y"),
how: str = "outer",
) -> DataFrame: # noqa: PR01, RT01, D200
"""
Perform a merge for ordered data with optional filling/interpolation.
"""
for operand in (left, right):
if not isinstance(operand, (Series, DataFrame)):
raise TypeError(
f"Can only merge Series or DataFrame objects, a {type(operand)} was passed"
)
return DataFrame(
query_compiler=left._query_compiler.merge_ordered(
right._query_compiler,
on=on,
left_on=left_on,
right_on=right_on,
left_by=left_by,
right_by=right_by,
fill_method=fill_method,
suffixes=suffixes,
how=how,
)
)
@_inherit_docstrings(pandas.merge_asof, apilink="pandas.merge_asof")
@enable_logging
def merge_asof(
left,
right,
on=None,
left_on=None,
right_on=None,
left_index: bool = False,
right_index: bool = False,
by=None,
left_by=None,
right_by=None,
suffixes=("_x", "_y"),
tolerance=None,
allow_exact_matches: bool = True,
direction: str = "backward",
) -> DataFrame: # noqa: PR01, RT01, D200
"""
Perform a merge by key distance.
"""
if not isinstance(left, DataFrame):
raise ValueError(
"can not merge DataFrame with instance of type {}".format(type(right))
)
ErrorMessage.default_to_pandas("`merge_asof`")
# As of Pandas 1.2 these should raise an error; before that it did
# something likely random:
if (
(on and (left_index or right_index))
or (left_on and left_index)
or (right_on and right_index)
):
raise ValueError("Can't combine left/right_index with left/right_on or on.")
if on is not None:
if left_on is not None or right_on is not None:
raise ValueError("If 'on' is set, 'left_on' and 'right_on' can't be set.")
left_on = on
right_on = on
if by is not None:
if left_by is not None or right_by is not None:
raise ValueError("Can't have both 'by' and 'left_by' or 'right_by'")
left_by = right_by = by
if left_on is None and not left_index:
raise ValueError("Must pass on, left_on, or left_index=True")
if right_on is None and not right_index:
raise ValueError("Must pass on, right_on, or right_index=True")
return DataFrame(
query_compiler=left._query_compiler.merge_asof(
right._query_compiler,
left_on,
right_on,
left_index,
right_index,
left_by,
right_by,
suffixes,
tolerance,
allow_exact_matches,
direction,
)
)
@_inherit_docstrings(pandas.pivot_table, apilink="pandas.pivot_table")
@enable_logging
def pivot_table(
data,
values=None,
index=None,
columns=None,
aggfunc="mean",
fill_value=None,
margins=False,
dropna=True,
margins_name="All",
observed=no_default,
sort=True,
) -> DataFrame:
if not isinstance(data, DataFrame):
raise ValueError(
"can not create pivot table with instance of type {}".format(type(data))
)
return data.pivot_table(
values=values,
index=index,
columns=columns,
aggfunc=aggfunc,
fill_value=fill_value,
margins=margins,
dropna=dropna,
margins_name=margins_name,
observed=observed,
sort=sort,
)
@_inherit_docstrings(pandas.pivot, apilink="pandas.pivot")
@enable_logging
def pivot(
data, *, columns, index=no_default, values=no_default
) -> DataFrame: # noqa: PR01, RT01, D200
"""
Return reshaped DataFrame organized by given index / column values.
"""
if not isinstance(data, DataFrame):
raise ValueError("can not pivot with instance of type {}".format(type(data)))
return data.pivot(index=index, columns=columns, values=values)
@_inherit_docstrings(pandas.to_numeric, apilink="pandas.to_numeric")
@enable_logging
def to_numeric(
arg,
errors="raise",
downcast=None,
dtype_backend: Union[DtypeBackend, NoDefault] = no_default,
) -> Scalar | np.ndarray | Series: # noqa: PR01, RT01, D200
"""
Convert argument to a numeric type.
"""
if not isinstance(arg, Series):
return pandas.to_numeric(
arg, errors=errors, downcast=downcast, dtype_backend=dtype_backend
)
return arg._to_numeric(
errors=errors, downcast=downcast, dtype_backend=dtype_backend
)
@_inherit_docstrings(pandas.qcut, apilink="pandas.qcut")
@enable_logging
def qcut(
x, q, labels=None, retbins=False, precision=3, duplicates="raise"
): # noqa: PR01, RT01, D200
"""
Quantile-based discretization function.
"""
kwargs = {
"labels": labels,
"retbins": retbins,
"precision": precision,
"duplicates": duplicates,
}
if not isinstance(x, Series):
return pandas.qcut(x, q, **kwargs)
return x._qcut(q, **kwargs)
@_inherit_docstrings(pandas.cut, apilink="pandas.cut")
@enable_logging
def cut(
x,
bins,
right: bool = True,
labels=None,
retbins: bool = False,
precision: int = 3,
include_lowest: bool = False,
duplicates: str = "raise",
ordered: bool = True,
):
if isinstance(x, DataFrame):
raise ValueError("Input array must be 1 dimensional")
if not isinstance(x, Series):
ErrorMessage.default_to_pandas(
reason=f"pd.cut is not supported on objects of type {type(x)}"
)
import pandas
return pandas.cut(
x,
bins,
right=right,
labels=labels,
retbins=retbins,
precision=precision,
include_lowest=include_lowest,
duplicates=duplicates,
ordered=ordered,
)
def _wrap_in_series_object(qc_result):
if isinstance(qc_result, type(x._query_compiler)):
return Series(query_compiler=qc_result)
if isinstance(qc_result, (tuple, list)):
return tuple([_wrap_in_series_object(result) for result in qc_result])
return qc_result
return _wrap_in_series_object(
x._query_compiler.cut(
bins,
right=right,
labels=labels,
retbins=retbins,
precision=precision,
include_lowest=include_lowest,
duplicates=duplicates,
ordered=ordered,
)
)
@_inherit_docstrings(pandas.unique, apilink="pandas.unique")
@enable_logging
def unique(values) -> ArrayLike: # noqa: PR01, RT01, D200
"""
Return unique values based on a hash table.
"""
return Series(values).unique()
# Adding docstring since pandas docs don't have web section for this function.
@enable_logging
def value_counts(
values, sort=True, ascending=False, normalize=False, bins=None, dropna=True
) -> Series:
"""
Compute a histogram of the counts of non-null values.
Parameters
----------
values : ndarray (1-d)
Values to perform computation.
sort : bool, default: True
Sort by values.
ascending : bool, default: False
Sort in ascending order.
normalize : bool, default: False
If True then compute a relative histogram.
bins : integer, optional
Rather than count values, group them into half-open bins,
convenience for pd.cut, only works with numeric data.
dropna : bool, default: True
Don't include counts of NaN.
Returns
-------
Series
"""
warnings.warn(
"pandas.value_counts is deprecated and will be removed in a "
+ "future version. Use pd.Series(obj).value_counts() instead.",
FutureWarning,
)
return Series(values).value_counts(
sort=sort,
ascending=ascending,
normalize=normalize,
bins=bins,
dropna=dropna,
)
@_inherit_docstrings(pandas.concat, apilink="pandas.concat")
@enable_logging
def concat(
objs: "Iterable[DataFrame | Series] | Mapping[Hashable, DataFrame | Series]",
*,
axis=0,
join="outer",
ignore_index: bool = False,
keys=None,
levels=None,
names=None,
verify_integrity: bool = False,
sort: bool = False,
copy: Optional[bool] = None,
) -> DataFrame | Series: # noqa: PR01, RT01, D200
"""
Concatenate Modin objects along a particular axis.
"""
if isinstance(objs, (pandas.Series, Series, DataFrame, str, pandas.DataFrame)):
raise TypeError(
"first argument must be an iterable of pandas "
+ "objects, you passed an object of type "
+ f'"{type(objs).__name__}"'
)
axis = pandas.DataFrame()._get_axis_number(axis)
if isinstance(objs, dict):
input_list_of_objs = list(objs.values())
else:
input_list_of_objs = list(objs)
if len(input_list_of_objs) == 0:
raise ValueError("No objects to concatenate")
list_of_objs = [obj for obj in input_list_of_objs if obj is not None]
if len(list_of_objs) == 0:
raise ValueError("All objects passed were None")
try:
type_check = next(
obj
for obj in list_of_objs
if not isinstance(obj, (pandas.Series, Series, pandas.DataFrame, DataFrame))
)
except StopIteration:
type_check = None
if type_check is not None:
raise ValueError(
'cannot concatenate object of type "{0}"; only '
+ "modin.pandas.Series "
+ "and modin.pandas.DataFrame objs are "
+ "valid",
type(type_check),
)
all_series = all(isinstance(obj, Series) for obj in list_of_objs)
if all_series and axis == 0:
return Series(
query_compiler=list_of_objs[0]._query_compiler.concat(
axis,
[o._query_compiler for o in list_of_objs[1:]],
join=join,
join_axes=None,
ignore_index=ignore_index,
keys=None,
levels=None,
names=None,
verify_integrity=False,
copy=True,
sort=sort,
)
)
if join == "outer":
# Filter out empties
list_of_objs = [
obj
for obj in list_of_objs
if (
isinstance(obj, (Series, pandas.Series))
or (isinstance(obj, DataFrame) and obj._query_compiler.lazy_execution)
or sum(obj.shape) > 0
)
]
elif join != "inner":
raise ValueError(
"Only can inner (intersect) or outer (union) join the other axis"
)
list_of_objs = [
(
obj._query_compiler
if isinstance(obj, DataFrame)
else DataFrame(obj)._query_compiler
)
for obj in list_of_objs
]
if keys is None and isinstance(objs, dict):
keys = list(objs.keys())
if keys is not None:
if all_series:
new_idx = keys
else:
list_of_objs = [
list_of_objs[i] for i in range(min(len(list_of_objs), len(keys)))
]
new_idx_labels = {
k: v.index if axis == 0 else v.columns
for k, v in zip(keys, list_of_objs)
}
tuples = [
(k, *o) if isinstance(o, tuple) else (k, o)
for k, obj in new_idx_labels.items()
for o in obj
]
new_idx = pandas.MultiIndex.from_tuples(tuples)
if names is not None:
new_idx.names = names
else:
old_name = _determine_name(list_of_objs, axis)
if old_name is not None:
new_idx.names = [None] + old_name
else:
new_idx = None
if len(list_of_objs) == 0:
return DataFrame(
index=input_list_of_objs[0].index.append(
[f.index for f in input_list_of_objs[1:]]
)
)
new_query_compiler = list_of_objs[0].concat(
axis,
list_of_objs[1:],
join=join,
join_axes=None,
ignore_index=ignore_index,
keys=None,
levels=None,
names=None,
verify_integrity=False,
copy=True,
sort=sort,
)
result_df = DataFrame(query_compiler=new_query_compiler)
if new_idx is not None:
if axis == 0:
result_df.index = new_idx
else:
result_df.columns = new_idx
return result_df
@_inherit_docstrings(pandas.to_datetime, apilink="pandas.to_datetime")
@enable_logging
def to_datetime(
arg,
errors="raise",
dayfirst=False,
yearfirst=False,
utc=False,
format=None,
exact=no_default,
unit=None,
infer_datetime_format=no_default,
origin="unix",
cache=True,
) -> Scalar | ArrayLike | Series | DataFrame: # noqa: PR01, RT01, D200
"""
Convert argument to datetime.
"""
if not hasattr(arg, "_to_datetime"):
return pandas.to_datetime(
arg,
errors=errors,
dayfirst=dayfirst,
yearfirst=yearfirst,
utc=utc,
format=format,
exact=exact,
unit=unit,
infer_datetime_format=infer_datetime_format,
origin=origin,
cache=cache,
)
return arg._to_datetime(
errors=errors,
dayfirst=dayfirst,
yearfirst=yearfirst,
utc=utc,
format=format,
exact=exact,
unit=unit,
infer_datetime_format=infer_datetime_format,
origin=origin,
cache=cache,
)
@_inherit_docstrings(pandas.get_dummies, apilink="pandas.get_dummies")
@enable_logging
def get_dummies(
data,
prefix=None,
prefix_sep="_",
dummy_na=False,
columns=None,
sparse=False,
drop_first=False,
dtype=None,
) -> DataFrame: # noqa: PR01, RT01, D200
"""
Convert categorical variable into dummy/indicator variables.
"""
if sparse:
raise NotImplementedError(
"SparseArray is not implemented. "
+ "To contribute to Modin, please visit "
+ "github.com/modin-project/modin."
)
if not isinstance(data, DataFrame):
ErrorMessage.default_to_pandas("`get_dummies` on non-DataFrame")
if isinstance(data, Series):
data = data._to_pandas()
return DataFrame(
pandas.get_dummies(
data,
prefix=prefix,
prefix_sep=prefix_sep,
dummy_na=dummy_na,
columns=columns,
sparse=sparse,
drop_first=drop_first,
dtype=dtype,
)
)
else:
new_manager = data._query_compiler.get_dummies(
columns,
prefix=prefix,
prefix_sep=prefix_sep,
dummy_na=dummy_na,
drop_first=drop_first,
dtype=dtype,
)
return DataFrame(query_compiler=new_manager)
@_inherit_docstrings(pandas.melt, apilink="pandas.melt")
@enable_logging
def melt(
frame,
id_vars=None,
value_vars=None,
var_name=None,
value_name="value",
col_level=None,
ignore_index: bool = True,
) -> DataFrame: # noqa: PR01, RT01, D200
"""
Unpivot a DataFrame from wide to long format, optionally leaving identifiers set.
"""
return frame.melt(
id_vars=id_vars,
value_vars=value_vars,
var_name=var_name,
value_name=value_name,
col_level=col_level,
ignore_index=ignore_index,
)
@_inherit_docstrings(pandas.crosstab, apilink="pandas.crosstab")
@enable_logging
def crosstab(
index,
columns,
values=None,
rownames=None,
colnames=None,
aggfunc=None,
margins=False,
margins_name: str = "All",
dropna: bool = True,
normalize=False,
) -> DataFrame: # noqa: PR01, RT01, D200
"""
Compute a simple cross tabulation of two (or more) factors.
"""
ErrorMessage.default_to_pandas("`crosstab`")
pandas_crosstab = pandas.crosstab(
index,
columns,
values,
rownames,
colnames,
aggfunc,
margins,
margins_name,
dropna,
normalize,
)
return DataFrame(pandas_crosstab)
# Adding docstring since pandas docs don't have web section for this function.
@enable_logging
def lreshape(data: DataFrame, groups, dropna=True) -> DataFrame:
"""
Reshape wide-format data to long. Generalized inverse of ``DataFrame.pivot``.
Accepts a dictionary, `groups`, in which each key is a new column name
and each value is a list of old column names that will be "melted" under
the new column name as part of the reshape.
Parameters
----------
data : DataFrame
The wide-format DataFrame.
groups : dict
Dictionary in the form: `{new_name : list_of_columns}`.
dropna : bool, default: True
Whether include columns whose entries are all NaN or not.
Returns
-------
DataFrame
Reshaped DataFrame.
"""
if not isinstance(data, DataFrame):
raise ValueError("can not lreshape with instance of type {}".format(type(data)))
ErrorMessage.default_to_pandas("`lreshape`")
return DataFrame(pandas.lreshape(to_pandas(data), groups, dropna=dropna))
@_inherit_docstrings(pandas.wide_to_long, apilink="pandas.wide_to_long")
@enable_logging
def wide_to_long(
df: DataFrame, stubnames, i, j, sep: str = "", suffix: str = r"\d+"
) -> DataFrame: # noqa: PR01, RT01, D200
"""
Unpivot a DataFrame from wide to long format.
"""
if not isinstance(df, DataFrame):
raise ValueError(
"can not wide_to_long with instance of type {}".format(type(df))
)
return DataFrame(
query_compiler=df._query_compiler.wide_to_long(
stubnames=stubnames,
i=i,
j=j,
sep=sep,
suffix=suffix,
)
)
def _determine_name(objs: Iterable[BaseQueryCompiler], axis: Union[int, str]):
"""
Determine names of index after concatenation along passed axis.
Parameters
----------
objs : iterable of QueryCompilers
Objects to concatenate.
axis : int or str
The axis to concatenate along.
Returns
-------
list with single element
Computed index name, `None` if it could not be determined.
"""
axis = pandas.DataFrame()._get_axis_number(axis)
def get_names(obj):
return obj.columns.names if axis else obj.index.names
names = np.array([get_names(obj) for obj in objs])
# saving old name, only if index names of all objs are the same
if np.all(names == names[0]):
# we must do this check to avoid this calls `list(str_like_name)`
return list(names[0]) if is_list_like(names[0]) else [names[0]]
else:
return None
@_inherit_docstrings(pandas.to_datetime, apilink="pandas.to_timedelta")
@enable_logging
def to_timedelta(
arg, unit=None, errors="raise"
) -> Scalar | pandas.Index | Series: # noqa: PR01, RT01, D200
"""
Convert argument to timedelta.
Accepts str, timedelta, list-like or Series for arg parameter.
Returns a Series if and only if arg is provided as a Series.
"""
if isinstance(arg, Series):
query_compiler = arg._query_compiler.to_timedelta(unit=unit, errors=errors)
return Series(query_compiler=query_compiler)
return pandas.to_timedelta(arg, unit=unit, errors=errors)