1
- """
1
+ """
2
2
Misc tools for implementing data structures
3
3
"""
4
4
@@ -1587,7 +1587,7 @@ def _clean_interp_method(method, **kwargs):
1587
1587
return method
1588
1588
1589
1589
1590
- def interpolate_1d (xvalues , yvalues , method = 'linear' , limit = None ,
1590
+ def interpolate_1d (xvalues , yvalues , new_x = None , method = 'linear' , limit = None ,
1591
1591
fill_value = None , bounds_error = False , order = None , ** kwargs ):
1592
1592
"""
1593
1593
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,
1597
1597
take it as an argumnet.
1598
1598
"""
1599
1599
# 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 ]
1607
1612
1608
1613
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 )):
1610
1616
# if not issubclass(xvalues.dtype.type, np.datetime64):
1611
1617
raise ValueError ('time-weighted interpolation only works '
1612
1618
'on Series or DataFrames with a '
@@ -1625,53 +1631,65 @@ def _interp_limit(invalid, limit):
1625
1631
xvalues = getattr (xvalues , 'values' , xvalues )
1626
1632
yvalues = getattr (yvalues , 'values' , yvalues )
1627
1633
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 :
1629
1637
violate_limit = _interp_limit (invalid , limit )
1630
1638
if valid .any ():
1631
1639
firstIndex = valid .argmax ()
1632
1640
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
1637
1646
else :
1638
1647
# have to call np.array(xvalues) since xvalues could be an Index
1639
1648
# 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 )
1641
1652
result .fill (np .nan )
1642
1653
return result
1643
1654
1644
1655
if method in ['linear' , 'time' , 'index' , 'values' ]:
1645
1656
if method in ('values' , 'index' ):
1646
- inds = np .asarray (xvalues )
1657
+ inds = np .asarray (xvalues if invalid else new_x )
1647
1658
# hack for DatetimeIndex, #1646
1648
1659
if issubclass (inds .dtype .type , np .datetime64 ):
1649
1660
inds = inds .view (np .int64 )
1650
1661
1651
1662
if inds .dtype == np .object_ :
1652
1663
inds = lib .maybe_convert_objects (inds )
1653
1664
else :
1654
- inds = xvalues
1655
-
1656
- inds = inds [firstIndex :]
1665
+ inds = xvalues if invalid else new_x
1657
1666
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 ],
1659
1672
yvalues [firstIndex :][valid ])
1660
1673
1661
- if limit :
1674
+ if limit and invalid :
1662
1675
result [violate_limit ] = np .nan
1663
1676
return result
1664
1677
1665
1678
sp_methods = ['nearest' , 'zero' , 'slinear' , 'quadratic' , 'cubic' ,
1666
1679
'barycentric' , 'krogh' , 'spline' , 'polynomial' ,
1667
1680
'piecewise_polynomial' , 'pchip' ]
1668
1681
if method in sp_methods :
1669
- new_x = new_x [firstIndex :]
1682
+ if invalid :
1683
+ new_x = new_x [firstIndex :]
1670
1684
1671
- result [ firstIndex :][ invalid ] = _interpolate_scipy_wrapper (
1685
+ _temp_res = _interpolate_scipy_wrapper (
1672
1686
valid_x , valid_y , new_x , method = method , fill_value = fill_value ,
1673
1687
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 :
1675
1693
result [violate_limit ] = np .nan
1676
1694
return result
1677
1695
0 commit comments