Skip to content

Commit

Permalink
ENH: add option to use Series.values to interpolate, close pandas-dev…
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed May 29, 2012
1 parent 2c1de1a commit 8526264
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
3 changes: 2 additions & 1 deletion RELEASE.rst
Expand Up @@ -55,7 +55,8 @@ pandas 0.8.0
DataFrame (#929, #1241) DataFrame (#929, #1241)
- Add 'kde' plot kind for Series/DataFrame.plot (#1059) - Add 'kde' plot kind for Series/DataFrame.plot (#1059)
- More flexible multiple function aggregation with GroupBy - More flexible multiple function aggregation with GroupBy
- Add pct_chagne function - Add pct_change function to Series/DataFrame
- Add option to interpolate by Index values in Series.interpolate (#1206)


**Improvements to existing features** **Improvements to existing features**


Expand Down
9 changes: 7 additions & 2 deletions pandas/core/series.py
Expand Up @@ -2527,10 +2527,11 @@ def interpolate(self, method='linear'):
Parameters Parameters
---------- ----------
method : {'linear', 'time'} method : {'linear', 'time', 'values'}
Interpolation method. Interpolation method.
Time interpolation works on daily and higher resolution 'time' interpolation works on daily and higher resolution
data to interpolate given length of interval data to interpolate given length of interval
'values' using the actual index numeric values
Returns Returns
------- -------
Expand All @@ -2541,6 +2542,10 @@ def interpolate(self, method='linear'):
raise Exception('time-weighted interpolation only works' raise Exception('time-weighted interpolation only works'
'on TimeSeries') 'on TimeSeries')
inds = np.array([d.toordinal() for d in self.index]) inds = np.array([d.toordinal() for d in self.index])
elif method == 'values':
inds = self.index.values
if inds.dtype == np.object_:
inds = lib.maybe_convert_objects(inds)
else: else:
inds = np.arange(len(self)) inds = np.arange(len(self))


Expand Down
15 changes: 15 additions & 0 deletions pandas/tests/test_series.py
Expand Up @@ -2685,6 +2685,21 @@ def test_interpolate(self):
# try time interpolation on a non-TimeSeries # try time interpolation on a non-TimeSeries
self.assertRaises(Exception, self.series.interpolate, method='time') self.assertRaises(Exception, self.series.interpolate, method='time')


def test_interpolate_index_values(self):
s = Series(np.nan, index=np.sort(np.random.rand(30)))
s[::3] = np.random.randn(10)

vals = s.index.values.astype(float)

result = s.interpolate(method='values')

expected = s.copy()
bad = isnull(expected.values)
good = -bad
expected[bad] = np.interp(vals[bad], vals[good], s.values[good])

assert_series_equal(result, expected)

def test_weekday(self): def test_weekday(self):
# Just run the function # Just run the function
weekdays = self.ts.weekday weekdays = self.ts.weekday
Expand Down
1 change: 0 additions & 1 deletion pandas/tseries/tests/test_offsets.py
Expand Up @@ -1319,7 +1319,6 @@ def test_alias_equality(self):
for k, v in _offset_map.iteritems(): for k, v in _offset_map.iteritems():
if v is None: if v is None:
continue continue
foo
self.assertEqual(k, v.copy()) self.assertEqual(k, v.copy())


def test_rule_code(self): def test_rule_code(self):
Expand Down
1 change: 0 additions & 1 deletion pandas/tseries/tests/test_timezones.py
Expand Up @@ -282,7 +282,6 @@ def test_tz_convert(self):
expected = DataFrame({'a': 1}, rng.tz_convert('UTC')) expected = DataFrame({'a': 1}, rng.tz_convert('UTC'))
self.assert_(result.index.tz.zone == 'UTC') self.assert_(result.index.tz.zone == 'UTC')
assert_frame_equal(result, expected) assert_frame_equal(result, expected)
foo


df = df.T df = df.T
result = df.tz_convert('utc', axis=1) result = df.tz_convert('utc', axis=1)
Expand Down

0 comments on commit 8526264

Please sign in to comment.