From 87bb1ac9c72a7881c33e4e431ddf173210337fea Mon Sep 17 00:00:00 2001 From: SaturninoMateus Date: Sat, 16 Jun 2018 16:16:18 -0300 Subject: [PATCH 1/8] Fixes #21510 Remove sort if its already an ordered dict --- pandas/core/reshape/concat.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pandas/core/reshape/concat.py b/pandas/core/reshape/concat.py index b36e9b8d900fd..b5de3edef1cb3 100644 --- a/pandas/core/reshape/concat.py +++ b/pandas/core/reshape/concat.py @@ -1,7 +1,7 @@ """ concat routines """ - +from collections import OrderedDict import numpy as np from pandas import compat, DataFrame, Series, Index, MultiIndex from pandas.core.index import (_get_objs_combined_axis, @@ -250,7 +250,10 @@ def __init__(self, objs, axis=0, join='outer', join_axes=None, if isinstance(objs, dict): if keys is None: - keys = sorted(objs) + if not isinstance(objs,OrderedDict): + keys = sorted(objs) + else: + keys = objs objs = [objs[k] for k in keys] else: objs = list(objs) From f9d105ea167ad9fd38f293b061c8a61a07a33272 Mon Sep 17 00:00:00 2001 From: SaturninoMateus Date: Sat, 16 Jun 2018 18:50:18 -0300 Subject: [PATCH 2/8] Adding a space and new line to match PEP8. #21510 --- pandas/core/reshape/concat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/reshape/concat.py b/pandas/core/reshape/concat.py index b5de3edef1cb3..d311f07c144ce 100644 --- a/pandas/core/reshape/concat.py +++ b/pandas/core/reshape/concat.py @@ -250,7 +250,7 @@ def __init__(self, objs, axis=0, join='outer', join_axes=None, if isinstance(objs, dict): if keys is None: - if not isinstance(objs,OrderedDict): + if not isinstance(objs, OrderedDict): keys = sorted(objs) else: keys = objs From b044e0459f3584543f081c8d6cc861230e343bac Mon Sep 17 00:00:00 2001 From: SaturninoMateus Date: Sat, 16 Jun 2018 22:17:46 -0300 Subject: [PATCH 3/8] Adding test case for concat OrderedDict and updating . #21510 --- doc/source/whatsnew/v0.23.2.txt | 3 ++- pandas/tests/reshape/test_concat.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.23.2.txt b/doc/source/whatsnew/v0.23.2.txt index b8d865195cddd..32d9f4d42f6c2 100644 --- a/doc/source/whatsnew/v0.23.2.txt +++ b/doc/source/whatsnew/v0.23.2.txt @@ -83,4 +83,5 @@ Bug Fixes **Other** - Bug in :meth:`Series.nlargest` for signed and unsigned integer dtypes when the minimum value is present (:issue:`21426`) -- +- Bug in :class:`_Concatenator` should not sort Ordered Dictionaries (:issue:21510`) +- \ No newline at end of file diff --git a/pandas/tests/reshape/test_concat.py b/pandas/tests/reshape/test_concat.py index dea305d4b3fee..5baccad5d6c44 100644 --- a/pandas/tests/reshape/test_concat.py +++ b/pandas/tests/reshape/test_concat.py @@ -1,3 +1,4 @@ +from collections import OrderedDict from warnings import catch_warnings from itertools import combinations, product @@ -1294,6 +1295,16 @@ def test_concat_rename_index(self): tm.assert_frame_equal(result, exp) assert result.index.names == exp.index.names + def test_concat_with_ordered_dict(self): + # GH 21510 + ps = pd.concat(OrderedDict([('First', pd.Series(range(3))), + ('Another', pd.Series(range(4)))])) + ps_list = list(ps.keys()) + exp_list = [('First', 0), ('First', 1), ('First', 2), + ('Another', 0), ('Another', 1), + ('Another', 2), ('Another', 3)] + assert ps_list == exp_list + def test_crossed_dtypes_weird_corner(self): columns = ['A', 'B', 'C', 'D'] df1 = DataFrame({'A': np.array([1, 2, 3, 4], dtype='f8'), From eaa7cc0b688c42aef0c2bba29d0c571ccb6d4b7f Mon Sep 17 00:00:00 2001 From: SaturninoMateus Date: Sat, 16 Jun 2018 22:22:37 -0300 Subject: [PATCH 4/8] Fixing v0.23.2.txt syntax . #21510 --- doc/source/whatsnew/v0.23.2.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.23.2.txt b/doc/source/whatsnew/v0.23.2.txt index 32d9f4d42f6c2..16be8c5226c4c 100644 --- a/doc/source/whatsnew/v0.23.2.txt +++ b/doc/source/whatsnew/v0.23.2.txt @@ -83,5 +83,5 @@ Bug Fixes **Other** - Bug in :meth:`Series.nlargest` for signed and unsigned integer dtypes when the minimum value is present (:issue:`21426`) -- Bug in :class:`_Concatenator` should not sort Ordered Dictionaries (:issue:21510`) +- Bug in :class:`_Concatenator` should not sort Ordered Dictionaries (:issue:`21510`) - \ No newline at end of file From 6869e2c8618c004a288c4c2e0eef92f9437a653a Mon Sep 17 00:00:00 2001 From: SaturninoMateus Date: Sun, 17 Jun 2018 03:44:27 -0300 Subject: [PATCH 5/8] Improving test case for orderedDict and whatsnew info. #21510 --- doc/source/whatsnew/v0.23.2.txt | 2 +- pandas/tests/reshape/test_concat.py | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/doc/source/whatsnew/v0.23.2.txt b/doc/source/whatsnew/v0.23.2.txt index 16be8c5226c4c..12ee072fe660b 100644 --- a/doc/source/whatsnew/v0.23.2.txt +++ b/doc/source/whatsnew/v0.23.2.txt @@ -83,5 +83,5 @@ Bug Fixes **Other** - Bug in :meth:`Series.nlargest` for signed and unsigned integer dtypes when the minimum value is present (:issue:`21426`) -- Bug in :class:`_Concatenator` should not sort Ordered Dictionaries (:issue:`21510`) +- Bug in :class:`_Concatenator` should maintain dict ordering when :meth:`concat` is called (:issue:`2151`) - \ No newline at end of file diff --git a/pandas/tests/reshape/test_concat.py b/pandas/tests/reshape/test_concat.py index 5baccad5d6c44..c7c988050b8ad 100644 --- a/pandas/tests/reshape/test_concat.py +++ b/pandas/tests/reshape/test_concat.py @@ -1297,13 +1297,14 @@ def test_concat_rename_index(self): def test_concat_with_ordered_dict(self): # GH 21510 - ps = pd.concat(OrderedDict([('First', pd.Series(range(3))), - ('Another', pd.Series(range(4)))])) - ps_list = list(ps.keys()) - exp_list = [('First', 0), ('First', 1), ('First', 2), - ('Another', 0), ('Another', 1), - ('Another', 2), ('Another', 3)] - assert ps_list == exp_list + result = pd.concat(OrderedDict([('First', pd.Series(range(3))), + ('Another', pd.Series(range(4)))])) + a = pd.Series(range(3), range(3)) + b = pd.Series(range(4), range(4)) + a.index = pd.MultiIndex.from_tuples([('First', v) for v in a.index]) + b.index = pd.MultiIndex.from_tuples([('Another', v) for v in b.index]) + expected = a.append(b) + tm.assert_series_equal(result, expected) def test_crossed_dtypes_weird_corner(self): columns = ['A', 'B', 'C', 'D'] From e9561fe22da758ca7e0502dfd2471b2696dc1b92 Mon Sep 17 00:00:00 2001 From: SaturninoMateus Date: Tue, 19 Jun 2018 02:50:04 -0400 Subject: [PATCH 6/8] Refactoring test case code for orderedDict and improving whatsnew info. #21510 --- doc/source/whatsnew/v0.23.2.txt | 2 +- pandas/tests/reshape/test_concat.py | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/doc/source/whatsnew/v0.23.2.txt b/doc/source/whatsnew/v0.23.2.txt index 12ee072fe660b..a9a304d0d8581 100644 --- a/doc/source/whatsnew/v0.23.2.txt +++ b/doc/source/whatsnew/v0.23.2.txt @@ -83,5 +83,5 @@ Bug Fixes **Other** - Bug in :meth:`Series.nlargest` for signed and unsigned integer dtypes when the minimum value is present (:issue:`21426`) -- Bug in :class:`_Concatenator` should maintain dict ordering when :meth:`concat` is called (:issue:`2151`) +- Bug in :meth:`concat` should maintain dict ordering when :meth:`concat` is called (:issue:`2151`) - \ No newline at end of file diff --git a/pandas/tests/reshape/test_concat.py b/pandas/tests/reshape/test_concat.py index c7c988050b8ad..3ed77c87c462d 100644 --- a/pandas/tests/reshape/test_concat.py +++ b/pandas/tests/reshape/test_concat.py @@ -1299,11 +1299,10 @@ def test_concat_with_ordered_dict(self): # GH 21510 result = pd.concat(OrderedDict([('First', pd.Series(range(3))), ('Another', pd.Series(range(4)))])) - a = pd.Series(range(3), range(3)) - b = pd.Series(range(4), range(4)) - a.index = pd.MultiIndex.from_tuples([('First', v) for v in a.index]) - b.index = pd.MultiIndex.from_tuples([('Another', v) for v in b.index]) - expected = a.append(b) + index = MultiIndex(levels=[['First', 'Another'], [0, 1, 2, 3]], + labels=[[0, 0, 0, 1, 1, 1, 1], [0, 1, 2, 0, 1, 2, 3]]) + data = list(range(3)) + list(range(4)) + expected = pd.Series(data,index=index) tm.assert_series_equal(result, expected) def test_crossed_dtypes_weird_corner(self): From 53aca810c74e1ad41737016411a625685bf4efde Mon Sep 17 00:00:00 2001 From: SaturninoMateus Date: Tue, 19 Jun 2018 03:08:39 -0400 Subject: [PATCH 7/8] Fixing spaces to match with PEP8. #21510 --- pandas/tests/reshape/test_concat.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/tests/reshape/test_concat.py b/pandas/tests/reshape/test_concat.py index 3ed77c87c462d..b7b38618be0b3 100644 --- a/pandas/tests/reshape/test_concat.py +++ b/pandas/tests/reshape/test_concat.py @@ -1300,9 +1300,10 @@ def test_concat_with_ordered_dict(self): result = pd.concat(OrderedDict([('First', pd.Series(range(3))), ('Another', pd.Series(range(4)))])) index = MultiIndex(levels=[['First', 'Another'], [0, 1, 2, 3]], - labels=[[0, 0, 0, 1, 1, 1, 1], [0, 1, 2, 0, 1, 2, 3]]) + labels=[[0, 0, 0, 1, 1, 1, 1], + [0, 1, 2, 0, 1, 2, 3]]) data = list(range(3)) + list(range(4)) - expected = pd.Series(data,index=index) + expected = pd.Series(data, index=index) tm.assert_series_equal(result, expected) def test_crossed_dtypes_weird_corner(self): From a7aa0523fbc5845b32d5c99bf8bfe7d9478b1775 Mon Sep 17 00:00:00 2001 From: SaturninoMateus Date: Wed, 20 Jun 2018 22:03:07 -0400 Subject: [PATCH 8/8] Adding info about pd.concat method into whatsnew 0.24.0 #21510 --- doc/source/whatsnew/v0.24.0.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index 15c5cc97b8426..86cf9774e2add 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -255,6 +255,6 @@ Other ^^^^^ - :meth: `~pandas.io.formats.style.Styler.background_gradient` now takes a ``text_color_threshold`` parameter to automatically lighten the text color based on the luminance of the background color. This improves readability with dark background colors without the need to limit the background colormap range. (:issue:`21258`) -- +- Bug in :meth:`concat` should maintain dict order when :meth:`concat` is called (:issue:`2151`) - -