Skip to content

Commit 68cdd46

Browse files
raph-mjorisvandenbossche
authored andcommitted
DOC: correct merge_ordered example (#19401)
* issue #19393. update of incorrect documentation example for merge_ordered. adding a test corresponding to this example.
1 parent 41e02a0 commit 68cdd46

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

pandas/core/reshape/merge.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -194,19 +194,17 @@ def merge_ordered(left, right, on=None,
194194
5 e 3 b
195195
196196
>>> merge_ordered(A, B, fill_method='ffill', left_by='group')
197-
key lvalue group rvalue
198-
0 a 1 a NaN
199-
1 b 1 a 1
200-
2 c 2 a 2
201-
3 d 2 a 3
202-
4 e 3 a 3
203-
5 f 3 a 4
204-
6 a 1 b NaN
205-
7 b 1 b 1
206-
8 c 2 b 2
207-
9 d 2 b 3
208-
10 e 3 b 3
209-
11 f 3 b 4
197+
group key lvalue rvalue
198+
0 a a 1 NaN
199+
1 a b 1 1.0
200+
2 a c 2 2.0
201+
3 a d 2 3.0
202+
4 a e 3 3.0
203+
5 b a 1 NaN
204+
6 b b 1 1.0
205+
7 b c 2 2.0
206+
8 b d 2 3.0
207+
9 b e 3 3.0
210208
211209
Returns
212210
-------

pandas/tests/reshape/merge/test_merge_ordered.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,21 @@ def test_empty_sequence_concat(self):
8181
pd.concat([pd.DataFrame()])
8282
pd.concat([None, pd.DataFrame()])
8383
pd.concat([pd.DataFrame(), None])
84+
85+
def test_doc_example(self):
86+
left = DataFrame({'key': ['a', 'c', 'e', 'a', 'c', 'e'],
87+
'lvalue': [1, 2, 3] * 2,
88+
'group': list('aaabbb')})
89+
90+
right = DataFrame({'key': ['b', 'c', 'd'],
91+
'rvalue': [1, 2, 3]})
92+
93+
result = merge_ordered(left, right, fill_method='ffill',
94+
left_by='group')
95+
96+
expected = DataFrame({'group': list('aaaaabbbbb'),
97+
'key': ['a', 'b', 'c', 'd', 'e'] * 2,
98+
'lvalue': [1, 1, 2, 2, 3] * 2,
99+
'rvalue': [nan, 1, 2, 3, 3] * 2})
100+
101+
assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)