Skip to content

Commit

Permalink
TST: platform indexing, xref GH8007
Browse files Browse the repository at this point in the history
  • Loading branch information
jreback committed Aug 20, 2014
1 parent 489c394 commit e7e02e6
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions pandas/core/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from pandas.core.indexing import _is_null_slice
from pandas.tseries.period import PeriodIndex
import pandas.core.common as com

from pandas.core.common import isnull
from pandas.util.terminal import get_terminal_size
from pandas.core.config import get_option
from pandas.core import format as fmt
Expand Down Expand Up @@ -237,7 +239,7 @@ def __init__(self, values, levels=None, ordered=None, name=None, fastpath=False,
# On list with NaNs, int values will be converted to float. Use "object" dtype
# to prevent this. In the end objects will be casted to int/... in the level
# assignment step.
dtype = 'object' if com.isnull(values).any() else None
dtype = 'object' if isnull(values).any() else None
values = _sanitize_array(values, None, dtype=dtype)

if levels is None:
Expand Down Expand Up @@ -384,7 +386,7 @@ def _validate_levels(cls, levels):
levels = _convert_to_list_like(levels)
# on levels with NaNs, int values would be converted to float. Use "object" dtype
# to prevent this.
if com.isnull(levels).any():
if isnull(levels).any():
without_na = np.array([x for x in levels if com.notnull(x)])
with_na = np.array(levels)
if with_na.dtype != without_na.dtype:
Expand Down Expand Up @@ -513,9 +515,9 @@ def isnull(self):
# String/object and float levels can hold np.nan
if self.levels.dtype.kind in ['S', 'O', 'f']:
if np.nan in self.levels:
nan_pos = np.where(com.isnull(self.levels))
nan_pos = np.where(isnull(self.levels))[0]
# we only have one NA in levels
ret = np.logical_or(ret , self._codes == nan_pos[0])
ret = np.logical_or(ret , self._codes == nan_pos)
return ret

def notnull(self):
Expand Down Expand Up @@ -714,9 +716,9 @@ def fillna(self, fill_value=None, method=None, limit=None, **kwargs):
if self.levels.dtype.kind in ['S', 'O', 'f']:
if np.nan in self.levels:
values = values.copy()
nan_pos = np.where(com.isnull(self.levels))
nan_pos = np.where(isnull(self.levels))[0]
# we only have one NA in levels
values[values == nan_pos[0]] = -1
values[values == nan_pos] = -1


# pad / bfill
Expand Down Expand Up @@ -885,7 +887,7 @@ def __setitem__(self, key, value):
rvalue = value if com.is_list_like(value) else [value]
to_add = Index(rvalue)-self.levels
# no assignments of values not in levels, but it's always ok to set something to np.nan
if len(to_add) and not com.isnull(to_add).all():
if len(to_add) and not isnull(to_add).all():
raise ValueError("cannot setitem on a Categorical with a new level,"
" set the levels first")

Expand Down Expand Up @@ -924,8 +926,8 @@ def __setitem__(self, key, value):
# is fixed.
# float levels do currently return -1 for np.nan, even if np.nan is included in the index
# "repair" this here
if com.isnull(rvalue).any() and com.isnull(self.levels).any():
nan_pos = np.where(com.isnull(self.levels))
if isnull(rvalue).any() and isnull(self.levels).any():
nan_pos = np.where(com.isnull(self.levels))[0]
lindexer[lindexer == -1] = nan_pos

self._codes[key] = lindexer
Expand Down

0 comments on commit e7e02e6

Please sign in to comment.