Skip to content

Commit

Permalink
Merge pull request #6269 from jreback/perf2
Browse files Browse the repository at this point in the history
PERF: micro optimizations
  • Loading branch information
jreback committed Feb 6, 2014
2 parents 74195ac + b7ea3b5 commit cc6ee40
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 116 deletions.
26 changes: 7 additions & 19 deletions pandas/core/common.py
Expand Up @@ -134,7 +134,7 @@ def _isnull_new(obj):
elif isinstance(obj, (ABCSeries, np.ndarray)):
return _isnull_ndarraylike(obj)
elif isinstance(obj, ABCGeneric):
return obj._constructor(obj._data.apply(lambda x: isnull(x.values)))
return obj._constructor(obj._data.isnull(func=isnull))
elif isinstance(obj, list) or hasattr(obj, '__array__'):
return _isnull_ndarraylike(np.asarray(obj))
else:
Expand All @@ -160,8 +160,7 @@ def _isnull_old(obj):
elif isinstance(obj, (ABCSeries, np.ndarray)):
return _isnull_ndarraylike_old(obj)
elif isinstance(obj, ABCGeneric):
return obj._constructor(obj._data.apply(
lambda x: _isnull_old(x.values)))
return obj._constructor(obj._data.isnull(func=_isnull_old))
elif isinstance(obj, list) or hasattr(obj, '__array__'):
return _isnull_ndarraylike_old(np.asarray(obj))
else:
Expand Down Expand Up @@ -1540,14 +1539,7 @@ def _maybe_box(indexer, values, obj, key):
# return the value
return values


def _values_from_object(o):
""" return my values or the object if we are say an ndarray """
f = getattr(o, 'get_values', None)
if f is not None:
o = f()
return o

_values_from_object = lib.values_from_object

def _possibly_convert_objects(values, convert_dates=True,
convert_numeric=True,
Expand Down Expand Up @@ -2036,20 +2028,16 @@ def _maybe_make_list(obj):
return obj


def is_bool(obj):
return isinstance(obj, (bool, np.bool_))
is_bool = lib.is_bool


def is_integer(obj):
return isinstance(obj, (numbers.Integral, np.integer))
is_integer = lib.is_integer


def is_float(obj):
return isinstance(obj, (float, np.floating))
is_float = lib.is_float


def is_complex(obj):
return isinstance(obj, (numbers.Complex, np.complexfloating))
is_complex = lib.is_complex


def is_iterator(obj):
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/frame.py
Expand Up @@ -2825,14 +2825,14 @@ def _combine_match_columns(self, other, func, fill_value=None):
fill_value)

new_data = left._data.eval(
func, right, axes=[left.columns, self.index])
func=func, other=right, axes=[left.columns, self.index])
return self._constructor(new_data)

def _combine_const(self, other, func, raise_on_error=True):
if self.empty:
return self

new_data = self._data.eval(func, other, raise_on_error=raise_on_error)
new_data = self._data.eval(func=func, other=other, raise_on_error=raise_on_error)
return self._constructor(new_data)

def _compare_frame_evaluate(self, other, func, str_rep):
Expand Down Expand Up @@ -3228,7 +3228,7 @@ def diff(self, periods=1):
-------
diffed : DataFrame
"""
new_data = self._data.diff(periods)
new_data = self._data.diff(n=periods)
return self._constructor(new_data)

#----------------------------------------------------------------------
Expand Down
34 changes: 20 additions & 14 deletions pandas/core/generic.py
Expand Up @@ -128,7 +128,7 @@ def _init_mgr(self, mgr, axes=None, dtype=None, copy=False):
elif dtype is not None:
# avoid copy if we can
if len(mgr.blocks) > 1 or mgr.blocks[0].values.dtype != dtype:
mgr = mgr.astype(dtype)
mgr = mgr.astype(dtype=dtype)
return mgr

#----------------------------------------------------------------------
Expand Down Expand Up @@ -2011,7 +2011,7 @@ def astype(self, dtype, copy=True, raise_on_error=True):
"""

mgr = self._data.astype(
dtype, copy=copy, raise_on_error=raise_on_error)
dtype=dtype, copy=copy, raise_on_error=raise_on_error)
return self._constructor(mgr).__finalize__(self)

def copy(self, deep=True):
Expand Down Expand Up @@ -2153,7 +2153,7 @@ def fillna(self, value=None, method=None, axis=0, inplace=False,
from pandas import Series
value = Series(value)

new_data = self._data.fillna(value, inplace=inplace,
new_data = self._data.fillna(value=value, inplace=inplace,
downcast=downcast)

elif isinstance(value, (dict, com.ABCSeries)):
Expand All @@ -2170,7 +2170,7 @@ def fillna(self, value=None, method=None, axis=0, inplace=False,
obj.fillna(v, inplace=True)
return result
else:
new_data = self._data.fillna(value, inplace=inplace,
new_data = self._data.fillna(value=value, inplace=inplace,
downcast=downcast)

if inplace:
Expand Down Expand Up @@ -2355,7 +2355,8 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
new_data = self._data
for c, src in compat.iteritems(to_replace):
if c in value and c in self:
new_data = new_data.replace(src, value[c],
new_data = new_data.replace(to_replace=src,
value=value[c],
filter=[c],
inplace=inplace,
regex=regex)
Expand All @@ -2365,7 +2366,8 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
new_data = self._data
for k, src in compat.iteritems(to_replace):
if k in self:
new_data = new_data.replace(src, value,
new_data = new_data.replace(to_replace=src,
value=value,
filter=[k],
inplace=inplace,
regex=regex)
Expand All @@ -2380,13 +2382,16 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
'in length. Expecting %d got %d ' %
(len(to_replace), len(value)))

new_data = self._data.replace_list(to_replace, value,
new_data = self._data.replace_list(src_list=to_replace,
dest_list=value,
inplace=inplace,
regex=regex)

else: # [NA, ''] -> 0
new_data = self._data.replace(to_replace, value,
inplace=inplace, regex=regex)
new_data = self._data.replace(to_replace=to_replace,
value=value,
inplace=inplace,
regex=regex)
elif to_replace is None:
if not (com.is_re_compilable(regex) or
com.is_list_like(regex) or
Expand All @@ -2406,13 +2411,14 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,

for k, v in compat.iteritems(value):
if k in self:
new_data = new_data.replace(to_replace, v,
new_data = new_data.replace(to_replace=to_replace,
value=v,
filter=[k],
inplace=inplace,
regex=regex)

elif not com.is_list_like(value): # NA -> 0
new_data = self._data.replace(to_replace, value,
new_data = self._data.replace(to_replace=to_replace, value=value,
inplace=inplace, regex=regex)
else:
msg = ('Invalid "to_replace" type: '
Expand Down Expand Up @@ -3116,12 +3122,12 @@ def where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
if inplace:
# we may have different type blocks come out of putmask, so
# reconstruct the block manager
new_data = self._data.putmask(cond, other, align=axis is None,
new_data = self._data.putmask(mask=cond, new=other, align=axis is None,
inplace=True)
self._update_inplace(new_data)

else:
new_data = self._data.where(other, cond, align=axis is None,
new_data = self._data.where(other=other, cond=cond, align=axis is None,
raise_on_error=raise_on_error,
try_cast=try_cast)

Expand Down Expand Up @@ -3168,7 +3174,7 @@ def shift(self, periods=1, freq=None, axis=0, **kwds):
if freq is None and not len(kwds):
block_axis = self._get_block_manager_axis(axis)
indexer = com._shift_indexer(len(self), periods)
new_data = self._data.shift(indexer, periods, axis=block_axis)
new_data = self._data.shift(indexer=indexer, periods=periods, axis=block_axis)
else:
return self.tshift(periods, freq, **kwds)

Expand Down
6 changes: 3 additions & 3 deletions pandas/core/indexing.py
Expand Up @@ -321,7 +321,7 @@ def _setitem_with_indexer(self, indexer, value):
# as we select a slice indexer on the mi
idx = index._convert_slice_indexer(idx)
obj = obj.copy()
obj._data = obj._data.setitem(tuple([idx]), value)
obj._data = obj._data.setitem(indexer=tuple([idx]), value=value)
self.obj[item] = obj
return

Expand All @@ -341,7 +341,7 @@ def setter(item, v):

# set the item, possibly having a dtype change
s = s.copy()
s._data = s._data.setitem(pi, v)
s._data = s._data.setitem(indexer=pi, value=v)
s._maybe_update_cacher(clear=True)
self.obj[item] = s

Expand Down Expand Up @@ -419,7 +419,7 @@ def can_do_equal_len():
value = self._align_panel(indexer, value)

# actually do the set
self.obj._data = self.obj._data.setitem(indexer, value)
self.obj._data = self.obj._data.setitem(indexer=indexer, value=value)
self.obj._maybe_update_cacher(clear=True)

def _align_series(self, indexer, ser):
Expand Down

0 comments on commit cc6ee40

Please sign in to comment.