Skip to content

Commit e7e5621

Browse files
committed
PERF: enhance tseries getitem indexing
PERF: micro optimzations in inference API/CLN: core.internals.apply now only takes kwargs; makes code a bit simpler CLN: wrap core/common/isnull into more direct Block.isnull PERF: fast path series block operations
1 parent 74195ac commit e7e5621

12 files changed

+152
-116
lines changed

pandas/core/common.py

+7-19
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def _isnull_new(obj):
134134
elif isinstance(obj, (ABCSeries, np.ndarray)):
135135
return _isnull_ndarraylike(obj)
136136
elif isinstance(obj, ABCGeneric):
137-
return obj._constructor(obj._data.apply(lambda x: isnull(x.values)))
137+
return obj._constructor(obj._data.isnull(func=isnull))
138138
elif isinstance(obj, list) or hasattr(obj, '__array__'):
139139
return _isnull_ndarraylike(np.asarray(obj))
140140
else:
@@ -160,8 +160,7 @@ def _isnull_old(obj):
160160
elif isinstance(obj, (ABCSeries, np.ndarray)):
161161
return _isnull_ndarraylike_old(obj)
162162
elif isinstance(obj, ABCGeneric):
163-
return obj._constructor(obj._data.apply(
164-
lambda x: _isnull_old(x.values)))
163+
return obj._constructor(obj._data.isnull(func=_isnull_old))
165164
elif isinstance(obj, list) or hasattr(obj, '__array__'):
166165
return _isnull_ndarraylike_old(np.asarray(obj))
167166
else:
@@ -1540,14 +1539,7 @@ def _maybe_box(indexer, values, obj, key):
15401539
# return the value
15411540
return values
15421541

1543-
1544-
def _values_from_object(o):
1545-
""" return my values or the object if we are say an ndarray """
1546-
f = getattr(o, 'get_values', None)
1547-
if f is not None:
1548-
o = f()
1549-
return o
1550-
1542+
_values_from_object = lib.values_from_object
15511543

15521544
def _possibly_convert_objects(values, convert_dates=True,
15531545
convert_numeric=True,
@@ -2036,20 +2028,16 @@ def _maybe_make_list(obj):
20362028
return obj
20372029

20382030

2039-
def is_bool(obj):
2040-
return isinstance(obj, (bool, np.bool_))
2031+
is_bool = lib.is_bool
20412032

20422033

2043-
def is_integer(obj):
2044-
return isinstance(obj, (numbers.Integral, np.integer))
2034+
is_integer = lib.is_integer
20452035

20462036

2047-
def is_float(obj):
2048-
return isinstance(obj, (float, np.floating))
2037+
is_float = lib.is_float
20492038

20502039

2051-
def is_complex(obj):
2052-
return isinstance(obj, (numbers.Complex, np.complexfloating))
2040+
is_complex = lib.is_complex
20532041

20542042

20552043
def is_iterator(obj):

pandas/core/frame.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2825,14 +2825,14 @@ def _combine_match_columns(self, other, func, fill_value=None):
28252825
fill_value)
28262826

28272827
new_data = left._data.eval(
2828-
func, right, axes=[left.columns, self.index])
2828+
func=func, other=right, axes=[left.columns, self.index])
28292829
return self._constructor(new_data)
28302830

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

2835-
new_data = self._data.eval(func, other, raise_on_error=raise_on_error)
2835+
new_data = self._data.eval(func=func, other=other, raise_on_error=raise_on_error)
28362836
return self._constructor(new_data)
28372837

28382838
def _compare_frame_evaluate(self, other, func, str_rep):
@@ -3228,7 +3228,7 @@ def diff(self, periods=1):
32283228
-------
32293229
diffed : DataFrame
32303230
"""
3231-
new_data = self._data.diff(periods)
3231+
new_data = self._data.diff(n=periods)
32323232
return self._constructor(new_data)
32333233

32343234
#----------------------------------------------------------------------

pandas/core/generic.py

+20-14
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def _init_mgr(self, mgr, axes=None, dtype=None, copy=False):
128128
elif dtype is not None:
129129
# avoid copy if we can
130130
if len(mgr.blocks) > 1 or mgr.blocks[0].values.dtype != dtype:
131-
mgr = mgr.astype(dtype)
131+
mgr = mgr.astype(dtype=dtype)
132132
return mgr
133133

134134
#----------------------------------------------------------------------
@@ -2011,7 +2011,7 @@ def astype(self, dtype, copy=True, raise_on_error=True):
20112011
"""
20122012

20132013
mgr = self._data.astype(
2014-
dtype, copy=copy, raise_on_error=raise_on_error)
2014+
dtype=dtype, copy=copy, raise_on_error=raise_on_error)
20152015
return self._constructor(mgr).__finalize__(self)
20162016

20172017
def copy(self, deep=True):
@@ -2153,7 +2153,7 @@ def fillna(self, value=None, method=None, axis=0, inplace=False,
21532153
from pandas import Series
21542154
value = Series(value)
21552155

2156-
new_data = self._data.fillna(value, inplace=inplace,
2156+
new_data = self._data.fillna(value=value, inplace=inplace,
21572157
downcast=downcast)
21582158

21592159
elif isinstance(value, (dict, com.ABCSeries)):
@@ -2170,7 +2170,7 @@ def fillna(self, value=None, method=None, axis=0, inplace=False,
21702170
obj.fillna(v, inplace=True)
21712171
return result
21722172
else:
2173-
new_data = self._data.fillna(value, inplace=inplace,
2173+
new_data = self._data.fillna(value=value, inplace=inplace,
21742174
downcast=downcast)
21752175

21762176
if inplace:
@@ -2355,7 +2355,8 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
23552355
new_data = self._data
23562356
for c, src in compat.iteritems(to_replace):
23572357
if c in value and c in self:
2358-
new_data = new_data.replace(src, value[c],
2358+
new_data = new_data.replace(to_replace=src,
2359+
value=value[c],
23592360
filter=[c],
23602361
inplace=inplace,
23612362
regex=regex)
@@ -2365,7 +2366,8 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
23652366
new_data = self._data
23662367
for k, src in compat.iteritems(to_replace):
23672368
if k in self:
2368-
new_data = new_data.replace(src, value,
2369+
new_data = new_data.replace(to_replace=src,
2370+
value=value,
23692371
filter=[k],
23702372
inplace=inplace,
23712373
regex=regex)
@@ -2380,13 +2382,16 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
23802382
'in length. Expecting %d got %d ' %
23812383
(len(to_replace), len(value)))
23822384

2383-
new_data = self._data.replace_list(to_replace, value,
2385+
new_data = self._data.replace_list(src_list=to_replace,
2386+
dest_list=value,
23842387
inplace=inplace,
23852388
regex=regex)
23862389

23872390
else: # [NA, ''] -> 0
2388-
new_data = self._data.replace(to_replace, value,
2389-
inplace=inplace, regex=regex)
2391+
new_data = self._data.replace(to_replace=to_replace,
2392+
value=value,
2393+
inplace=inplace,
2394+
regex=regex)
23902395
elif to_replace is None:
23912396
if not (com.is_re_compilable(regex) or
23922397
com.is_list_like(regex) or
@@ -2406,13 +2411,14 @@ def replace(self, to_replace=None, value=None, inplace=False, limit=None,
24062411

24072412
for k, v in compat.iteritems(value):
24082413
if k in self:
2409-
new_data = new_data.replace(to_replace, v,
2414+
new_data = new_data.replace(to_replace=to_replace,
2415+
value=v,
24102416
filter=[k],
24112417
inplace=inplace,
24122418
regex=regex)
24132419

24142420
elif not com.is_list_like(value): # NA -> 0
2415-
new_data = self._data.replace(to_replace, value,
2421+
new_data = self._data.replace(to_replace=to_replace, value=value,
24162422
inplace=inplace, regex=regex)
24172423
else:
24182424
msg = ('Invalid "to_replace" type: '
@@ -3116,12 +3122,12 @@ def where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
31163122
if inplace:
31173123
# we may have different type blocks come out of putmask, so
31183124
# reconstruct the block manager
3119-
new_data = self._data.putmask(cond, other, align=axis is None,
3125+
new_data = self._data.putmask(mask=cond, new=other, align=axis is None,
31203126
inplace=True)
31213127
self._update_inplace(new_data)
31223128

31233129
else:
3124-
new_data = self._data.where(other, cond, align=axis is None,
3130+
new_data = self._data.where(other=other, cond=cond, align=axis is None,
31253131
raise_on_error=raise_on_error,
31263132
try_cast=try_cast)
31273133

@@ -3168,7 +3174,7 @@ def shift(self, periods=1, freq=None, axis=0, **kwds):
31683174
if freq is None and not len(kwds):
31693175
block_axis = self._get_block_manager_axis(axis)
31703176
indexer = com._shift_indexer(len(self), periods)
3171-
new_data = self._data.shift(indexer, periods, axis=block_axis)
3177+
new_data = self._data.shift(indexer=indexer, periods=periods, axis=block_axis)
31723178
else:
31733179
return self.tshift(periods, freq, **kwds)
31743180

pandas/core/indexing.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ def _setitem_with_indexer(self, indexer, value):
321321
# as we select a slice indexer on the mi
322322
idx = index._convert_slice_indexer(idx)
323323
obj = obj.copy()
324-
obj._data = obj._data.setitem(tuple([idx]), value)
324+
obj._data = obj._data.setitem(indexer=tuple([idx]), value=value)
325325
self.obj[item] = obj
326326
return
327327

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

342342
# set the item, possibly having a dtype change
343343
s = s.copy()
344-
s._data = s._data.setitem(pi, v)
344+
s._data = s._data.setitem(indexer=pi, value=v)
345345
s._maybe_update_cacher(clear=True)
346346
self.obj[item] = s
347347

@@ -419,7 +419,7 @@ def can_do_equal_len():
419419
value = self._align_panel(indexer, value)
420420

421421
# actually do the set
422-
self.obj._data = self.obj._data.setitem(indexer, value)
422+
self.obj._data = self.obj._data.setitem(indexer=indexer, value=value)
423423
self.obj._maybe_update_cacher(clear=True)
424424

425425
def _align_series(self, indexer, ser):

0 commit comments

Comments
 (0)