Skip to content

Commit 407e904

Browse files
committed
CLN: refactored out take from core/frame.py to use core/generic.py version
1 parent 3f76205 commit 407e904

File tree

6 files changed

+49
-56
lines changed

6 files changed

+49
-56
lines changed

doc/source/release.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ pandas 0.13
151151
- ``Series.isin()`` and ``DataFrame.isin()`` now raise a ``TypeError`` when
152152
passed a string (:issue:`4763`). Pass a ``list`` of one element (containing
153153
the string) instead.
154+
- Remove undocumented/unused ``kind`` keyword argument from ``read_excel``, and ``ExcelFile``. (:issue:`4713`, :issue:`4712`)
154155

155156
**Internal Refactoring**
156157

@@ -172,7 +173,7 @@ See :ref:`Internal Refactoring<whatsnew_0130.refactoring>`
172173
- ``_indexed_same,reindex_like,align,where,mask``
173174
- ``fillna,replace`` (``Series`` replace is now consistent with ``DataFrame``)
174175
- ``filter`` (also added axis argument to selectively filter on a different axis)
175-
- ``reindex,reindex_axis`` (which was the biggest change to make generic)
176+
- ``reindex,reindex_axis,take``
176177
- ``truncate`` (moved to become part of ``NDFrame``)
177178

178179
- These are API changes which make ``Panel`` more consistent with ``DataFrame``
@@ -224,7 +225,6 @@ See :ref:`Internal Refactoring<whatsnew_0130.refactoring>`
224225
- Refactor of ``_get_numeric_data/_get_bool_data`` to core/generic.py, allowing Series/Panel functionaility
225226
- Refactor of Series arithmetic with time-like objects (datetime/timedelta/time
226227
etc.) into a separate, cleaned up wrapper class. (:issue:`4613`)
227-
- Remove undocumented/unused ``kind`` keyword argument from ``read_excel``, and ``ExcelFile``. (:issue:`4713`, :issue:`4712`)
228228

229229
**Experimental Features**
230230

doc/source/v0.13.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ and behaviors. Series formerly subclassed directly from ``ndarray``. (:issue:`40
297297
- ``_indexed_same,reindex_like,align,where,mask``
298298
- ``fillna,replace`` (``Series`` replace is now consistent with ``DataFrame``)
299299
- ``filter`` (also added axis argument to selectively filter on a different axis)
300-
- ``reindex,reindex_axis`` (which was the biggest change to make generic)
300+
- ``reindex,reindex_axis,take``
301301
- ``truncate`` (moved to become part of ``NDFrame``)
302302

303303
- These are API changes which make ``Panel`` more consistent with ``DataFrame``

pandas/core/frame.py

-42
Original file line numberDiff line numberDiff line change
@@ -2513,48 +2513,6 @@ def _maybe_cast(values, labels=None):
25132513

25142514
delevel = deprecate('delevel', reset_index)
25152515

2516-
def take(self, indices, axis=0, convert=True):
2517-
"""
2518-
Analogous to ndarray.take, return DataFrame corresponding to requested
2519-
indices along an axis
2520-
2521-
Parameters
2522-
----------
2523-
indices : list / array of ints
2524-
axis : {0, 1}
2525-
convert : convert indices for negative values, check bounds, default True
2526-
mainly useful for an user routine calling
2527-
2528-
Returns
2529-
-------
2530-
taken : DataFrame
2531-
"""
2532-
2533-
# check/convert indicies here
2534-
if convert:
2535-
axis = self._get_axis_number(axis)
2536-
indices = _maybe_convert_indices(
2537-
indices, len(self._get_axis(axis)))
2538-
2539-
if self._is_mixed_type:
2540-
if axis == 0:
2541-
new_data = self._data.take(indices, axis=1, verify=False)
2542-
return DataFrame(new_data)
2543-
else:
2544-
return self.reindex(columns=indices, takeable=True)
2545-
else:
2546-
new_values = com.take_nd(self.values,
2547-
com._ensure_int64(indices),
2548-
axis=axis)
2549-
if axis == 0:
2550-
new_columns = self.columns
2551-
new_index = self.index.take(indices)
2552-
else:
2553-
new_columns = self.columns.take(indices)
2554-
new_index = self.index
2555-
return self._constructor(new_values, index=new_index,
2556-
columns=new_columns)
2557-
25582516
#----------------------------------------------------------------------
25592517
# Reindex-based selection methods
25602518

pandas/core/generic.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -862,12 +862,13 @@ def take(self, indices, axis=0, convert=True):
862862
indices = _maybe_convert_indices(
863863
indices, len(self._get_axis(axis)))
864864

865-
if axis == 0:
865+
baxis = self._get_block_manager_axis(axis)
866+
if baxis == 0:
866867
labels = self._get_axis(axis)
867868
new_items = labels.take(indices)
868-
new_data = self._data.reindex_axis(new_items, axis=0)
869+
new_data = self._data.reindex_axis(new_items, indexer=indices, axis=0)
869870
else:
870-
new_data = self._data.take(indices, axis=axis, verify=False)
871+
new_data = self._data.take(indices, axis=baxis)
871872
return self._constructor(new_data)
872873

873874
def select(self, crit, axis=0):

pandas/core/internals.py

+39-7
Original file line numberDiff line numberDiff line change
@@ -1516,15 +1516,28 @@ def reindex_items_from(self, new_ref_items, indexer=None, method=None, fill_valu
15161516
if indexer is None:
15171517
indexer = np.arange(len(self.items))
15181518

1519-
new_values = com.take_1d(self.values.values, indexer)
1519+
# single block
1520+
if self.ndim == 1:
1521+
1522+
new_items = new_ref_items
1523+
new_values = com.take_1d(self.values.values, indexer)
1524+
1525+
else:
1526+
1527+
# if we don't overlap at all, then don't include this block
1528+
new_items = self.items & new_ref_items
1529+
if not len(new_items):
1530+
return None
1531+
1532+
new_values = self.values.values
15201533

15211534
# fill if needed
15221535
if method is not None or limit is not None:
15231536
if fill_value is None:
15241537
fill_value = self.fill_value
15251538
new_values = com.interpolate_2d(new_values, method=method, limit=limit, fill_value=fill_value)
15261539

1527-
return self.make_block(new_values, items=new_ref_items, ref_items=new_ref_items, copy=copy)
1540+
return self.make_block(new_values, items=new_items, ref_items=new_ref_items, copy=copy)
15281541

15291542
def sparse_reindex(self, new_index):
15301543
""" sparse reindex and return a new block
@@ -2794,15 +2807,34 @@ def reindex_items(self, new_items, indexer=None, copy=True, fill_value=None):
27942807
if indexer is None:
27952808
for blk in self.blocks:
27962809
if copy:
2797-
new_blocks.append(blk.reindex_items_from(new_items))
2810+
blk = blk.reindex_items_from(new_items)
27982811
else:
27992812
blk.ref_items = new_items
2813+
if blk is not None:
28002814
new_blocks.append(blk)
28012815
else:
2802-
for block in self.blocks:
2803-
newb = block.reindex_items_from(new_items, copy=copy)
2804-
if len(newb.items) > 0:
2805-
new_blocks.append(newb)
2816+
2817+
# unique
2818+
if self.axes[0].is_unique:
2819+
for block in self.blocks:
2820+
2821+
newb = block.reindex_items_from(new_items, copy=copy)
2822+
if newb is not None and len(newb.items) > 0:
2823+
new_blocks.append(newb)
2824+
2825+
# non-unique
2826+
else:
2827+
rl = self._set_ref_locs()
2828+
for i, idx in enumerate(indexer):
2829+
blk, lidx = rl[idx]
2830+
item = new_items.take([i])
2831+
blk = make_block(_block_shape(blk.iget(lidx)),
2832+
item,
2833+
new_items,
2834+
ndim=self.ndim,
2835+
fastpath=True,
2836+
placement = [i])
2837+
new_blocks.append(blk)
28062838

28072839
# add a na block if we are missing items
28082840
mask = indexer == -1

pandas/sparse/tests/test_sparse.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,9 @@ def test_getitem_slice(self):
384384
idx = self.bseries.index
385385
res = self.bseries[::2]
386386
tm.assert_isinstance(res, SparseSeries)
387-
assert_sp_series_equal(res, self.bseries.reindex(idx[::2]))
387+
388+
expected = self.bseries.reindex(idx[::2])
389+
assert_sp_series_equal(res, expected)
388390

389391
res = self.bseries[:5]
390392
tm.assert_isinstance(res, SparseSeries)

0 commit comments

Comments
 (0)