Skip to content

Commit 506ae16

Browse files
itholicHyukjinKwon
authored andcommitted
Add property: DataFrame.ndim, Index.ndim, MultiIndex.ndim (#947)
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.ndim.html#pandas.DataFrame.ndim ```python >>> df = ks.range(10) >>> df.ndim 2 >>> df = ks.DataFrame([[1, 2], [4, 5], [7, 8]], ... index=['cobra', 'viper', 'sidewinder'], ... columns=['max_speed', 'shield']) >>> df.ndim 2 ```
1 parent 709b928 commit 506ae16

File tree

7 files changed

+59
-8
lines changed

7 files changed

+59
-8
lines changed

databricks/koalas/base.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,39 @@ def is_monotonic_decreasing(self):
363363
window = Window.orderBy(monotonically_increasing_id()).rowsBetween(-1, -1)
364364
return self._with_new_scol((col <= F.lag(col, 1).over(window)) & col.isNotNull()).all()
365365

366+
@property
367+
def ndim(self):
368+
"""
369+
Return an int representing the number of array dimensions.
370+
371+
Return 1 for Series / Index / MultiIndex.
372+
373+
Examples
374+
--------
375+
376+
For Series
377+
378+
>>> s = ks.Series([None, 1, 2, 3, 4], index=[4, 5, 2, 1, 8])
379+
>>> s.ndim
380+
1
381+
382+
For Index
383+
384+
>>> s.index.ndim
385+
1
386+
387+
For MultiIndex
388+
389+
>>> midx = pd.MultiIndex([['lama', 'cow', 'falcon'],
390+
... ['speed', 'weight', 'length']],
391+
... [[0, 0, 0, 1, 1, 1, 2, 2, 2],
392+
... [1, 1, 1, 1, 1, 2, 1, 2, 2]])
393+
>>> s = ks.Series([45, 200, 1.2, 30, 250, 1.5, 320, 1, 0.3], index=midx)
394+
>>> s.index.ndim
395+
1
396+
"""
397+
return 1
398+
366399
def astype(self, dtype):
367400
"""
368401
Cast a Koalas object to a specified dtype ``dtype``.

databricks/koalas/frame.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,29 @@ def __init__(self, data=None, index=None, columns=None, dtype=None, copy=False):
377377
def _sdf(self) -> spark.DataFrame:
378378
return self._internal.sdf
379379

380+
@property
381+
def ndim(self):
382+
"""
383+
Return an int representing the number of array dimensions.
384+
385+
return 2 for DataFrame.
386+
387+
Examples
388+
--------
389+
390+
>>> df = ks.DataFrame([[1, 2], [4, 5], [7, 8]],
391+
... index=['cobra', 'viper', None],
392+
... columns=['max_speed', 'shield'])
393+
>>> df
394+
max_speed shield
395+
cobra 1 2
396+
viper 4 5
397+
NaN 7 8
398+
>>> df.ndim
399+
2
400+
"""
401+
return 2
402+
380403
def _reduce_for_stat_function(self, sfun, name, axis=None, numeric_only=False):
381404
"""
382405
Applies sfun to each column and returns a pd.Series where the number of rows equal the

databricks/koalas/missing/frame.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ class _MissingPandasLikeDataFrame(object):
3232
# Properties
3333
axes = unsupported_property('axes')
3434
iat = unsupported_property('iat')
35-
ndim = unsupported_property('ndim')
3635

3736
# Deprecated properties
3837
blocks = unsupported_property('blocks', deprecated=True)

databricks/koalas/missing/indexes.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ class _MissingPandasLikeIndex(object):
3333
T = unsupported_property('T')
3434
has_duplicates = unsupported_property('has_duplicates')
3535
nbytes = unsupported_property('nbytes')
36-
ndim = unsupported_property('ndim')
3736
nlevels = unsupported_property('nlevels')
3837
shape = unsupported_property('shape')
3938

@@ -134,7 +133,6 @@ class _MissingPandasLikeMultiIndex(object):
134133
is_all_dates = unsupported_property('is_all_dates')
135134
levels = unsupported_property('levels')
136135
levshape = unsupported_property('levshape')
137-
ndim = unsupported_property('ndim')
138136
nlevels = unsupported_property('nlevels')
139137
shape = unsupported_property('shape')
140138

databricks/koalas/series.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -821,11 +821,6 @@ def shape(self):
821821
"""Return a tuple of the shape of the underlying data."""
822822
return len(self),
823823

824-
@property
825-
def ndim(self):
826-
"""Returns number of dimensions of the Series."""
827-
return 1
828-
829824
@property
830825
def name(self) -> Union[str, Tuple[str, ...]]:
831826
"""Return name of the Series."""

docs/source/reference/frame.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Attributes and underlying data
2727

2828
DataFrame.dtypes
2929
DataFrame.shape
30+
DataFrame.ndim
3031
DataFrame.size
3132
DataFrame.select_dtypes
3233

docs/source/reference/indexing.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Properties
2121
Index.dtype
2222
Index.name
2323
Index.names
24+
Index.ndim
2425
Index.empty
2526

2627
Modifying and computations
@@ -76,6 +77,7 @@ MultiIndex Properties
7677
:toctree: api/
7778

7879
MultiIndex.names
80+
MultiIndex.ndim
7981

8082
MultiIndex Modifying and computations
8183
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)