Skip to content

Commit 340da19

Browse files
committed
STEP #1 in introducing new API for interpolate_at(), low-level backwards compatible changes to add new_x as input
1 parent 44e7e44 commit 340da19

File tree

1 file changed

+43
-25
lines changed

1 file changed

+43
-25
lines changed

pandas/core/common.py

+43-25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""
1+
"""
22
Misc tools for implementing data structures
33
"""
44

@@ -1587,7 +1587,7 @@ def _clean_interp_method(method, **kwargs):
15871587
return method
15881588

15891589

1590-
def interpolate_1d(xvalues, yvalues, method='linear', limit=None,
1590+
def interpolate_1d(xvalues, yvalues, new_x=None, method='linear', limit=None,
15911591
fill_value=None, bounds_error=False, order=None, **kwargs):
15921592
"""
15931593
Logic for the 1-d interpolation. The result should be 1-d, inputs
@@ -1597,16 +1597,22 @@ def interpolate_1d(xvalues, yvalues, method='linear', limit=None,
15971597
take it as an argumnet.
15981598
"""
15991599
# Treat the original, non-scipy methods first.
1600-
1601-
invalid = isnull(yvalues)
1602-
valid = ~invalid
1603-
1604-
valid_y = yvalues[valid]
1605-
valid_x = xvalues[valid]
1606-
new_x = xvalues[invalid]
1600+
invalid = None
1601+
if new_x:
1602+
# TODO - interp on existing nans in yvalues?
1603+
valid = notnull(yvalues)
1604+
valid_y = yvalues[valid]
1605+
valid_x = xvalues[valid]
1606+
else:
1607+
invalid = isnull(yvalues)
1608+
valid = ~invalid
1609+
new_x = xvalues[invalid]
1610+
valid_y = yvalues[valid]
1611+
valid_x = xvalues[valid]
16071612

16081613
if method == 'time':
1609-
if not getattr(xvalues, 'is_all_dates', None):
1614+
if (not getattr(xvalues, 'is_all_dates', None)) or ((not
1615+
(getattr(new_x, 'is_all_dates', None)) if not invalid else True)):
16101616
# if not issubclass(xvalues.dtype.type, np.datetime64):
16111617
raise ValueError('time-weighted interpolation only works '
16121618
'on Series or DataFrames with a '
@@ -1625,53 +1631,65 @@ def _interp_limit(invalid, limit):
16251631
xvalues = getattr(xvalues, 'values', xvalues)
16261632
yvalues = getattr(yvalues, 'values', yvalues)
16271633

1628-
if limit:
1634+
# TODO: should 'limit' be valid option if new_x is given
1635+
# TODO: if yes, then 'invalid' has to be calc.
1636+
if limit and invalid:
16291637
violate_limit = _interp_limit(invalid, limit)
16301638
if valid.any():
16311639
firstIndex = valid.argmax()
16321640
valid = valid[firstIndex:]
1633-
invalid = invalid[firstIndex:]
1634-
result = yvalues.copy()
1635-
if valid.all():
1636-
return yvalues
1641+
if invalid:
1642+
invalid = invalid[firstIndex:]
1643+
result = yvalues.copy()
1644+
if valid.all():
1645+
return yvalues
16371646
else:
16381647
# have to call np.array(xvalues) since xvalues could be an Index
16391648
# which cant be mutated
1640-
result = np.empty_like(np.array(xvalues), dtype=np.float64)
1649+
1650+
result = np.empty_like(np.array(
1651+
xvalues if invalid else new_x), dtype=np.float64)
16411652
result.fill(np.nan)
16421653
return result
16431654

16441655
if method in ['linear', 'time', 'index', 'values']:
16451656
if method in ('values', 'index'):
1646-
inds = np.asarray(xvalues)
1657+
inds = np.asarray(xvalues if invalid else new_x)
16471658
# hack for DatetimeIndex, #1646
16481659
if issubclass(inds.dtype.type, np.datetime64):
16491660
inds = inds.view(np.int64)
16501661

16511662
if inds.dtype == np.object_:
16521663
inds = lib.maybe_convert_objects(inds)
16531664
else:
1654-
inds = xvalues
1655-
1656-
inds = inds[firstIndex:]
1665+
inds = xvalues if invalid else new_x
16571666

1658-
result[firstIndex:][invalid] = np.interp(inds[invalid], inds[valid],
1667+
if not invalid:
1668+
result = np.interp(new_x, valid_x, valid_y)
1669+
else:
1670+
inds = inds[firstIndex:]
1671+
result[firstIndex:][invalid] = np.interp(inds[invalid], inds[valid],
16591672
yvalues[firstIndex:][valid])
16601673

1661-
if limit:
1674+
if limit and invalid:
16621675
result[violate_limit] = np.nan
16631676
return result
16641677

16651678
sp_methods = ['nearest', 'zero', 'slinear', 'quadratic', 'cubic',
16661679
'barycentric', 'krogh', 'spline', 'polynomial',
16671680
'piecewise_polynomial', 'pchip']
16681681
if method in sp_methods:
1669-
new_x = new_x[firstIndex:]
1682+
if invalid:
1683+
new_x = new_x[firstIndex:]
16701684

1671-
result[firstIndex:][invalid] = _interpolate_scipy_wrapper(
1685+
_temp_res = _interpolate_scipy_wrapper(
16721686
valid_x, valid_y, new_x, method=method, fill_value=fill_value,
16731687
bounds_error=bounds_error, order=order, **kwargs)
1674-
if limit:
1688+
if invalid:
1689+
result[firstIndex:][invalid] = _temp_res
1690+
else:
1691+
result = _temp_res
1692+
if limit and invalid:
16751693
result[violate_limit] = np.nan
16761694
return result
16771695

0 commit comments

Comments
 (0)