forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 4
/
dataframe.py
1177 lines (958 loc) · 39.7 KB
/
dataframe.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
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF 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.
#
from typing import (
Any,
Dict,
List,
Optional,
Sequence,
Tuple,
Union,
TYPE_CHECKING,
overload,
Callable,
cast,
)
import pandas
import pyspark.sql.connect.plan as plan
from pyspark.sql.connect.column import (
Column,
Expression,
LiteralExpression,
SQLExpression,
ScalarFunctionExpression,
)
from pyspark.sql.types import (
StructType,
Row,
)
if TYPE_CHECKING:
from pyspark.sql.connect._typing import ColumnOrName, ExpressionOrString, LiteralType
from pyspark.sql.connect.session import SparkSession
class GroupedData(object):
def __init__(self, df: "DataFrame", *grouping_cols: Union[Column, str]) -> None:
self._df = df
self._grouping_cols = [x if isinstance(x, Column) else df[x] for x in grouping_cols]
def agg(self, measures: Sequence[Expression]) -> "DataFrame":
assert len(measures) > 0, "exprs should not be empty"
res = DataFrame.withPlan(
plan.Aggregate(
child=self._df._plan,
grouping_cols=self._grouping_cols,
measures=measures,
),
session=self._df._session,
)
return res
def _map_cols_to_expression(
self, fun: str, col: Union[Expression, str]
) -> Sequence[Expression]:
return [
ScalarFunctionExpression(fun, Column(col)) if isinstance(col, str) else col,
]
def min(self, col: Union[Expression, str]) -> "DataFrame":
expr = self._map_cols_to_expression("min", col)
return self.agg(expr)
def max(self, col: Union[Expression, str]) -> "DataFrame":
expr = self._map_cols_to_expression("max", col)
return self.agg(expr)
def sum(self, col: Union[Expression, str]) -> "DataFrame":
expr = self._map_cols_to_expression("sum", col)
return self.agg(expr)
def count(self) -> "DataFrame":
return self.agg([ScalarFunctionExpression("count", LiteralExpression(1))])
class DataFrame(object):
"""Every DataFrame object essentially is a Relation that is refined using the
member functions. Calling a method on a dataframe will essentially return a copy
of the DataFrame with the changes applied.
"""
def __init__(
self,
session: "SparkSession",
data: Optional[List[Any]] = None,
schema: Optional[StructType] = None,
):
"""Creates a new data frame"""
self._schema = schema
self._plan: Optional[plan.LogicalPlan] = None
self._session: "SparkSession" = session
def __repr__(self) -> str:
return "DataFrame[%s]" % (", ".join("%s: %s" % c for c in self.dtypes))
@classmethod
def withPlan(cls, plan: plan.LogicalPlan, session: "SparkSession") -> "DataFrame":
"""Main initialization method used to construct a new data frame with a child plan."""
new_frame = DataFrame(session=session)
new_frame._plan = plan
return new_frame
def isEmpty(self) -> bool:
"""Returns ``True`` if this :class:`DataFrame` is empty.
.. versionadded:: 3.4.0
Returns
-------
bool
Whether it's empty DataFrame or not.
"""
return len(self.take(1)) == 0
def select(self, *cols: "ExpressionOrString") -> "DataFrame":
return DataFrame.withPlan(plan.Project(self._plan, *cols), session=self._session)
def selectExpr(self, *expr: Union[str, List[str]]) -> "DataFrame":
"""Projects a set of SQL expressions and returns a new :class:`DataFrame`.
This is a variant of :func:`select` that accepts SQL expressions.
.. versionadded:: 3.4.0
Returns
-------
:class:`DataFrame`
A DataFrame with new/old columns transformed by expressions.
"""
sql_expr = []
if len(expr) == 1 and isinstance(expr[0], list):
expr = expr[0] # type: ignore[assignment]
for element in expr:
if isinstance(element, str):
sql_expr.append(SQLExpression(element))
else:
sql_expr.extend([SQLExpression(e) for e in element])
return DataFrame.withPlan(plan.Project(self._plan, *sql_expr), session=self._session)
def agg(self, *exprs: Union[Expression, Dict[str, str]]) -> "DataFrame":
if not exprs:
raise ValueError("Argument 'exprs' must not be empty")
if len(exprs) == 1 and isinstance(exprs[0], dict):
measures = [ScalarFunctionExpression(f, Column(e)) for e, f in exprs[0].items()]
return self.groupBy().agg(measures)
else:
# other expressions
assert all(isinstance(c, Expression) for c in exprs), "all exprs should be Expression"
exprs = cast(Tuple[Expression, ...], exprs)
return self.groupBy().agg(exprs)
def alias(self, alias: str) -> "DataFrame":
return DataFrame.withPlan(plan.SubqueryAlias(self._plan, alias), session=self._session)
def approxQuantile(self, col: Column, probabilities: Any, relativeError: Any) -> "DataFrame":
...
def colRegex(self, regex: str) -> "DataFrame":
...
@property
def dtypes(self) -> List[Tuple[str, str]]:
"""Returns all column names and their data types as a list.
.. versionadded:: 3.4.0
Returns
-------
list
List of columns as tuple pairs.
"""
return [(str(f.name), f.dataType.simpleString()) for f in self.schema.fields]
@property
def columns(self) -> List[str]:
"""Returns the list of columns of the current data frame."""
if self._plan is None:
return []
return self.schema.names
def sparkSession(self) -> "SparkSession":
"""Returns Spark session that created this :class:`DataFrame`.
.. versionadded:: 3.4.0
Returns
-------
:class:`SparkSession`
"""
return self._session
def count(self) -> int:
"""Returns the number of rows in the data frame"""
pdd = self.agg(ScalarFunctionExpression("count", LiteralExpression(1))).toPandas()
if pdd is None:
raise Exception("Empty result")
return pdd.iloc[0, 0]
def crossJoin(self, other: "DataFrame") -> "DataFrame":
...
def coalesce(self, numPartitions: int) -> "DataFrame":
"""
Returns a new :class:`DataFrame` that has exactly `numPartitions` partitions.
Coalesce does not trigger a shuffle.
.. versionadded:: 3.4.0
Parameters
----------
numPartitions : int
specify the target number of partitions
Returns
-------
:class:`DataFrame`
"""
if not numPartitions > 0:
raise ValueError("numPartitions must be positive.")
return DataFrame.withPlan(
plan.Repartition(self._plan, num_partitions=numPartitions, shuffle=False),
self._session,
)
def repartition(self, numPartitions: int) -> "DataFrame":
"""
Returns a new :class:`DataFrame` that has exactly `numPartitions` partitions.
Repartition will shuffle source partition into partitions specified by numPartitions.
.. versionadded:: 3.4.0
Parameters
----------
numPartitions : int
specify the target number of partitions
Returns
-------
:class:`DataFrame`
"""
if not numPartitions > 0:
raise ValueError("numPartitions must be positive.")
return DataFrame.withPlan(
plan.Repartition(self._plan, num_partitions=numPartitions, shuffle=True),
self._session,
)
def describe(self, cols: List[Column]) -> Any:
...
def dropDuplicates(self, subset: Optional[List[str]] = None) -> "DataFrame":
"""Return a new :class:`DataFrame` with duplicate rows removed,
optionally only deduplicating based on certain columns.
.. versionadded:: 3.4.0
Parameters
----------
subset : List of column names, optional
List of columns to use for duplicate comparison (default All columns).
Returns
-------
:class:`DataFrame`
DataFrame without duplicated rows.
"""
if subset is None:
return DataFrame.withPlan(
plan.Deduplicate(child=self._plan, all_columns_as_keys=True), session=self._session
)
else:
return DataFrame.withPlan(
plan.Deduplicate(child=self._plan, column_names=subset), session=self._session
)
def distinct(self) -> "DataFrame":
"""Returns a new :class:`DataFrame` containing the distinct rows in this :class:`DataFrame`.
.. versionadded:: 3.4.0
Returns
-------
:class:`DataFrame`
DataFrame with distinct rows.
"""
return DataFrame.withPlan(
plan.Deduplicate(child=self._plan, all_columns_as_keys=True), session=self._session
)
def drop(self, *cols: "ColumnOrName") -> "DataFrame":
_cols = list(cols)
if any(not isinstance(c, (str, Column)) for c in _cols):
raise TypeError(
f"'cols' must contains strings or Columns, but got {type(cols).__name__}"
)
if len(_cols) == 0:
raise ValueError("'cols' must be non-empty")
return DataFrame.withPlan(
plan.Drop(
child=self._plan,
columns=_cols,
),
session=self._session,
)
def filter(self, condition: Expression) -> "DataFrame":
return DataFrame.withPlan(
plan.Filter(child=self._plan, filter=condition), session=self._session
)
def first(self) -> Optional[Row]:
"""Returns the first row as a :class:`Row`.
.. versionadded:: 3.4.0
Returns
-------
:class:`Row`
First row if :class:`DataFrame` is not empty, otherwise ``None``.
"""
return self.head()
def groupBy(self, *cols: "ColumnOrName") -> GroupedData:
return GroupedData(self, *cols)
@overload
def head(self) -> Optional[Row]:
...
@overload
def head(self, n: int) -> List[Row]:
...
def head(self, n: Optional[int] = None) -> Union[Optional[Row], List[Row]]:
"""Returns the first ``n`` rows.
.. versionadded:: 3.4.0
Parameters
----------
n : int, optional
default 1. Number of rows to return.
Returns
-------
If n is greater than 1, return a list of :class:`Row`.
If n is 1, return a single Row.
"""
if n is None:
rs = self.head(1)
return rs[0] if rs else None
return self.take(n)
def take(self, num: int) -> List[Row]:
"""Returns the first ``num`` rows as a :class:`list` of :class:`Row`.
.. versionadded:: 3.4.0
Parameters
----------
num : int
Number of records to return. Will return this number of records
or whataver number is available.
Returns
-------
list
List of rows
"""
return self.limit(num).collect()
# TODO: extend `on` to also be type List[Column].
def join(
self,
other: "DataFrame",
on: Optional[Union[str, List[str], Column]] = None,
how: Optional[str] = None,
) -> "DataFrame":
if self._plan is None:
raise Exception("Cannot join when self._plan is empty.")
if other._plan is None:
raise Exception("Cannot join when other._plan is empty.")
return DataFrame.withPlan(
plan.Join(left=self._plan, right=other._plan, on=on, how=how),
session=self._session,
)
def limit(self, n: int) -> "DataFrame":
return DataFrame.withPlan(plan.Limit(child=self._plan, limit=n), session=self._session)
def offset(self, n: int) -> "DataFrame":
return DataFrame.withPlan(plan.Offset(child=self._plan, offset=n), session=self._session)
def tail(self, num: int) -> List[Row]:
"""
Returns the last ``num`` rows as a :class:`list` of :class:`Row`.
Running tail requires moving data into the application's driver process, and doing so with
a very large ``num`` can crash the driver process with OutOfMemoryError.
.. versionadded:: 3.4.0
Parameters
----------
num : int
Number of records to return. Will return this number of records
or whataver number is available.
Returns
-------
list
List of rows
"""
return DataFrame.withPlan(
plan.Tail(child=self._plan, limit=num), session=self._session
).collect()
def sort(self, *cols: "ColumnOrName") -> "DataFrame":
"""Sort by a specific column"""
return DataFrame.withPlan(
plan.Sort(self._plan, columns=list(cols), is_global=True), session=self._session
)
def sortWithinPartitions(self, *cols: "ColumnOrName") -> "DataFrame":
"""Sort within each partition by a specific column"""
return DataFrame.withPlan(
plan.Sort(self._plan, columns=list(cols), is_global=False), session=self._session
)
def sample(
self,
fraction: float,
*,
withReplacement: bool = False,
seed: Optional[int] = None,
) -> "DataFrame":
if not isinstance(fraction, float):
raise TypeError(f"'fraction' must be float, but got {type(fraction).__name__}")
if not isinstance(withReplacement, bool):
raise TypeError(
f"'withReplacement' must be bool, but got {type(withReplacement).__name__}"
)
if seed is not None and not isinstance(seed, int):
raise TypeError(f"'seed' must be None or int, but got {type(seed).__name__}")
return DataFrame.withPlan(
plan.Sample(
child=self._plan,
lower_bound=0.0,
upper_bound=fraction,
with_replacement=withReplacement,
seed=seed,
),
session=self._session,
)
def _show_string(
self, n: int = 20, truncate: Union[bool, int] = True, vertical: bool = False
) -> str:
if not isinstance(n, int) or isinstance(n, bool):
raise TypeError("Parameter 'n' (number of rows) must be an int")
if not isinstance(vertical, bool):
raise TypeError("Parameter 'vertical' must be a bool")
_truncate: int = -1
if isinstance(truncate, bool) and truncate:
_truncate = 20
else:
try:
_truncate = int(truncate)
except ValueError:
raise TypeError(
"Parameter 'truncate={}' should be either bool or int.".format(truncate)
)
pdf = DataFrame.withPlan(
plan.ShowString(child=self._plan, numRows=n, truncate=_truncate, vertical=vertical),
session=self._session,
).toPandas()
assert pdf is not None
return pdf["show_string"][0]
def show(self, n: int = 20, truncate: Union[bool, int] = True, vertical: bool = False) -> None:
"""
Prints the first ``n`` rows to the console.
.. versionadded:: 3.4.0
Parameters
----------
n : int, optional
Number of rows to show.
truncate : bool or int, optional
If set to ``True``, truncate strings longer than 20 chars by default.
If set to a number greater than one, truncates long strings to length ``truncate``
and align cells right.
vertical : bool, optional
If set to ``True``, print output rows vertically (one line
per column value).
"""
print(self._show_string(n, truncate, vertical))
def union(self, other: "DataFrame") -> "DataFrame":
return self.unionAll(other)
def unionAll(self, other: "DataFrame") -> "DataFrame":
if other._plan is None:
raise ValueError("Argument to Union does not contain a valid plan.")
return DataFrame.withPlan(
plan.SetOperation(self._plan, other._plan, "union", is_all=True), session=self._session
)
def unionByName(self, other: "DataFrame", allowMissingColumns: bool = False) -> "DataFrame":
"""Returns a new :class:`DataFrame` containing union of rows in this and another
:class:`DataFrame`.
This is different from both `UNION ALL` and `UNION DISTINCT` in SQL. To do a SQL-style set
union (that does deduplication of elements), use this function followed by :func:`distinct`.
.. versionadded:: 3.4.0
Parameters
----------
other : :class:`DataFrame`
Another :class:`DataFrame` that needs to be combined.
allowMissingColumns : bool, optional, default False
Specify whether to allow missing columns.
Returns
-------
:class:`DataFrame`
Combined DataFrame.
"""
if other._plan is None:
raise ValueError("Argument to UnionByName does not contain a valid plan.")
return DataFrame.withPlan(
plan.SetOperation(
self._plan, other._plan, "union", is_all=True, by_name=allowMissingColumns
),
session=self._session,
)
def exceptAll(self, other: "DataFrame") -> "DataFrame":
"""Return a new :class:`DataFrame` containing rows in this :class:`DataFrame` but
not in another :class:`DataFrame` while preserving duplicates.
This is equivalent to `EXCEPT ALL` in SQL.
As standard in SQL, this function resolves columns by position (not by name).
.. versionadded:: 3.4.0
Parameters
----------
other : :class:`DataFrame`
The other :class:`DataFrame` to compare to.
Returns
-------
:class:`DataFrame`
"""
return DataFrame.withPlan(
plan.SetOperation(self._plan, other._plan, "except", is_all=True), session=self._session
)
def intersect(self, other: "DataFrame") -> "DataFrame":
"""Return a new :class:`DataFrame` containing rows only in
both this :class:`DataFrame` and another :class:`DataFrame`.
Note that any duplicates are removed. To preserve duplicates
use :func:`intersectAll`.
.. versionadded:: 3.4.0
Parameters
----------
other : :class:`DataFrame`
Another :class:`DataFrame` that needs to be combined.
Returns
-------
:class:`DataFrame`
Combined DataFrame.
Notes
-----
This is equivalent to `INTERSECT` in SQL.
"""
return DataFrame.withPlan(
plan.SetOperation(self._plan, other._plan, "intersect", is_all=False),
session=self._session,
)
def intersectAll(self, other: "DataFrame") -> "DataFrame":
"""Return a new :class:`DataFrame` containing rows in both this :class:`DataFrame`
and another :class:`DataFrame` while preserving duplicates.
This is equivalent to `INTERSECT ALL` in SQL. As standard in SQL, this function
resolves columns by position (not by name).
.. versionadded:: 3.4.0
Parameters
----------
other : :class:`DataFrame`
Another :class:`DataFrame` that needs to be combined.
Returns
-------
:class:`DataFrame`
Combined DataFrame.
"""
return DataFrame.withPlan(
plan.SetOperation(self._plan, other._plan, "intersect", is_all=True),
session=self._session,
)
def where(self, condition: Expression) -> "DataFrame":
return self.filter(condition)
@property
def na(self) -> "DataFrameNaFunctions":
"""Returns a :class:`DataFrameNaFunctions` for handling missing values.
.. versionadded:: 3.4.0
Returns
-------
:class:`DataFrameNaFunctions`
"""
return DataFrameNaFunctions(self)
def fillna(
self,
value: Union["LiteralType", Dict[str, "LiteralType"]],
subset: Optional[Union[str, Tuple[str, ...], List[str]]] = None,
) -> "DataFrame":
"""Replace null values, alias for ``na.fill()``.
:func:`DataFrame.fillna` and :func:`DataFrameNaFunctions.fill` are aliases of each other.
.. versionadded:: 3.4.0
Parameters
----------
value : int, float, string, bool or dict
Value to replace null values with.
If the value is a dict, then `subset` is ignored and `value` must be a mapping
from column name (string) to replacement value. The replacement value must be
an int, float, boolean, or string.
subset : str, tuple or list, optional
optional list of column names to consider.
Columns specified in subset that do not have matching data type are ignored.
For example, if `value` is a string, and cols contains a non-string column,
then the non-string column is simply ignored.
Returns
-------
:class:`DataFrame`
DataFrame with replaced null values.
"""
if not isinstance(value, (float, int, str, bool, dict)):
raise TypeError(
f"value should be a float, int, string, bool or dict, "
f"but got {type(value).__name__}"
)
if isinstance(value, dict):
if len(value) == 0:
raise ValueError("value dict can not be empty")
for c, v in value.items():
if not isinstance(c, str):
raise TypeError(
f"key type of dict should be string, but got {type(c).__name__}"
)
if not isinstance(v, (bool, int, float, str)):
raise TypeError(
f"value type of dict should be float, int, string or bool, "
f"but got {type(v).__name__}"
)
_cols: List[str] = []
if subset is not None:
if isinstance(subset, str):
_cols = [subset]
elif isinstance(subset, (tuple, list)):
for c in subset:
if not isinstance(c, str):
raise TypeError(
f"cols should be a str, tuple[str] or list[str], "
f"but got {type(c).__name__}"
)
_cols = list(subset)
else:
raise TypeError(
f"cols should be a str, tuple[str] or list[str], "
f"but got {type(subset).__name__}"
)
if isinstance(value, dict):
_cols = list(value.keys())
_values = [value[c] for c in _cols]
else:
_values = [value]
return DataFrame.withPlan(
plan.NAFill(child=self._plan, cols=_cols, values=_values),
session=self._session,
)
@property
def stat(self) -> "DataFrameStatFunctions":
"""Returns a :class:`DataFrameStatFunctions` for statistic functions.
.. versionadded:: 3.4.0
Returns
-------
:class:`DataFrameStatFunctions`
"""
return DataFrameStatFunctions(self)
def summary(self, *statistics: str) -> "DataFrame":
_statistics: List[str] = list(statistics)
for s in _statistics:
if not isinstance(s, str):
raise TypeError(f"'statistics' must be list[str], but got {type(s).__name__}")
return DataFrame.withPlan(
plan.StatSummary(child=self._plan, statistics=_statistics),
session=self._session,
)
def crosstab(self, col1: str, col2: str) -> "DataFrame":
"""
Computes a pair-wise frequency table of the given columns. Also known as a contingency
table. The number of distinct values for each column should be less than 1e4. At most 1e6
non-zero pair frequencies will be returned.
The first column of each row will be the distinct values of `col1` and the column names
will be the distinct values of `col2`. The name of the first column will be `$col1_$col2`.
Pairs that have no occurrences will have zero as their counts.
:func:`DataFrame.crosstab` and :func:`DataFrameStatFunctions.crosstab` are aliases.
.. versionadded:: 3.4.0
Parameters
----------
col1 : str
The name of the first column. Distinct items will make the first item of
each row.
col2 : str
The name of the second column. Distinct items will make the column names
of the :class:`DataFrame`.
Returns
-------
:class:`DataFrame`
Frequency matrix of two columns.
"""
if not isinstance(col1, str):
raise TypeError(f"'col1' must be str, but got {type(col1).__name__}")
if not isinstance(col2, str):
raise TypeError(f"'col2' must be str, but got {type(col2).__name__}")
return DataFrame.withPlan(
plan.StatCrosstab(child=self._plan, col1=col1, col2=col2),
session=self._session,
)
def _get_alias(self) -> Optional[str]:
p = self._plan
while p is not None:
if isinstance(p, plan.Project) and p.alias:
return p.alias
p = p._child
return None
def __getattr__(self, name: str) -> "Column":
return self[name]
def __getitem__(self, name: str) -> "Column":
# Check for alias
alias = self._get_alias()
if alias is not None:
return Column(alias)
else:
return Column(name)
def _print_plan(self) -> str:
if self._plan:
return self._plan.print()
return ""
def collect(self) -> List[Row]:
pdf = self.toPandas()
if pdf is not None:
return list(pdf.apply(lambda row: Row(**row), axis=1))
else:
return []
def toPandas(self) -> "pandas.DataFrame":
if self._plan is None:
raise Exception("Cannot collect on empty plan.")
if self._session is None:
raise Exception("Cannot collect on empty session.")
query = self._plan.to_proto(self._session.client)
return self._session.client._to_pandas(query)
@property
def schema(self) -> StructType:
"""Returns the schema of this :class:`DataFrame` as a :class:`pyspark.sql.types.StructType`.
.. versionadded:: 3.4.0
Returns
-------
:class:`StructType`
"""
if self._schema is None:
if self._plan is not None:
query = self._plan.to_proto(self._session.client)
if self._session is None:
raise Exception("Cannot analyze without SparkSession.")
self._schema = self._session.client.schema(query)
return self._schema
else:
raise Exception("Empty plan.")
else:
return self._schema
@property
def isLocal(self) -> bool:
"""Returns ``True`` if the :func:`collect` and :func:`take` methods can be run locally
(without any Spark executors).
.. versionadded:: 3.4.0
Returns
-------
bool
"""
if self._plan is None:
raise Exception("Cannot analyze on empty plan.")
query = self._plan.to_proto(self._session.client)
return self._session.client._analyze(query).is_local
@property
def isStreaming(self) -> bool:
"""Returns ``True`` if this :class:`DataFrame` contains one or more sources that
continuously return data as it arrives. A :class:`DataFrame` that reads data from a
streaming source must be executed as a :class:`StreamingQuery` using the :func:`start`
method in :class:`DataStreamWriter`. Methods that return a single answer, (e.g.,
:func:`count` or :func:`collect`) will throw an :class:`AnalysisException` when there
is a streaming source present.
.. versionadded:: 3.4.0
Notes
-----
This API is evolving.
Returns
-------
bool
Whether it's streaming DataFrame or not.
"""
if self._plan is None:
raise Exception("Cannot analyze on empty plan.")
query = self._plan.to_proto(self._session.client)
return self._session.client._analyze(query).is_streaming
def _tree_string(self) -> str:
if self._plan is None:
raise Exception("Cannot analyze on empty plan.")
query = self._plan.to_proto(self._session.client)
return self._session.client._analyze(query).tree_string
def printSchema(self) -> None:
"""Prints out the schema in the tree format.
.. versionadded:: 3.4.0
Returns
-------
None
"""
print(self._tree_string())
def inputFiles(self) -> List[str]:
"""
Returns a best-effort snapshot of the files that compose this :class:`DataFrame`.
This method simply asks each constituent BaseRelation for its respective files and
takes the union of all results. Depending on the source relations, this may not find
all input files. Duplicates are removed.
.. versionadded:: 3.4.0
Returns
-------
list
List of file paths.
"""
if self._plan is None:
raise Exception("Cannot analyze on empty plan.")
query = self._plan.to_proto(self._session.client)
return self._session.client._analyze(query).input_files
def transform(self, func: Callable[..., "DataFrame"], *args: Any, **kwargs: Any) -> "DataFrame":
"""Returns a new :class:`DataFrame`. Concise syntax for chaining custom transformations.
.. versionadded:: 3.4.0
Parameters
----------
func : function
a function that takes and returns a :class:`DataFrame`.
*args
Positional arguments to pass to func.
**kwargs
Keyword arguments to pass to func.
Returns
-------
:class:`DataFrame`
Transformed DataFrame.
Examples
--------
>>> from pyspark.sql.connect.functions import col
>>> df = spark.createDataFrame([(1, 1.0), (2, 2.0)], ["int", "float"])
>>> def cast_all_to_int(input_df):
... return input_df.select([col(col_name).cast("int") for col_name in input_df.columns])
>>> def sort_columns_asc(input_df):
... return input_df.select(*sorted(input_df.columns))
>>> df.transform(cast_all_to_int).transform(sort_columns_asc).show()
+-----+---+
|float|int|
+-----+---+
| 1| 1|
| 2| 2|
+-----+---+
>>> def add_n(input_df, n):
... return input_df.select([(col(col_name) + n).alias(col_name)
... for col_name in input_df.columns])
>>> df.transform(add_n, 1).transform(add_n, n=10).show()
+---+-----+
|int|float|
+---+-----+
| 12| 12.0|
| 13| 13.0|
+---+-----+
"""
result = func(self, *args, **kwargs)
assert isinstance(
result, DataFrame
), "Func returned an instance of type [%s], " "should have been DataFrame." % type(result)
return result
def explain(
self, extended: Optional[Union[bool, str]] = None, mode: Optional[str] = None
) -> str:
"""Retruns plans in string for debugging purpose.
.. versionadded:: 3.4.0
Parameters
----------
extended : bool, optional
default ``False``. If ``False``, returns only the physical plan.
When this is a string without specifying the ``mode``, it works as the mode is
specified.
mode : str, optional
specifies the expected output format of plans.
* ``simple``: Print only a physical plan.
* ``extended``: Print both logical and physical plans.
* ``codegen``: Print a physical plan and generated codes if they are available.
* ``cost``: Print a logical plan and statistics if they are available.
* ``formatted``: Split explain output into two sections: a physical plan outline \
and node details.
"""