Skip to content

Commit 674cdd2

Browse files
authored
Missing return types for major Koalas objects (#1900)
1 parent cb01abd commit 674cdd2

File tree

5 files changed

+60
-53
lines changed

5 files changed

+60
-53
lines changed

databricks/koalas/frame.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
cast,
4444
)
4545

46+
import matplotlib
4647
import numpy as np
4748
import pandas as pd
4849
from pandas.api.types import is_list_like, is_dict_like, is_scalar
@@ -843,12 +844,12 @@ def add(self, other) -> "DataFrame":
843844
# create accessor for Koalas specific methods.
844845
koalas = CachedAccessor("koalas", KoalasFrameMethods)
845846

846-
def hist(self, bins=10, **kwds):
847+
def hist(self, bins=10, **kwds) -> matplotlib.axes.Axes:
847848
return self.plot.hist(bins, **kwds)
848849

849850
hist.__doc__ = KoalasPlotAccessor.hist.__doc__
850851

851-
def kde(self, bw_method=None, ind=None, **kwds):
852+
def kde(self, bw_method=None, ind=None, **kwds) -> matplotlib.axes.Axes:
852853
return self.plot.kde(bw_method, ind, **kwds)
853854

854855
kde.__doc__ = KoalasPlotAccessor.kde.__doc__
@@ -1432,7 +1433,7 @@ def items(self) -> Iterator:
14321433
"""This is an alias of ``iteritems``."""
14331434
return self.iteritems()
14341435

1435-
def to_clipboard(self, excel=True, sep=None, **kwargs):
1436+
def to_clipboard(self, excel=True, sep=None, **kwargs) -> None:
14361437
"""
14371438
Copy object to the system clipboard.
14381439
@@ -3806,7 +3807,7 @@ def nunique(
38063807

38073808
return first_series(DataFrame(internal).transpose())
38083809

3809-
def round(self, decimals=0):
3810+
def round(self, decimals=0) -> "DataFrame":
38103811
"""
38113812
Round a DataFrame to a variable number of decimal places.
38123813
@@ -8472,7 +8473,7 @@ def stack(self) -> Union["DataFrame", "ks.Series"]:
84728473
else:
84738474
return kdf
84748475

8475-
def unstack(self):
8476+
def unstack(self) -> Union["DataFrame", "ks.Series"]:
84768477
"""
84778478
Pivot the (necessarily hierarchical) index labels.
84788479
@@ -9649,7 +9650,7 @@ def idxmin(self, axis=0) -> "ks.Series":
96499650

96509651
return ks.from_pandas(kdf._to_internal_pandas().idxmin()) # type: ignore
96519652

9652-
def info(self, verbose=None, buf=None, max_cols=None, null_counts=None):
9653+
def info(self, verbose=None, buf=None, max_cols=None, null_counts=None) -> None:
96539654
"""
96549655
Print a concise summary of a DataFrame.
96559656
@@ -9750,7 +9751,7 @@ def info(self, verbose=None, buf=None, max_cols=None, null_counts=None):
97509751
# hack to use pandas' info as is.
97519752
self._data = self
97529753
count_func = self.count
9753-
self.count = lambda: count_func().to_pandas()
9754+
self.count = lambda: count_func().to_pandas() # type: ignore
97549755
return pd.DataFrame.info(
97559756
self,
97569757
verbose=verbose,
@@ -9761,7 +9762,7 @@ def info(self, verbose=None, buf=None, max_cols=None, null_counts=None):
97619762
)
97629763
finally:
97639764
del self._data
9764-
self.count = count_func
9765+
self.count = count_func # type: ignore
97659766

97669767
# TODO: fix parameter 'axis' and 'numeric_only' to work same as pandas'
97679768
def quantile(
@@ -10083,7 +10084,7 @@ class max_speed
1008310084
else:
1008410085
return self.iloc[:, indices] # type: ignore
1008510086

10086-
def eval(self, expr, inplace=False) -> Optional["DataFrame"]:
10087+
def eval(self, expr, inplace=False) -> Optional[Union["DataFrame", "ks.Series"]]:
1008710088
"""
1008810089
Evaluate a string describing operations on DataFrame columns.
1008910090

databricks/koalas/generic.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from collections.abc import Iterable
2323
from distutils.version import LooseVersion
2424
from functools import reduce
25-
from typing import List, Optional, Tuple, Union, TYPE_CHECKING, cast
25+
from typing import Any, List, Optional, Tuple, Union, TYPE_CHECKING, cast
2626
import warnings
2727

2828
import numpy as np # noqa: F401
@@ -58,6 +58,10 @@ class Frame(object, metaclass=ABCMeta):
5858
The base class for both DataFrame and Series.
5959
"""
6060

61+
@abstractmethod
62+
def __getitem__(self, key):
63+
pass
64+
6165
@property
6266
@abstractmethod
6367
def _internal(self) -> InternalFrame:
@@ -405,7 +409,7 @@ def get_dtype_counts(self) -> pd.Series:
405409
dtypes = list(self.dtypes)
406410
return pd.Series(dict(Counter([d.name for d in dtypes])))
407411

408-
def pipe(self, func, *args, **kwargs):
412+
def pipe(self, func, *args, **kwargs) -> Any:
409413
r"""
410414
Apply func(self, \*args, \*\*kwargs).
411415
@@ -1683,7 +1687,7 @@ def bool(self) -> bool:
16831687
raise TypeError("bool() expects DataFrame or Series; however, " "got [%s]" % (self,))
16841688
return df.head(2)._to_internal_pandas().bool()
16851689

1686-
def first_valid_index(self):
1690+
def first_valid_index(self) -> Union[Any, Tuple[Any, ...]]:
16871691
"""
16881692
Retrieves the index of the first valid value.
16891693
@@ -1778,7 +1782,7 @@ def first_valid_index(self):
17781782

17791783
return first_valid_idx
17801784

1781-
def last_valid_index(self):
1785+
def last_valid_index(self) -> Union[Any, Tuple[Any, ...]]:
17821786
"""
17831787
Return index for last non-NA/null value.
17841788
@@ -2024,7 +2028,7 @@ def expanding(self, min_periods=1) -> Expanding:
20242028
"""
20252029
return Expanding(self, min_periods=min_periods)
20262030

2027-
def get(self, key, default=None):
2031+
def get(self, key, default=None) -> Any:
20282032
"""
20292033
Get item from object for given key (DataFrame column, Panel slice,
20302034
etc.). Returns default value if not found.

databricks/koalas/groupby.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2657,10 +2657,10 @@ def _agg_columns(self):
26572657
def _reduce_for_stat_function(self, sfun, only_numeric):
26582658
return first_series(super()._reduce_for_stat_function(sfun, only_numeric))
26592659

2660-
def agg(self, *args, **kwargs):
2660+
def agg(self, *args, **kwargs) -> None:
26612661
return MissingPandasLikeSeriesGroupBy.agg(self, *args, **kwargs)
26622662

2663-
def aggregate(self, *args, **kwargs):
2663+
def aggregate(self, *args, **kwargs) -> None:
26642664
return MissingPandasLikeSeriesGroupBy.aggregate(self, *args, **kwargs)
26652665

26662666
def transform(self, func, *args, **kwargs) -> Series:

databricks/koalas/indexes.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2391,10 +2391,10 @@ def __abs__(self):
23912391
def _with_new_scol(self, scol: spark.Column):
23922392
raise NotImplementedError("Not supported for type MultiIndex")
23932393

2394-
def any(self, *args, **kwargs):
2394+
def any(self, *args, **kwargs) -> None:
23952395
raise TypeError("cannot perform any with this index type: MultiIndex")
23962396

2397-
def all(self, *args, **kwargs):
2397+
def all(self, *args, **kwargs) -> None:
23982398
raise TypeError("cannot perform all with this index type: MultiIndex")
23992399

24002400
@staticmethod
@@ -2861,7 +2861,7 @@ def toPandas(self) -> pd.MultiIndex:
28612861

28622862
toPandas.__doc__ = to_pandas.__doc__
28632863

2864-
def nunique(self, dropna=True):
2864+
def nunique(self, dropna=True) -> None: # type: ignore
28652865
raise NotImplementedError("nunique is not defined for MultiIndex")
28662866

28672867
# TODO: add 'name' parameter after pd.MultiIndex.name is implemented
@@ -3074,13 +3074,13 @@ def value_counts(
30743074

30753075
value_counts.__doc__ = IndexOpsMixin.value_counts.__doc__
30763076

3077-
def argmax(self):
3077+
def argmax(self) -> None:
30783078
raise TypeError("reduction operation 'argmax' not allowed for this dtype")
30793079

3080-
def argmin(self):
3080+
def argmin(self) -> None:
30813081
raise TypeError("reduction operation 'argmin' not allowed for this dtype")
30823082

3083-
def asof(self, label):
3083+
def asof(self, label) -> None:
30843084
raise NotImplementedError(
30853085
"only the default get_loc method is currently supported for MultiIndex"
30863086
)

0 commit comments

Comments
 (0)