Skip to content

BUG: reindexing a frame fails to pick nan value #8108

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
Mar 5, 2015
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
2 changes: 1 addition & 1 deletion pandas/src/klib/khash_python.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// kludge

#define kh_float64_hash_func _Py_HashDouble
#define kh_float64_hash_equal kh_int64_hash_equal
#define kh_float64_hash_equal(a, b) ((a) == (b) || ((b) != (b) && (a) != (a)))

#define KHASH_MAP_INIT_FLOAT64(name, khval_t) \
KHASH_INIT(name, khfloat64_t, khval_t, 1, kh_float64_hash_func, kh_float64_hash_equal)
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,16 @@ def test_duplicated_drop_duplicates(self):
s.drop_duplicates(inplace=True)
tm.assert_series_equal(s, original)


class TestFloat64HashTable(tm.TestCase):
def test_lookup_nan(self):
from pandas.hashtable import Float64HashTable
xs = np.array([2.718, 3.14, np.nan, -7, 5, 2, 3])
m = Float64HashTable()
m.map_locations(xs)
self.assert_numpy_array_equal(m.lookup(xs), np.arange(len(xs)))


if __name__ == '__main__':
import nose

Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -9263,6 +9263,16 @@ def test_reindex(self):
assert_frame_equal(result,self.frame)
self.assertFalse(result is self.frame)

def test_reindex_nan(self):
df = pd.DataFrame([[1, 2], [3, 5], [7, 11], [9, 23]],
index=[2, np.nan, 1, 5], columns=['joe', 'jim'])

i, j = [np.nan, 5, 5, np.nan, 1, 2, np.nan], [1, 3, 3, 1, 2, 0, 1]
tm.assert_frame_equal(df.reindex(i), df.iloc[j])

df.index = df.index.astype('object')
tm.assert_frame_equal(df.reindex(i), df.iloc[j])

def test_reindex_name_remains(self):
s = Series(random.rand(10))
df = DataFrame(s, index=np.arange(len(s)))
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ def test_symmetric_diff(self):
# expected = Index([0.0, np.nan, 2.0, 3.0, np.nan])

nans = pd.isnull(result)
self.assertEqual(nans.sum(), 2)
self.assertEqual(nans.sum(), 1)
self.assertEqual((~nans).sum(), 3)
[self.assertIn(x, result) for x in [0.0, 2.0, 3.0]]

Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -5846,6 +5846,15 @@ def test_reindex(self):
result = self.ts.reindex()
self.assertFalse((result is self.ts))

def test_reindex_nan(self):
ts = Series([2, 3, 5, 7], index=[1, 4, nan, 8])

i, j = [nan, 1, nan, 8, 4, nan], [2, 0, 2, 3, 1, 2]
assert_series_equal(ts.reindex(i), ts.iloc[j])

ts.index = ts.index.astype('object')
assert_series_equal(ts.reindex(i), ts.iloc[j])

def test_reindex_corner(self):
# (don't forget to fix this) I think it's fixed
reindexed_dep = self.empty.reindex(self.ts.index, method='pad')
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,8 @@ def pxd(name):
'pxdfiles': [],
'depends': lib_depends},
hashtable={'pyxfile': 'hashtable',
'pxdfiles': ['hashtable']},
'pxdfiles': ['hashtable'],
'depends': ['pandas/src/klib/khash_python.h']},
tslib={'pyxfile': 'tslib',
'depends': tseries_depends,
'sources': ['pandas/src/datetime/np_datetime.c',
Expand Down