Skip to content

ENH Warn about panel.to_frame() discarding NaN GH7879 #7970

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

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
9 changes: 9 additions & 0 deletions pandas/core/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pandas import compat
import sys
import numpy as np
import warnings
from pandas.core.common import (PandasError, _try_sort, _default_index,
_infer_dtype_from_scalar, notnull)
from pandas.core.categorical import Categorical
Expand Down Expand Up @@ -846,6 +847,7 @@ def to_frame(self, filter_observations=True):
filter_observations : boolean, default True
Drop (major, minor) pairs without a complete set of observations
across all the items
Default will be changed to False in future versions

Returns
-------
Expand All @@ -858,6 +860,13 @@ def to_frame(self, filter_observations=True):
mask = com.notnull(self.values).all(axis=0)
# size = mask.sum()
selector = mask.ravel()
if not selector.all():
warnings.warn("Panel to_frame method discards entries with "
"NaN/None in the data by default, use "
"filter_observations = False to save them. "
"This will be default behaviour "
"in future versions.",
FutureWarning)
else:
# size = N * K
selector = slice(None, None)
Expand Down
6 changes: 5 additions & 1 deletion pandas/tests/test_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1496,7 +1496,11 @@ def test_to_frame_multi_major(self):
assert_frame_equal(result, expected)

wp.iloc[0, 0].iloc[0] = np.nan # BUG on setting. GH #5773
result = wp.to_frame()

with tm.assert_produces_warning(FutureWarning):
setattr(panelm, '__warningregistry__', {})
Copy link
Contributor

Choose a reason for hiding this comment

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

what does this setattr line do?

Copy link
Author

Choose a reason for hiding this comment

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

Resets warnings in case it's already been raised somewhere. So I can for sure catch this warning.

result = wp.to_frame()

assert_frame_equal(result, expected[1:])

idx = MultiIndex.from_tuples([(1, 'two'), (1, 'one'), (2, 'one'),
Expand Down