Skip to content

Adding a warning when dropping NA values for panel.to_frame #7879 #8063

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
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions pandas/core/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,9 @@ def to_frame(self, filter_observations=True):
mask = com.notnull(self.values).all(axis=0)
# size = mask.sum()
selector = mask.ravel()
if not np.all(selector):
warnings.warn("NaN values found\
Copy link
Contributor

Choose a reason for hiding this comment

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

You'll need to import warnings in this file too

empty values will be dropped", RuntimeWarning)
else:
# size = N * K
selector = slice(None, None)
Expand Down
32 changes: 31 additions & 1 deletion pandas/tests/test_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import nose

import numpy as np

import warnings
from pandas import Series, DataFrame, Index, isnull, notnull, pivot, MultiIndex
from pandas.core.datetools import bday
from pandas.core.panel import Panel
Expand Down Expand Up @@ -1556,6 +1556,36 @@ def test_to_frame_multi_drop_level(self):
expected = DataFrame({'i1': [1., 2], 'i2': [1., 2]}, index=exp_idx)
assert_frame_equal(result, expected)

def test_to_frame_na_drop_warnings(self):
def create_a_panel_with_na_vals(filter_observations=True):
df1 = DataFrame(np.random.randn(2, 3), columns=['A', 'B', 'C'],
index=['foo', 'bar'])
df2 = DataFrame(np.random.randn(2, 3), columns=['A', 'B', 'C'],
index=['foo', 'bar'])
df2.loc['foo', 'B'] = np.nan
dict_with_dropped_vals = {'df1': df1, 'df2': df2}
Panel(dict_with_dropped_vals).\
to_frame(filter_observations=filter_observations)

def create_a_panel_without_na_vals(filter_observations=True):
df1 = DataFrame(np.random.randn(2, 3), columns=['A', 'B', 'C'],
index=['foo', 'bar'])
df2 = DataFrame(np.random.randn(2, 3), columns=['A', 'B', 'C'],
index=['foo', 'bar'])
dict_with_dropped_vals = {'df1': df1, 'df2': df2}
Panel(dict_with_dropped_vals).\
to_frame(filter_observations=filter_observations)
with warnings.catch_warnings(record=True) as w:
Copy link
Contributor

Choose a reason for hiding this comment

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

We have a context manger called assert_produces_warning in pandas/util/testing (usually imported as tm).

You can call it like

with tm.assert_produces_warning(RuntimeWarning):
    panel.to_frame()

then break each of your test cases into to separate lines. You can also check that a warning isn't raised with

with tm.assert_produces_warning(False)

warnings.simplefilter("always")
create_a_panel_with_na_vals()
create_a_panel_with_na_vals(False)
create_a_panel_without_na_vals()
create_a_panel_without_na_vals(False)
self.assertEqual(len(w), 1)
self.assertTrue(issubclass(w[0].category, RuntimeWarning))
self.assertEqual(str(w[0].message),
"NaN values found, empty values will be dropped")

def test_to_panel_na_handling(self):
df = DataFrame(np.random.randint(0, 10, size=20).reshape((10, 2)),
index=[[0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
Expand Down