Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add property: DataFrame.ndim, Index.ndim, MultiIndex.ndim #947

Merged
merged 5 commits into from
Oct 28, 2019
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions databricks/koalas/frame.py
Original file line number Diff line number Diff line change
@@ -377,6 +377,29 @@ def __init__(self, data=None, index=None, columns=None, dtype=None, copy=False):
def _sdf(self) -> spark.DataFrame:
return self._internal.sdf

@property
def ndim(self):
"""
Return an int representing the number of array dimensions.

Return 1 if Series. Otherwise return 2 if DataFrame.

Examples
--------

>>> df = ks.DataFrame([[1, 2], [4, 5], [7, 8]],
... index=['cobra', 'viper', None],
... columns=['max_speed', 'shield'])
>>> df
max_speed shield
cobra 1 2
viper 4 5
NaN 7 8
>>> df.ndim
2
"""
return 2

def _reduce_for_stat_function(self, sfun, name, axis=None, numeric_only=False):
"""
Applies sfun to each column and returns a pd.Series where the number of rows equal the
21 changes: 21 additions & 0 deletions databricks/koalas/indexes.py
Original file line number Diff line number Diff line change
@@ -151,6 +151,27 @@ def names(self, names: List[Union[str, Tuple[str, ...]]]) -> None:
names = [name if isinstance(name, tuple) else (name,) for name in names]
self._kdf._internal = internal.copy(index_map=list(zip(internal.index_columns, names)))

@property
def ndim(self):
"""
Number of dimensions of the underlying data, by definition 1.

Examples
--------
>>> s = ks.Series([None, 1, 2, 3, 4], index=[4, 5, 2, 1, 8])
>>> s.index.ndim
1

>>> midx = pd.MultiIndex([['lama', 'cow', 'falcon'],
... ['speed', 'weight', 'length']],
... [[0, 0, 0, 1, 1, 1, 2, 2, 2],
... [1, 1, 1, 1, 1, 2, 1, 2, 2]])
>>> s = ks.Series([45, 200, 1.2, 30, 250, 1.5, 320, 1, 0.3], index=midx)
>>> s.index.ndim
1
"""
return 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should move this to IndexOpsMixin and remove this in Series?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ueshin Thanks for the review!

oh i see. i just rearranged them.


def rename(self, name: Union[str, Tuple[str, ...]], inplace: bool = False):
"""
Alter Index name.
1 change: 0 additions & 1 deletion databricks/koalas/missing/frame.py
Original file line number Diff line number Diff line change
@@ -32,7 +32,6 @@ class _MissingPandasLikeDataFrame(object):
# Properties
axes = unsupported_property('axes')
iat = unsupported_property('iat')
ndim = unsupported_property('ndim')

# Deprecated properties
blocks = unsupported_property('blocks', deprecated=True)
2 changes: 0 additions & 2 deletions databricks/koalas/missing/indexes.py
Original file line number Diff line number Diff line change
@@ -33,7 +33,6 @@ class _MissingPandasLikeIndex(object):
T = unsupported_property('T')
has_duplicates = unsupported_property('has_duplicates')
nbytes = unsupported_property('nbytes')
ndim = unsupported_property('ndim')
nlevels = unsupported_property('nlevels')
shape = unsupported_property('shape')

@@ -134,7 +133,6 @@ class _MissingPandasLikeMultiIndex(object):
is_all_dates = unsupported_property('is_all_dates')
levels = unsupported_property('levels')
levshape = unsupported_property('levshape')
ndim = unsupported_property('ndim')
nlevels = unsupported_property('nlevels')
shape = unsupported_property('shape')

1 change: 1 addition & 0 deletions docs/source/reference/frame.rst
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ Attributes and underlying data

DataFrame.dtypes
DataFrame.shape
DataFrame.ndim
DataFrame.size
DataFrame.select_dtypes

2 changes: 2 additions & 0 deletions docs/source/reference/indexing.rst
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@ Properties
Index.dtype
Index.name
Index.names
Index.ndim
Index.empty

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

MultiIndex.names
MultiIndex.ndim

MultiIndex Modifying and computations
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~