Skip to content

Commit

Permalink
adding reindex_like functions
Browse files Browse the repository at this point in the history
  • Loading branch information
wesm committed Nov 22, 2010
1 parent b7c122d commit 22afce7
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 3 deletions.
20 changes: 20 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,26 @@ def _reindex_columns(self, columns):

return result

def reindex_like(self, other, fillMethod=None):
"""
Reindex DataFrame to match indices of another DataFrame
Parameters
----------
other : DataFrame
fillMethod : string or None
Notes
-----
Like calling s.reindex(index=other.index, columns=other.columns)
Returns
-------
reindexed : DataFrame
"""
# todo: object columns
return self.reindex(index=other.index, columns=other.columns)

def groupby(self, mapper, axis=0):
"""
Goup series using mapper (dict or key function, apply given
Expand Down
19 changes: 19 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,25 @@ def reindex(self, newIndex, fillMethod=None):

return Series(newValues, index=newIndex)

def reindex_like(self, other, fillMethod=None):
"""
Reindex Series to match index of another Series
Parameters
----------
other : Series
fillMethod : string or None
Notes
-----
Like calling s.reindex(other.index)
Returns
-------
reindexed : Series
"""
return self.reindex(other.index, fillMethod=fillMethod)

def fill(self, value=None, method='pad'):
"""
Fill NaN values using the specified method.
Expand Down
6 changes: 6 additions & 0 deletions pandas/core/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,12 @@ def test_reindex_int(self):
smaller = self.intframe.reindex(columns=['A', 'B'])
self.assert_(smaller['A'].dtype == np.int_)

def test_reindex_like(self):
other = self.frame.reindex(index=self.frame.index[:10],
columns=['C', 'B'])

assert_frame_equal(other, self.frame.reindex_like(other))

def test_rename(self):
mapping = {
'A' : 'a',
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,11 @@ def test_reindex_bool_pad(self):
filled_bool = bool_ts.reindex(self.ts.index, fillMethod='pad')
self.assert_(isnull(filled_bool[:5]).all())

def test_reindex_like(self):
other = self.ts[::2]
assert_series_equal(self.ts.reindex(other.index),
self.ts.reindex_like(other))

def test_rename(self):
renamer = lambda x: x.strftime('%Y%m%d')
renamed = self.ts.rename(renamer)
Expand Down
2 changes: 1 addition & 1 deletion pandas/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from datetime import datetime

version = '0.3.0.dev20101012'
version = '0.3.0.dev20101119'
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

DISTNAME = 'pandas'
LICENSE = 'BSD'
AUTHOR = "AQR Capital Management, LLC"
MAINTAINER = "AQR Capital Management, LLC"
MAINTAINER_EMAIL = "[email protected]"
URL = "http://pandas.sourceforge.net"
Expand Down
4 changes: 2 additions & 2 deletions test.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/sh
#nosetests -w pandas/core --with-coverage --cover-package=pandas.core --pdb-failure
coverage erase
nosetests -w pandas/stats --with-coverage --cover-package=pandas.stats
nosetests -w pandas/core --with-coverage --cover-package=pandas.core --pdb-failure
# nosetests -w pandas/stats --with-coverage --cover-package=pandas.stats
# coverage run runtests.py

0 comments on commit 22afce7

Please sign in to comment.