Skip to content

Commit 96168ef

Browse files
jbrockmendelgfyoung
authored andcommitted
MAINT: Move series.remove_na to core.dtypes.missing.remove_na_arraylike
Closes pandas-devgh-16935
1 parent 3955261 commit 96168ef

File tree

5 files changed

+19
-19
lines changed

5 files changed

+19
-19
lines changed

pandas/core/dtypes/missing.py

+7
Original file line numberDiff line numberDiff line change
@@ -394,3 +394,10 @@ def na_value_for_dtype(dtype):
394394
elif is_bool_dtype(dtype):
395395
return False
396396
return np.nan
397+
398+
399+
def remove_na_arraylike(arr):
400+
"""
401+
Return array-like containing only true/non-NaN values, possibly empty.
402+
"""
403+
return arr[notnull(lib.values_from_object(arr))]

pandas/core/series.py

+2-9
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
maybe_upcast, infer_dtype_from_scalar,
3737
maybe_convert_platform,
3838
maybe_cast_to_datetime, maybe_castable)
39-
from pandas.core.dtypes.missing import isnull, notnull
39+
from pandas.core.dtypes.missing import isnull, notnull, remove_na_arraylike
4040

4141
from pandas.core.common import (is_bool_indexer,
4242
_default_index,
@@ -2749,7 +2749,7 @@ def dropna(self, axis=0, inplace=False, **kwargs):
27492749
axis = self._get_axis_number(axis or 0)
27502750

27512751
if self._can_hold_na:
2752-
result = remove_na(self)
2752+
result = remove_na_arraylike(self)
27532753
if inplace:
27542754
self._update_inplace(result)
27552755
else:
@@ -2888,13 +2888,6 @@ def _dir_additions(self):
28882888
# Supplementary functions
28892889

28902890

2891-
def remove_na(series):
2892-
"""
2893-
Return series containing only true/non-NaN values, possibly empty.
2894-
"""
2895-
return series[notnull(_values_from_object(series))]
2896-
2897-
28982891
def _sanitize_index(data, index, copy=False):
28992892
""" sanitize an index type to return an ndarray of the underlying, pass
29002893
thru a non-Index

pandas/plotting/_core.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from pandas.util._decorators import cache_readonly
1313
from pandas.core.base import PandasObject
14-
from pandas.core.dtypes.missing import notnull
14+
from pandas.core.dtypes.missing import notnull, remove_na_arraylike
1515
from pandas.core.dtypes.common import (
1616
is_list_like,
1717
is_integer,
@@ -21,7 +21,7 @@
2121
from pandas.core.common import AbstractMethodError, isnull, _try_sort
2222
from pandas.core.generic import _shared_docs, _shared_doc_kwargs
2323
from pandas.core.index import Index, MultiIndex
24-
from pandas.core.series import Series, remove_na
24+
from pandas.core.series import Series
2525
from pandas.core.indexes.period import PeriodIndex
2626
from pandas.compat import range, lrange, map, zip, string_types
2727
import pandas.compat as compat
@@ -1376,7 +1376,7 @@ def _plot(cls, ax, y, style=None, bw_method=None, ind=None,
13761376
from scipy.stats import gaussian_kde
13771377
from scipy import __version__ as spv
13781378

1379-
y = remove_na(y)
1379+
y = remove_na_arraylike(y)
13801380

13811381
if LooseVersion(spv) >= '0.11.0':
13821382
gkde = gaussian_kde(y, bw_method=bw_method)
@@ -1495,13 +1495,13 @@ def _args_adjust(self):
14951495
@classmethod
14961496
def _plot(cls, ax, y, column_num=None, return_type='axes', **kwds):
14971497
if y.ndim == 2:
1498-
y = [remove_na(v) for v in y]
1498+
y = [remove_na_arraylike(v) for v in y]
14991499
# Boxplot fails with empty arrays, so need to add a NaN
15001500
# if any cols are empty
15011501
# GH 8181
15021502
y = [v if v.size > 0 else np.array([np.nan]) for v in y]
15031503
else:
1504-
y = remove_na(y)
1504+
y = remove_na_arraylike(y)
15051505
bp = ax.boxplot(y, **kwds)
15061506

15071507
if return_type == 'dict':
@@ -1969,7 +1969,7 @@ def maybe_color_bp(bp):
19691969

19701970
def plot_group(keys, values, ax):
19711971
keys = [pprint_thing(x) for x in keys]
1972-
values = [remove_na(v) for v in values]
1972+
values = [remove_na_arraylike(v) for v in values]
19731973
bp = ax.boxplot(values, **kwds)
19741974
if fontsize is not None:
19751975
ax.tick_params(axis='both', labelsize=fontsize)

pandas/tests/test_panel.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
import pandas as pd
1111

1212
from pandas.core.dtypes.common import is_float_dtype
13+
from pandas.core.dtypes.missing import remove_na_arraylike
1314
from pandas import (Series, DataFrame, Index, date_range, isnull, notnull,
1415
pivot, MultiIndex)
1516
from pandas.core.nanops import nanall, nanany
1617
from pandas.core.panel import Panel
17-
from pandas.core.series import remove_na
1818

1919
from pandas.io.formats.printing import pprint_thing
2020
from pandas import compat
@@ -155,7 +155,7 @@ def _check_stat_op(self, name, alternative, obj=None, has_skipna=True):
155155
if has_skipna:
156156

157157
def skipna_wrapper(x):
158-
nona = remove_na(x)
158+
nona = remove_na_arraylike(x)
159159
if len(nona) == 0:
160160
return np.nan
161161
return alternative(nona)

pandas/tests/test_panel4d.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import numpy as np
88

99
from pandas.core.dtypes.common import is_float_dtype
10+
from pandas.core.dtypes.missing import remove_na_arraylike
1011
from pandas import Series, Index, isnull, notnull
1112
from pandas.core.panel import Panel
1213
from pandas.core.panel4d import Panel4D
13-
from pandas.core.series import remove_na
1414
from pandas.tseries.offsets import BDay
1515

1616
from pandas.util.testing import (assert_frame_equal, assert_series_equal,
@@ -118,7 +118,7 @@ def _check_stat_op(self, name, alternative, obj=None, has_skipna=True):
118118

119119
if has_skipna:
120120
def skipna_wrapper(x):
121-
nona = remove_na(x)
121+
nona = remove_na_arraylike(x)
122122
if len(nona) == 0:
123123
return np.nan
124124
return alternative(nona)

0 commit comments

Comments
 (0)