Skip to content

Commit 859c80f

Browse files
Guilherme Beltraminijreback
Guilherme Beltramini
authored andcommitted
BUG: Reindex with columns and method
closes #14992 Author: Guilherme Beltramini <[email protected]> Closes #14993 from gcbeltramini/reindex and squashes the following commits: 9c9ad95 [Guilherme Beltramini] BUG: Add tests for sparse dataframe fdbd901 [Guilherme Beltramini] BUG: Tests for reindex with columns and methods 2b5724b [Guilherme Beltramini] BUG: Reindex with columns and method
1 parent a42a015 commit 859c80f

File tree

5 files changed

+121
-9
lines changed

5 files changed

+121
-9
lines changed

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ Bug Fixes
290290
- Bug in ``DataFrame(..).apply(to_numeric)`` when values are of type decimal.Decimal. (:issue:`14827`)
291291
- Bug in ``describe()`` when passing a numpy array which does not contain the median to the ``percentiles`` keyword argument (:issue:`14908`)
292292
- Bug in ``DataFrame.sort_values()`` when sorting by multiple columns where one column is of type ``int64`` and contains ``NaT`` (:issue:`14922`)
293+
- Bug in ``DataFrame.reindex()`` in which ``method`` was ignored when passing ``columns`` (:issue:`14992`)
293294

294295

295296
- Bug in ``pd.read_msgpack()`` in which ``Series`` categoricals were being improperly processed (:issue:`14901`)

pandas/core/frame.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -2725,8 +2725,8 @@ def _reindex_axes(self, axes, level, limit, tolerance, method, fill_value,
27252725

27262726
columns = axes['columns']
27272727
if columns is not None:
2728-
frame = frame._reindex_columns(columns, copy, level, fill_value,
2729-
limit, tolerance)
2728+
frame = frame._reindex_columns(columns, method, copy, level,
2729+
fill_value, limit, tolerance)
27302730

27312731
index = axes['index']
27322732
if index is not None:
@@ -2737,17 +2737,17 @@ def _reindex_axes(self, axes, level, limit, tolerance, method, fill_value,
27372737

27382738
def _reindex_index(self, new_index, method, copy, level, fill_value=NA,
27392739
limit=None, tolerance=None):
2740-
new_index, indexer = self.index.reindex(new_index, method, level,
2741-
limit=limit,
2740+
new_index, indexer = self.index.reindex(new_index, method=method,
2741+
level=level, limit=limit,
27422742
tolerance=tolerance)
27432743
return self._reindex_with_indexers({0: [new_index, indexer]},
27442744
copy=copy, fill_value=fill_value,
27452745
allow_dups=False)
27462746

2747-
def _reindex_columns(self, new_columns, copy, level, fill_value=NA,
2747+
def _reindex_columns(self, new_columns, method, copy, level, fill_value=NA,
27482748
limit=None, tolerance=None):
2749-
new_columns, indexer = self.columns.reindex(new_columns, level=level,
2750-
limit=limit,
2749+
new_columns, indexer = self.columns.reindex(new_columns, method=method,
2750+
level=level, limit=limit,
27512751
tolerance=tolerance)
27522752
return self._reindex_with_indexers({1: [new_columns, indexer]},
27532753
copy=copy, fill_value=fill_value,

pandas/sparse/frame.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,8 @@ def _reindex_index(self, index, method, copy, level, fill_value=np.nan,
571571
new_series, index=index, columns=self.columns,
572572
default_fill_value=self._default_fill_value).__finalize__(self)
573573

574-
def _reindex_columns(self, columns, copy, level, fill_value, limit=None,
575-
takeable=False):
574+
def _reindex_columns(self, columns, method, copy, level, fill_value=None,
575+
limit=None, takeable=False):
576576
if level is not None:
577577
raise TypeError('Reindex by level not supported for sparse')
578578

@@ -582,6 +582,9 @@ def _reindex_columns(self, columns, copy, level, fill_value, limit=None,
582582
if limit:
583583
raise NotImplementedError("'limit' argument is not supported")
584584

585+
if method is not None:
586+
raise NotImplementedError("'method' argument is not supported")
587+
585588
# TODO: fill value handling
586589
sdict = dict((k, v) for k, v in compat.iteritems(self) if k in columns)
587590
return self._constructor(

pandas/sparse/tests/test_frame.py

+70
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,76 @@ def test_reindex_fill_value(self):
799799
exp = exp.to_sparse(self.zframe.default_fill_value)
800800
tm.assert_sp_frame_equal(result, exp)
801801

802+
def test_reindex_method(self):
803+
804+
sparse = SparseDataFrame(data=[[11., 12., 14.],
805+
[21., 22., 24.],
806+
[41., 42., 44.]],
807+
index=[1, 2, 4],
808+
columns=[1, 2, 4],
809+
dtype=float)
810+
811+
# Over indices
812+
813+
# default method
814+
result = sparse.reindex(index=range(6))
815+
expected = SparseDataFrame(data=[[nan, nan, nan],
816+
[11., 12., 14.],
817+
[21., 22., 24.],
818+
[nan, nan, nan],
819+
[41., 42., 44.],
820+
[nan, nan, nan]],
821+
index=range(6),
822+
columns=[1, 2, 4],
823+
dtype=float)
824+
tm.assert_sp_frame_equal(result, expected)
825+
826+
# method='bfill'
827+
result = sparse.reindex(index=range(6), method='bfill')
828+
expected = SparseDataFrame(data=[[11., 12., 14.],
829+
[11., 12., 14.],
830+
[21., 22., 24.],
831+
[41., 42., 44.],
832+
[41., 42., 44.],
833+
[nan, nan, nan]],
834+
index=range(6),
835+
columns=[1, 2, 4],
836+
dtype=float)
837+
tm.assert_sp_frame_equal(result, expected)
838+
839+
# method='ffill'
840+
result = sparse.reindex(index=range(6), method='ffill')
841+
expected = SparseDataFrame(data=[[nan, nan, nan],
842+
[11., 12., 14.],
843+
[21., 22., 24.],
844+
[21., 22., 24.],
845+
[41., 42., 44.],
846+
[41., 42., 44.]],
847+
index=range(6),
848+
columns=[1, 2, 4],
849+
dtype=float)
850+
tm.assert_sp_frame_equal(result, expected)
851+
852+
# Over columns
853+
854+
# default method
855+
result = sparse.reindex(columns=range(6))
856+
expected = SparseDataFrame(data=[[nan, 11., 12., nan, 14., nan],
857+
[nan, 21., 22., nan, 24., nan],
858+
[nan, 41., 42., nan, 44., nan]],
859+
index=[1, 2, 4],
860+
columns=range(6),
861+
dtype=float)
862+
tm.assert_sp_frame_equal(result, expected)
863+
864+
# method='bfill'
865+
with tm.assertRaises(NotImplementedError):
866+
sparse.reindex(columns=range(6), method='bfill')
867+
868+
# method='ffill'
869+
with tm.assertRaises(NotImplementedError):
870+
sparse.reindex(columns=range(6), method='ffill')
871+
802872
def test_take(self):
803873
result = self.frame.take([1, 0, 2], axis=1)
804874
expected = self.frame.reindex(columns=['B', 'A', 'C'])

pandas/tests/frame/test_axis_select_reindex.py

+38
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,44 @@ def test_reindex_columns(self):
297297
newFrame = self.frame.reindex(columns=[])
298298
self.assertTrue(newFrame.empty)
299299

300+
def test_reindex_columns_method(self):
301+
302+
# GH 14992, reindexing over columns ignored method
303+
df = DataFrame(data=[[11, 12, 13], [21, 22, 23], [31, 32, 33]],
304+
index=[1, 2, 4],
305+
columns=[1, 2, 4],
306+
dtype=float)
307+
308+
# default method
309+
result = df.reindex(columns=range(6))
310+
expected = DataFrame(data=[[np.nan, 11, 12, np.nan, 13, np.nan],
311+
[np.nan, 21, 22, np.nan, 23, np.nan],
312+
[np.nan, 31, 32, np.nan, 33, np.nan]],
313+
index=[1, 2, 4],
314+
columns=range(6),
315+
dtype=float)
316+
assert_frame_equal(result, expected)
317+
318+
# method='ffill'
319+
result = df.reindex(columns=range(6), method='ffill')
320+
expected = DataFrame(data=[[np.nan, 11, 12, 12, 13, 13],
321+
[np.nan, 21, 22, 22, 23, 23],
322+
[np.nan, 31, 32, 32, 33, 33]],
323+
index=[1, 2, 4],
324+
columns=range(6),
325+
dtype=float)
326+
assert_frame_equal(result, expected)
327+
328+
# method='bfill'
329+
result = df.reindex(columns=range(6), method='bfill')
330+
expected = DataFrame(data=[[11, 11, 12, 13, 13, np.nan],
331+
[21, 21, 22, 23, 23, np.nan],
332+
[31, 31, 32, 33, 33, np.nan]],
333+
index=[1, 2, 4],
334+
columns=range(6),
335+
dtype=float)
336+
assert_frame_equal(result, expected)
337+
300338
def test_reindex_axes(self):
301339
# GH 3317, reindexing by both axes loses freq of the index
302340
df = DataFrame(np.ones((3, 3)),

0 commit comments

Comments
 (0)