Skip to content

CLN: remove Panel-specific parts of core.generic #26738

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

Merged
merged 4 commits into from
Jun 9, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
50 changes: 20 additions & 30 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1568,13 +1568,13 @@ def _is_level_reference(self, key, axis=0):
-------
is_level : bool
"""
axis = self._get_axis_number(axis)

if self.ndim > 2:
raise NotImplementedError(
"_is_level_reference is not implemented for {type}"
.format(type=type(self)))

axis = self._get_axis_number(axis)
Copy link
Contributor

Choose a reason for hiding this comment

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

actually I wouldn't worry about checking ndim at all in any methods and just remove all of the checks.

Copy link
Member Author

Choose a reason for hiding this comment

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

sounds good. thanks.


return (key is not None and
is_hashable(key) and
key in self.axes[axis].names and
Expand Down Expand Up @@ -2782,6 +2782,11 @@ class (index) object 'bird' 'bird' 'mammal' 'mammal'
speed (date, animal) int64 350 18 361 15
"""

if self.ndim > 2:
raise NotImplementedError(
"to_xarray is not implemented for {type}"
.format(type=type(self)))

try:
import xarray
except ImportError:
Expand All @@ -2794,15 +2799,9 @@ class (index) object 'bird' 'bird' 'mammal' 'mammal'

if self.ndim == 1:
return xarray.DataArray.from_series(self)
elif self.ndim == 2:
else:
return xarray.Dataset.from_dataframe(self)

# > 2 dims
coords = [(a, self._get_axis(a)) for a in self._AXIS_ORDERS]
return xarray.DataArray(self,
coords=coords,
)

def to_latex(self, buf=None, columns=None, col_space=None, header=True,
index=True, na_rep='NaN', formatters=None, float_format=None,
sparsify=None, index_names=True, bold_rows=False,
Expand Down Expand Up @@ -6051,22 +6050,11 @@ def fillna(self, value=None, method=None, axis=None, inplace=False,

return result

# > 3d
if self.ndim > 3:
if self.ndim > 2:
raise NotImplementedError('Cannot fillna with a method for > '
'3dims')

# 3d
elif self.ndim == 3:
# fill in 2d chunks
result = {col: s.fillna(method=method, value=value)
for col, s in self.iteritems()}
prelim_obj = self._constructor.from_dict(result)
new_obj = prelim_obj.__finalize__(self)
new_data = new_obj._data
'2dims')

else:
# 2d or less
new_data = self._data.interpolate(method=method, axis=axis,
limit=limit, inplace=inplace,
coerce=True,
Expand Down Expand Up @@ -6783,7 +6771,9 @@ def interpolate(self, method='linear', axis=0, limit=None, inplace=False,
inplace = validate_bool_kwarg(inplace, 'inplace')

if self.ndim > 2:
raise NotImplementedError("Interpolate has not been implemented ")
raise NotImplementedError(
"interpolate is not implemented for {type}"
.format(type=type(self)))

if axis == 0:
ax = self._info_axis_name
Expand Down Expand Up @@ -6953,6 +6943,9 @@ def asof(self, where, subset=None):
2018-02-27 09:03:30 30.0 NaN
2018-02-27 09:04:30 40.0 NaN
"""
if self.ndim > 2:
raise NotImplementedError("asof is not implemented "
"for {type}".format(type=type(self)))
if isinstance(where, str):
from pandas import to_datetime
where = to_datetime(where)
Expand All @@ -6964,9 +6957,6 @@ def asof(self, where, subset=None):
if is_series:
if subset is not None:
raise ValueError("subset is not valid for Series")
elif self.ndim > 2:
raise NotImplementedError("asof is not implemented "
"for {type}".format(type=type(self)))
else:
if subset is None:
subset = self.columns
Expand Down Expand Up @@ -8371,11 +8361,11 @@ def rank(self, axis=0, method='average', numeric_only=None,
3 spider 8.0 4.0 4.0 4.0 1.000
4 snake NaN NaN NaN 5.0 NaN
"""
axis = self._get_axis_number(axis)

if self.ndim > 2:
msg = "rank does not make sense when ndim > 2"
raise NotImplementedError(msg)
raise NotImplementedError("rank is not implemented "
"for {type}".format(type=type(self)))

axis = self._get_axis_number(axis)

if na_option not in {'keep', 'top', 'bottom'}:
msg = "na_option must be one of 'keep', 'top', or 'bottom'"
Expand Down
31 changes: 31 additions & 0 deletions pandas/tests/generic/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import pandas as pd
from pandas import DataFrame, MultiIndex, Series, date_range
from pandas.core.generic import NDFrame
import pandas.util.testing as tm
from pandas.util.testing import assert_frame_equal, assert_series_equal

Expand Down Expand Up @@ -918,3 +919,33 @@ def test_axis_classmethods(self, box):
assert obj._get_axis_name(v) == box._get_axis_name(v)
assert obj._get_block_manager_axis(v) == \
box._get_block_manager_axis(v)


@pytest.mark.parametrize('method, args', [
('_is_level_reference', ['key']),
('_is_label_reference', ['key']),
('_is_label_or_level_reference', ['key']),
('_check_label_or_level_ambiguity', ['key']),
('_get_label_or_level_values', ['key']),
('_drop_labels_or_levels', ['keys']),
('to_xarray', []),
('interpolate', []),
('asof', ['where']),
('rank', [])
])
def test_ndframe_not_implemented_raises(method, args):
obj = NDFrame(np.empty(shape=(0, 0, 0)))
msg = ("{} is not implemented for "
"<class 'pandas.core.generic.NDFrame'>").format(method)
with pytest.raises(NotImplementedError, match=msg):
getattr(obj, method)(*args)


def test_fillna_method_arg_raises(monkeypatch):
monkeypatch.setattr(NDFrame, '_consolidate_inplace', lambda x: x)
monkeypatch.setattr(NDFrame, '_get_axis_number', lambda x, y: 0)
monkeypatch.setattr(NDFrame, '_is_mixed_type', False)
obj = NDFrame(np.empty(shape=(0, 0, 0)))
msg = ("Cannot fillna with a method for > 2dims")
with pytest.raises(NotImplementedError, match=msg):
obj.fillna(method='backfill')