Skip to content
This repository was archived by the owner on Dec 22, 2019. It is now read-only.

Commit a8f03b7

Browse files
author
araraonline
committed
Ignore empty indexes in _merge_index_list
This reflects in the columns index of DataFrame.append, it will ignore empty indexes (of dtype object)! Some tests are not passing, but this is due to columns dtypes, not indexes.
1 parent c1e8e0f commit a8f03b7

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

pandas/core/indexes/api.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,14 @@ def _merge_index_list(index_list,
276276
When verify_dups=True and at least one of the input indexes contain
277277
duplicate values. This is error is not raised if
278278
allow_matching_dups=True and all the indexes have a common identity.
279+
280+
Notes
281+
-----
282+
Empty indexes (of object dtype) are forgotten.
279283
"""
280284
# unique index list (a is b)
281285
uindex_list = com.get_distinct_objs(index_list)
286+
uindex_list = [i for i in uindex_list if not i.is_empty()]
282287

283288
# verify duplicates
284289
if sort or verify_dups:

pandas/core/indexes/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,6 +1541,9 @@ def is_unique(self):
15411541
def has_duplicates(self):
15421542
return not self.is_unique
15431543

1544+
def is_empty(self):
1545+
return self.inferred_type in ['empty']
1546+
15441547
def is_boolean(self):
15451548
return self.inferred_type in ['boolean']
15461549

pandas/tests/reshape/test_append.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,35 @@ def test_preserve_index_type(self, sort, index):
388388
expected = pd.DataFrame([[1, 2, np.nan], [3, 4, 5]], columns=index)
389389
assert_frame_equal(result, expected)
390390

391+
def test_ignore_empty_index_dtype(self, sort):
392+
# When one of the indexes is empty and of object dtype, it should be
393+
# ignored in the result (as empty).
394+
395+
df1 = pd.DataFrame()
396+
df2 = pd.DataFrame([[11, 12, 13]], columns=[1, 2, 3])
397+
398+
result1 = df1.append(df2, sort=sort)
399+
result2 = df2.append(df1, sort=sort)
400+
401+
expected = df2.copy()
402+
assert_frame_equal(result1, expected)
403+
assert_frame_equal(result2, expected)
404+
405+
def test_account_empty_index_dtype(self, sort):
406+
# When one of the indexes is empty and of dtype different from object,
407+
# it should not be ignored when calculating the result dtype.
408+
409+
df1 = pd.DataFrame(columns=pd.Float64Index([]))
410+
df2 = pd.DataFrame([[11, 12, 13]], columns=[1, 2, 3])
411+
412+
result1 = df1.append(df2, sort=sort)
413+
result2 = df2.append(df1, sort=sort)
414+
415+
expected = df2.copy()
416+
expected.columns = [1.0, 2.0, 3.0]
417+
assert_frame_equal(result1, expected)
418+
assert_frame_equal(result2, expected)
419+
391420
@pytest.mark.parametrize('index2', indexes, ids=cls_name)
392421
@pytest.mark.parametrize('index1', indexes, ids=cls_name)
393422
def test_preserve_index_values_without_sort(self, index1, index2):
@@ -751,6 +780,35 @@ def test_preserve_index_name(self, sort, idx_name1, idx_name2, idx_name3):
751780
expected.index.name = idx_name1
752781
assert_frame_equal(result, expected)
753782

783+
def test_ignore_empty_index_dtype(self, sort):
784+
# When one of the indexes is empty and of object dtype, it should be
785+
# ignored in the result (as empty).
786+
787+
df1 = pd.DataFrame()
788+
df2 = pd.DataFrame([[11], [12], [13]], index=[1, 2, 3])
789+
790+
result1 = df1.append(df2, sort=sort)
791+
result2 = df2.append(df1, sort=sort)
792+
793+
expected = df2.copy()
794+
assert_frame_equal(result1, expected)
795+
assert_frame_equal(result2, expected)
796+
797+
def test_account_empty_index_dtype(self, sort):
798+
# When one of the indexes is empty and of dtype different from object,
799+
# it should not be ignored when calculating the result dtype.
800+
801+
df1 = pd.DataFrame(index=pd.Float64Index([]))
802+
df2 = pd.DataFrame([[11], [12], [13]], index=[1, 2, 3])
803+
804+
result1 = df1.append(df2, sort=sort)
805+
result2 = df2.append(df1, sort=sort)
806+
807+
expected = df2.copy()
808+
expected.index = [1.0, 2.0, 3.0]
809+
assert_frame_equal(result1, expected)
810+
assert_frame_equal(result2, expected)
811+
754812
@pytest.mark.parametrize('index', indexes, ids=cls_name)
755813
def test_preserve_index_type(self, sort, index):
756814
# when there's only one index type in the inputs,

0 commit comments

Comments
 (0)