Skip to content

REGR: Bug in Series.reindex when specifying a method with some nan values was inconsistent (GH6418) #6421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ Bug Fixes
- ``Float64Index`` with nans not comparing correctly
- ``eval``/``query`` expressions with strings containing the ``@`` character
will now work (:issue:`6366`).
- Bug in ``Series.reindex`` when specifying a ``method`` with some nan values was inconsistent (noted on a resample) (:issue:`6418`)

pandas 0.13.1
-------------
Expand Down
20 changes: 3 additions & 17 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1589,23 +1589,9 @@ def _reindex_axes(self, axes, level, limit, method, fill_value, copy,

axis = self._get_axis_number(a)
ax = self._get_axis(a)
try:
new_index, indexer = ax.reindex(
labels, level=level, limit=limit, method=method,
takeable=takeable)
except (ValueError):

# catch trying to reindex a non-monotonic index with a
# specialized indexer e.g. pad, so fallback to the regular
# indexer this will show up on reindexing a not-naturally
# ordering series,
# e.g.
# Series(
# [1,2,3,4], index=['a','b','c','d']
# ).reindex(['c','b','g'], method='pad')
new_index, indexer = ax.reindex(
labels, level=level, limit=limit, method=None,
takeable=takeable)
new_index, indexer = ax.reindex(
labels, level=level, limit=limit, method=method,
takeable=takeable)

obj = obj._reindex_with_indexers(
{axis: [new_index, indexer]}, method=method,
Expand Down
9 changes: 5 additions & 4 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def reindex_items_from(self, new_ref_items, indexer=None, method=None,
new_ref_items, indexer = self.items.reindex(new_ref_items,
limit=limit)

needs_fill = method is not None and limit is None
needs_fill = method is not None
if fill_value is None:
fill_value = self.fill_value

Expand All @@ -275,10 +275,13 @@ def reindex_items_from(self, new_ref_items, indexer=None, method=None,

else:

# single block reindex
# single block reindex, filling is already happending
if self.ndim == 1:
new_values = com.take_1d(self.values, indexer,
fill_value=fill_value)
block = make_block(new_values, new_items, new_ref_items,
ndim=self.ndim, fastpath=True)
return block
else:

masked_idx = indexer[indexer != -1]
Expand Down Expand Up @@ -3705,8 +3708,6 @@ def _reindex_indexer_items(self, new_items, indexer, fill_value):

def reindex_axis0_with_method(self, new_axis, indexer=None, method=None,
fill_value=None, limit=None, copy=True):
if method is None:
indexer = None
return self.reindex(new_axis, indexer=indexer, method=method,
fill_value=fill_value, limit=limit, copy=copy)

Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -4999,9 +4999,8 @@ def test_reindex_pad(self):
result = s.reindex(new_index).ffill(downcast='infer')
assert_series_equal(result, expected)

# this preserves dtype
result = s.reindex(new_index, method='ffill')
assert_series_equal(result, expected)
# invalid because we can't forward fill on this type of index
self.assertRaises(ValueError, lambda : s.reindex(new_index, method='ffill'))

# inferrence of new dtype
s = Series([True,False,False,True],index=list('abcd'))
Expand Down
22 changes: 22 additions & 0 deletions pandas/tseries/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,28 @@ def test_resample_unequal_times(self):
# it works!
df.resample('AS', 'sum')

def test_resample_consistency(self):

# GH 6418
# resample with bfill / limit / reindex consistency

i30 = index=pd.date_range('2002-02-02', periods=4, freq='30T')
s=pd.Series(np.arange(4.), index=i30)
s[2] = np.NaN

# Upsample by factor 3 with reindex() and resample() methods:
i10 = pd.date_range(i30[0], i30[-1], freq='10T')

s10 = s.reindex(index=i10, method='bfill')
s10_2 = s.reindex(index=i10, method='bfill', limit=2)
rl = s.reindex_like(s10, method='bfill', limit=2)
r10_2 = s.resample('10Min', fill_method='bfill', limit=2)
r10 = s.resample('10Min', fill_method='bfill')

# s10_2, r10, r10_2, rl should all be equal
assert_series_equal(s10_2, r10)
assert_series_equal(s10_2, r10_2)
assert_series_equal(s10_2, rl)

def _simple_ts(start, end, freq='D'):
rng = date_range(start, end, freq=freq)
Expand Down